diff --git a/README.rdoc b/README.rdoc index 9b372f1..f509875 100644 --- a/README.rdoc +++ b/README.rdoc @@ -36,6 +36,7 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and Neography::Rest.create_node("age" => 31, "name" => "Max") # Create a node with some properties Neography::Rest.get_node(id) # Get a node and its properties Neography::Rest.delete_node(id) # Delete an unrelated node + Neography::Rest.reset_node_properties(id, {"age" => 31}) # Reset a node's properties Neography::Rest.set_node_properties(id, {"weight" => 200}) # Set a node's properties Neography::Rest.get_node_properties(id) # Get just the node properties @@ -43,7 +44,13 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and Neography::Rest.remove_node_properties(id) # Remove all properties of a node Neography::Rest.remove_node_properties(id, "weight") # Remove one property of a node Neography::Rest.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node - Neography::Rest.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 + + Neography::Rest.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 + Neography::Rest.get_node_relationships(new_node1[:id]) # Get all relationships + Neography::Rest.get_node_relationships(new_node1[:id], "incoming") # Get only incoming relationships + Neography::Rest.get_node_relationships(new_node1[:id], "all", "enemies") # Get all relationships of type enemies + Neography::Rest.get_node_relationships(new_node1[:id], "incoming", "enemies") # Get only incoming relationships of type enemies + ... and a work in progress more rubyish layer that's not quite ready for use yet. diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index c1cd158..e9262c8 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -68,6 +68,23 @@ def create_relationship(type, from, to, props = nil) rescue_ij { post("/node/#{from}/relationships", options) } 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 + + if types.nil? + rescue_ij { get("/node/#{id}/relationships/#{dir}") } + else + rescue_ij { get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") } + end + end + private # Rescue from Invalid JSON error thrown by Crack Gem diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index 63787db..a6f5cae 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -266,4 +266,125 @@ end end + describe "get_node_relationships" do + it "can get a node's relationship" 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"}) + relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + relationships.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node1[:id] + relationships[0]["end"].split('/').last.should == new_node2[:id] + relationships[0]["type"].should == "friends" + relationships[0]["data"]["met"].should == "college" + relationships[0]["data"]["since"].should == '10-1-2005' + end + + it "can get a node's multiple relationships" 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_node3 = Neography::Rest.create_node + 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_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + relationships.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node1[:id] + relationships[0]["end"].split('/').last.should == new_node2[:id] + relationships[0]["type"].should == "friends" + relationships[0]["data"]["met"].should == "college" + relationships[0]["data"]["since"].should == '10-1-2005' + relationships[1]["start"].split('/').last.should == new_node1[:id] + relationships[1]["end"].split('/').last.should == new_node3[:id] + relationships[1]["type"].should == "enemies" + relationships[1]["data"]["met"].should == "work" + relationships[1]["data"]["since"].should == '10-2-2010' + end + + it "can get a node's outgoing relationship" 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_node3 = Neography::Rest.create_node + 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.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node1[:id] + relationships[0]["end"].split('/').last.should == new_node2[:id] + relationships[0]["type"].should == "friends" + relationships[0]["data"]["met"].should == "college" + relationships[0]["data"]["since"].should == '10-1-2005' + relationships[1].should be_nil + end + + it "can get a node's incoming relationship" 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_node3 = Neography::Rest.create_node + 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.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node3[:id] + relationships[0]["end"].split('/').last.should == new_node1[:id] + relationships[0]["type"].should == "enemies" + relationships[0]["data"]["met"].should == "work" + relationships[0]["data"]["since"].should == '10-2-2010' + relationships[1].should be_nil + end + + it "can get a specific type of node relationships" 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_node3 = Neography::Rest.create_node + 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_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"}) + relationships = Neography::Rest.get_node_relationships(new_node1[:id], "all", "friends") + relationships.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node1[:id] + relationships[0]["end"].split('/').last.should == new_node2[:id] + relationships[0]["type"].should == "friends" + relationships[0]["data"]["met"].should == "college" + relationships[0]["data"]["since"].should == '10-1-2005' + relationships[1].should be_nil + end + + it "can get a specific type and direction of a node relationships" 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_node3 = Neography::Rest.create_node + new_node3[:id] = new_node3["self"].split('/').last + new_node4 = Neography::Rest.create_node + new_node4[:id] = new_node4["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_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.should_not be_nil + relationships[0]["start"].split('/').last.should == new_node4[:id] + relationships[0]["end"].split('/').last.should == new_node1[:id] + relationships[0]["type"].should == "enemies" + relationships[0]["data"]["met"].should == "gym" + relationships[0]["data"]["since"].should == '10-3-2010' + relationships[1].should be_nil + end + + + end + end \ No newline at end of file