From 93e4fbd3bee52fb7578efa68d30616e651cd87fa Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Thu, 18 Nov 2010 15:28:33 -0800 Subject: [PATCH] adding get_path --- README.rdoc | 4 +-- lib/neography/rest.rb | 44 ++++++++++++++++++++------ spec/integration/rest_spec.rb | 59 +++++++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/README.rdoc b/README.rdoc index cf598a1..2f841eb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -48,9 +48,9 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and Neography::Rest.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 Neography::Rest.get_node_relationships(id) # Get all relationships - Neography::Rest.get_node_relationships(id, "incoming") # Get only incoming relationships + Neography::Rest.get_node_relationships(id, "in") # Get only incoming relationships Neography::Rest.get_node_relationships(id, "all", "enemies") # Get all relationships of type enemies - Neography::Rest.get_node_relationships(id, "incoming", "enemies") # Get only incoming relationships of type enemies + Neography::Rest.get_node_relationships(id, "in", "enemies") # Get only incoming relationships of type enemies Neography::Rest.delete_relationship(id) # Delete a relationship Neography::Rest.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index ed2458b..0a36524 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -109,14 +109,7 @@ def delete_relationship(id) end def get_node_relationships(id, dir=nil, types=nil) - case dir - when :incoming, "incoming" - dir = "in" - when :outgoing, "outgoing" - dir = "out" - else - dir = "all" - end + dir = get_dir(dir) if types.nil? node_relationships = rescue_ij { get("/node/#{id}/relationships/#{dir}") } || Array.new @@ -152,9 +145,19 @@ def get_index(key, value) index end + def get_path(from, to, relationships, depth=1, algorithm="allPaths") + options = { :body => {"to" => Neography::Config.to_s + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } + path = rescue_ij { post("/node/#{from}/path", options) } || Hash.new + end + + def get_paths(from, to, relationships, depth=1, algorithm="allPaths") + options = { :body => {"to" => Neography::Config.to_s + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } + paths = rescue_ij { post("/node/#{from}/paths", options) } || Array.new + end + private -# Rescue from Invalid JSON error thrown by Crack Gem + # Rescue from Invalid JSON error thrown by Crack Gem def rescue_ij(&block) begin @@ -166,6 +169,29 @@ def rescue_ij(&block) response end + def get_dir(dir) + case dir + when :incoming, "incoming", :in, "in" + "in" + when :outgoing, "outgoing", :out, "out" + "out" + else + "all" + end + end + + def get_algorithm(algorithm) + case algorithm + when :shortest, "shortest", :shortestPath, "shortestPath", :short, "short" + "shortestPath" + when :allSimplePaths, "allSimplePaths", :simple, "simple" + "allSimplePaths" + else + "allPaths" + end + end + + end end diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index f987d3f..f246b3c 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -521,7 +521,7 @@ new_node3[:id] = new_node3["self"].split('/').last new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "outgoing") + relationships = Neography::Rest.get_node_relationships(new_node1[:id], "out") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node1[:id] relationships[0]["end"].split('/').last.should == new_node2[:id] @@ -540,7 +540,7 @@ new_node3[:id] = new_node3["self"].split('/').last new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming") + relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node3[:id] relationships[0]["end"].split('/').last.should == new_node1[:id] @@ -581,7 +581,7 @@ new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) new_relationship = Neography::Rest.create_relationship("enemies", new_node4[:id], new_node1[:id], {"since" => '10-3-2010', "met" => "gym"}) - relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming", "enemies") + relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in", "enemies") relationships.should_not be_nil relationships[0]["start"].split('/').last.should == new_node4[:id] relationships[0]["end"].split('/').last.should == new_node1[:id] @@ -646,5 +646,58 @@ end end + describe "get path" do + it "can get a path between two nodes" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"}) + path = Neography::Rest.get_path(new_node1[:id], new_node2[:id], {"type"=> "friends", "direction" => "out"}) + path["start"].should == new_node1["self"] + path["end"].should == new_node2["self"] + path["nodes"].should == [new_node1["self"], new_node2["self"]] + end + + it "can get the shortest path between two nodes" do + pending + end + + it "can get a simple path between two nodes" do + pending + end + + it "can get a path between two nodes of max depth 3" do + pending + end + + it "can get a path between two nodes of a specific relationship" do + pending + end + end + + describe "get paths" do + it "can get the shortest paths between two nodes" do + pending + end + + it "can get all paths between two nodes" do + pending + end + + it "can get all simple paths between two nodes" do + pending + end + + it "can get paths between two nodes of max depth 3" do + pending + end + + it "can get paths between two nodes of a specific relationship" do + pending + end + + end + end \ No newline at end of file