Permalink
Browse files

major refactoring

  • Loading branch information...
Philipp Brüll
Philipp Brüll committed Mar 30, 2010
1 parent ab7f21c commit 9fe169037da40894d7aded7d2ac4a62e48e44125
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), "transport")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -15,7 +15,7 @@ class Federation
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(server, name, options = { })
@@ -26,7 +26,7 @@ def initialize(server, name, options = { })
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
@@ -1,117 +0,0 @@
-
-module AllegroGraph
-
- module Proxy
-
- class Geo
-
- attr_reader :resource
-
- def initialize(resource)
- @resource = resource
- end
-
- def path
- "#{@resource.path}/geo"
- end
-
- def cartesian_type(strip_width, x_min, y_min, x_max, y_max)
- parameters = {
- :stripWidth => strip_width.to_s,
- :xmin => x_min.to_s,
- :ymin => y_min.to_s,
- :xmax => x_max.to_s,
- :ymax => y_max.to_s
- }
- type = @resource.request :post, self.path + "/types/cartesian", :parameters => parameters, :expected_status_code => 200
- type.sub! /^.*</, "<"
- type.sub! />.*$/, ">"
- type
- end
-
- def spherical_type(strip_width, unit, latitude_min, longitude_min, latitude_max, longitude_max)
- parameters = {
- :stripWidth => strip_width.to_s,
- :unit => unit.to_s,
- :latmin => latitude_min.to_s,
- :longmin => longitude_min.to_s,
- :latmax => latitude_max.to_s,
- :longmax => longitude_max.to_s
- }
- type = @resource.request :post, self.path + "/types/spherical", :parameters => parameters, :expected_status_code => 200
- type.sub! /^.*</, "<"
- type.sub! />.*$/, ">"
- type
- end
-
- def create_polygon(name, type, points)
- raise ArgumentError, "at least three points has to defined" unless points.is_a?(Array) && points.size >= 3
- parameters = {
- :resource => "\"#{name}\"",
- :point => points.map{ |point| "\"%+g%+g\"^^%s" % [ point[0], point[1], type ] }
- }
- @resource.request :put, self.path + "/polygon", :parameters => parameters, :expected_status_code => 204
- true
- end
-
- def inside_box(type, predicate, x_min, y_min, x_max, y_max)
- parameters = {
- :type => type,
- :predicate => predicate,
- :xmin => x_min.to_s,
- :ymin => y_min.to_s,
- :xmax => x_max.to_s,
- :ymax => y_max.to_s
- }
- @resource.request :get, self.path + "/box", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_circle(type, predicate, x, y, radius)
- parameters = {
- :type => type,
- :predicate => predicate,
- :x => x.to_s,
- :y => y.to_s,
- :radius => radius.to_s
- }
- @resource.request :get, self.path + "/circle", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_haversine(type, predicate, latitude, longitude, radius, unit = :km)
- parameters = {
- :type => type,
- :predicate => predicate,
- :lat => float_to_iso_6709(latitude, 2),
- :long => float_to_iso_6709(longitude, 3),
- :radius => radius.to_s,
- :unit => unit.to_s
- }
- @resource.request :get, self.path + "/haversine", :parameters => parameters, :expected_status_code => 200
- end
-
- def inside_polygon(type, predicate, name)
- parameters = {
- :type => type,
- :predicate => predicate,
- :polygon => "\"#{name}\""
- }
- @resource.request :get, self.path + "/polygon", :parameters => parameters, :expected_status_code => 200
- end
-
- private
-
- def float_to_iso_6709(value, digits)
- sign = "+"
- if value < 0
- sign = "-"
- value = -value
- end
- floor = value.to_i
- sign + (("%%0%dd" % digits) % floor) + (".%07d" % ((value - floor) * 10000000))
- end
-
- end
-
- end
-
-end
@@ -0,0 +1,50 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "utility", "parameter_mapper"))
+
+module AllegroGraph
+
+ module Proxy
+
+ class Geometric
+
+ attr_reader :resource
+
+ def initialize(resource)
+ @resource = resource
+ end
+
+ def path
+ "#{@resource.path}/geo"
+ end
+
+ def cartesian_type(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :cartesian_type
+ type = @resource.request :post, self.path + "/types/cartesian", :parameters => parameters, :expected_status_code => 200
+ type.sub! /^.*</, "<"
+ type.sub! />.*$/, ">"
+ type
+ end
+
+ def spherical_type(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :spherical_type
+ type = @resource.request :post, self.path + "/types/spherical", :parameters => parameters, :expected_status_code => 200
+ type.sub! /^.*</, "<"
+ type.sub! />.*$/, ">"
+ type
+ end
+
+ def create_polygon(points, parameters = { })
+ type = parameters.delete :type
+ parameters = Utility::ParameterMapper.map parameters, :create_polygon
+
+ raise ArgumentError, "at least three points has to defined" unless points.is_a?(Array) && points.size >= 3
+ parameters[:point] = points.map{ |point| "\"%+g%+g\"^^%s" % [ point[0], point[1], type ] }
+
+ @resource.request :put, self.path + "/polygon", :parameters => parameters, :expected_status_code => 204
+ true
+ end
+
+ end
+
+ end
+
+end
@@ -1,3 +1,4 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "utility", "parameter_mapper"))
module AllegroGraph
@@ -12,16 +13,27 @@ def initialize(resource)
end
def path
- "#{@resource.path}/statements"
+ @resource.path
end
def create(subject, predicate, object, context = nil)
statement = [ subject, predicate, object ]
statement << context if context
- @resource.request :post, self.path, :body => [ statement ], :expected_status_code => 204
+ @resource.request :post, self.path + "/statements", :body => [ statement ], :expected_status_code => 204
true
end
+ def delete(options = { })
+ parameters = { }
+
+ { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
+ value = options[option_key]
+ parameters.merge! parameter_key => value if value
+ end
+
+ @resource.request :delete, self.path + "/statements", :parameters => parameters, :expected_status_code => 200
+ end
+
def find(options = { })
parameters = { }
@@ -38,18 +50,27 @@ def find(options = { })
parameters = nil if parameters.empty?
- @resource.request :get, self.path, :parameters => parameters, :expected_status_code => 200
+ @resource.request :get, self.path + "/statements", :parameters => parameters, :expected_status_code => 200
end
- def delete(options = { })
- parameters = { }
+ def find_inside_box(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_box
+ @resource.request :get, self.path + "/geo/box", :parameters => parameters, :expected_status_code => 200
+ end
- { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
- value = options[option_key]
- parameters.merge! parameter_key => value if value
- end
+ def find_inside_circle(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_circle
+ @resource.request :get, self.path + "/geo/circle", :parameters => parameters, :expected_status_code => 200
+ end
+
+ def find_inside_haversine(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_haversine
+ @resource.request :get, self.path + "/geo/haversine", :parameters => parameters, :expected_status_code => 200
+ end
- @resource.request :delete, self.path, :parameters => parameters, :expected_status_code => 200
+ def find_inside_polygon(parameters = { })
+ parameters = Utility::ParameterMapper.map parameters, :find_inside_polygon
+ @resource.request :get, self.path + "/geo/polygon", :parameters => parameters, :expected_status_code => 200
end
end
@@ -2,7 +2,7 @@
require File.join(File.dirname(__FILE__), "session")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -15,7 +15,7 @@ class Repository
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(server_or_catalog, name, options = { })
@@ -24,7 +24,7 @@ def initialize(server_or_catalog, name, options = { })
@name = name
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), "transport")
require File.join(File.dirname(__FILE__), "proxy", "statements")
require File.join(File.dirname(__FILE__), "proxy", "query")
-require File.join(File.dirname(__FILE__), "proxy", "geo")
+require File.join(File.dirname(__FILE__), "proxy", "geometric")
require File.join(File.dirname(__FILE__), "proxy", "mapping")
module AllegroGraph
@@ -14,7 +14,7 @@ class Session
attr_reader :statements
attr_reader :query
- attr_reader :geo
+ attr_reader :geometric
attr_reader :mapping
def initialize(options = { })
@@ -24,7 +24,7 @@ def initialize(options = { })
@statements = Proxy::Statements.new self
@query = Proxy::Query.new self
- @geo = Proxy::Geo.new self
+ @geometric = Proxy::Geometric.new self
@mapping = Proxy::Mapping.new self
end
@@ -0,0 +1,77 @@
+
+module AllegroGraph
+
+ module Utility
+
+ module ParameterMapper
+
+ MAP = {
+ :strip_width => { :to => :stripWidth, :for => [ :cartesian_type, :spherical_type ], :default => 1 },
+ :x_min => { :to => :xmin, :for => [ :cartesian_type, :find_inside_box ] },
+ :y_min => { :to => :ymin, :for => [ :cartesian_type, :find_inside_box ] },
+ :x_max => { :to => :xmax, :for => [ :cartesian_type, :find_inside_box ] },
+ :y_max => { :to => :ymax, :for => [ :cartesian_type, :find_inside_box ] },
+ :unit => { :to => :unit, :for => [ :spherical_type, :find_inside_haversine ], :default => :degree },
+ :latitude_min => { :to => :latmin, :for => [ :spherical_type ] },
+ :longitude_min => { :to => :longmin, :for => [ :spherical_type ] },
+ :latitude_max => { :to => :latmax, :for => [ :spherical_type ] },
+ :longitude_max => { :to => :longmax, :for => [ :spherical_type ] },
+ :name => { :to => :resource, :for => [ :create_polygon ], :modifier => [ :quote ] },
+ :type => { :to => :type, :for => [ :find_inside_box, :find_inside_circle, :find_inside_haversine, :find_inside_polygon ] },
+ :predicate => { :to => :predicate, :for => [ :find_inside_box, :find_inside_circle, :find_inside_haversine, :find_inside_polygon ] },
+ :x => { :to => :x, :for => [ :find_inside_circle ] },
+ :y => { :to => :y, :for => [ :find_inside_circle ] },
+ :radius => { :to => :radius, :for => [ :find_inside_circle, :find_inside_haversine ] },
+ :latitude => { :to => :lat, :for => [ :find_inside_haversine ], :modifier => :latitude_to_iso },
+ :longitude => { :to => :long, :for => [ :find_inside_haversine ], :modifier => :longitude_to_iso },
+ :polygon_name => { :to => :polygon, :for => [ :find_inside_polygon ], :modifier => [ :quote ] }
+ }.freeze unless defined?(MAP)
+
+ def self.map(parameters, method_name)
+ result = { }
+ MAP.each do |key, mapping|
+ value = parameters[key] || mapping[:default]
+ required = [ mapping[:for] ].flatten.include? method_name.to_sym
+ if required
+ if value
+ [ mapping[:modifier] ].flatten.compact.each do |modifier|
+ value = send modifier, value
+ end
+ result[ mapping[:to] ] = value.to_s
+ else
+ raise ArgumentError, "missing parameter :#{key}!"
+ end
+ end
+ end
+ result
+ end
+
+ def self.quote(value)
+ "\"#{value}\""
+ end
+
+ def self.latitude_to_iso(value)
+ float_to_iso value, 2
+ end
+
+ def self.longitude_to_iso(value)
+ float_to_iso value, 3
+ end
+
+ private
+
+ def self.float_to_iso(value, digits)
+ sign = "+"
+ if value < 0
+ sign = "-"
+ value = -value
+ end
+ floor = value.to_i
+ sign + (("%%0%dd" % digits) % floor) + (".%07d" % ((value - floor) * 10000000))
+ end
+
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit 9fe1690

Please sign in to comment.