diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index ac7c3ee..04034b3 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -52,6 +52,7 @@ class Rest include RelationshipIndexes include RelationshipAutoIndexes include NodeTraversal + include NodePaths extend Forwardable attr_reader :connection @@ -61,8 +62,6 @@ class Rest def initialize(options = ENV['NEO4J_URL'] || {}) @connection = Connection.new(options) - @node_paths ||= NodePaths.new(@connection) - @cypher ||= Cypher.new(@connection) @gremlin ||= Gremlin.new(@connection) @extensions ||= Extensions.new(@connection) @@ -102,20 +101,6 @@ def get_relationship_end_node(rel) get_node(rel["end"]) end - # paths - - def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath") - @node_paths.get(from, to, relationships, depth, algorithm) - end - - def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths") - @node_paths.get_all(from, to, relationships, depth, algorithm) - end - - def get_shortest_weighted_path(from, to, relationships, weight_attr = "weight", depth = 1, algorithm = "dijkstra") - @node_paths.shortest_weighted(from, to, relationships, weight_attr, depth, algorithm) - end - # cypher query def execute_query(query, params = {}, cypher_options = nil) diff --git a/lib/neography/rest/node_paths.rb b/lib/neography/rest/node_paths.rb index 2495ff9..26142dc 100644 --- a/lib/neography/rest/node_paths.rb +++ b/lib/neography/rest/node_paths.rb @@ -1,29 +1,21 @@ module Neography class Rest - class NodePaths - extend Neography::Rest::Paths + module NodePaths include Neography::Rest::Helpers - - add_path :base, "/node/:id/path" - add_path :all, "/node/:id/paths" - - def initialize(connection) - @connection ||= connection - end - - def get(from, to, relationships, depth = 1, algorithm = "shortestPath") + + def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath") options = path_options(to, relationships, depth, algorithm) - @connection.post(base_path(:id => get_id(from)), options) || {} + @connection.post("/node/%{id}/path" % {:id => get_id(from)}, options) || {} end - def get_all(from, to, relationships, depth = 1, algorithm = "allPaths") + def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths") options = path_options(to, relationships, depth, algorithm) - @connection.post(all_path(:id => get_id(from)), options) || [] + @connection.post("/node/%{id}/paths" % {:id => get_id(from)}, options) || [] end - def shortest_weighted(from, to, relationships, weight_attribute = "weight", depth = 1, algorithm = "dijkstra") + def get_shortest_weighted_path(from, to, relationships, weight_attribute = "weight", depth = 1, algorithm = "dijkstra") options = path_options(to, relationships, depth, algorithm, { :cost_property => weight_attribute }) - @connection.post(all_path(:id => get_id(from)), options) || {} + @connection.post("/node/%{id}/paths" % {:id => get_id(from)}, options) || {} end private diff --git a/spec/unit/rest/node_paths_spec.rb b/spec/unit/rest/node_paths_spec.rb index 16fb1d9..462f96c 100644 --- a/spec/unit/rest/node_paths_spec.rb +++ b/spec/unit/rest/node_paths_spec.rb @@ -4,53 +4,50 @@ module Neography class Rest describe NodePaths do - let(:connection) { double(:configuration => "http://configuration") } - subject { NodePaths.new(connection) } + subject { Neography::Rest.new } it "gets a shortest path between two nodes" do expected_body = { - "to" => "http://configuration/node/43", + "to" => "http://localhost:7474/node/43", "relationships" => "relationships", "max_depth" => 3, "algorithm" => "shortestPath" } - connection.should_receive(:post).with("/node/42/path", json_match(:body, expected_body)) + subject.connection.should_receive(:post).with("/node/42/path", json_match(:body, expected_body)) - subject.get("42", "43", "relationships", 3, "shortestPath") + subject.get_path("42", "43", "relationships", 3, "shortestPath") end it "gets all shortest paths between two nodes" do expected_body = { - "to" => "http://configuration/node/43", + "to" => "http://localhost:7474/node/43", "relationships" => "relationships", "max_depth" => 3, "algorithm" => "shortestPath" } - connection.should_receive(:post).with("/node/42/paths", json_match(:body, expected_body)) + subject.connection.should_receive(:post).with("/node/42/paths", json_match(:body, expected_body)) - subject.get_all("42", "43", "relationships", 3, "shortestPath") + subject.get_paths("42", "43", "relationships", 3, "shortestPath") end it "gets all shortest weighted paths between two nodes" do expected_body = { - "to" => "http://configuration/node/43", + "to" => "http://localhost:7474/node/43", "relationships" => "relationships", "cost_property" => "cost", "max_depth" => 3, "algorithm" => "shortestPath" } - connection.should_receive(:post).with("/node/42/paths", json_match(:body, expected_body)) + subject.connection.should_receive(:post).with("/node/42/paths", json_match(:body, expected_body)) - subject.shortest_weighted("42", "43", "relationships", "cost", 3, "shortestPath") + subject.get_shortest_weighted_path("42", "43", "relationships", "cost", 3, "shortestPath") end context "algorithm" do - subject { NodePaths.new(nil) } - [ :shortest, "shortest", :shortestPath, "shortestPath", :short, "short" ].each do |algorithm| it "parses shortestPath" do subject.send(:get_algorithm, algorithm).should == "shortestPath"