diff --git a/README.rdoc b/README.rdoc index f509875..15996db 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.delete_node!(id) # Delete a node and all its relationships 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 @@ -45,11 +46,12 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and 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.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 + 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, "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.delete_relationship(id) # Delete a relationship ... 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 e9262c8..19107ff 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -6,9 +6,9 @@ class Rest class << self - def get_root - rescue_ij { get('/') } - end + def get_root + rescue_ij { get('/') } + end def create_node(*args) if args[0].respond_to?(:each_pair) && args[0] @@ -68,6 +68,10 @@ def create_relationship(type, from, to, props = nil) rescue_ij { post("/node/#{from}/relationships", options) } end + def delete_relationship(id) + rescue_ij { delete("/relationship/#{id}") } + end + def get_node_relationships(id, dir=nil, types=nil) case dir when :incoming, "incoming" @@ -79,10 +83,18 @@ def get_node_relationships(id, dir=nil, types=nil) end if types.nil? - rescue_ij { get("/node/#{id}/relationships/#{dir}") } + node_relationships = rescue_ij { get("/node/#{id}/relationships/#{dir}") } || Array.new else - rescue_ij { get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") } + node_relationships = rescue_ij { get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") } || Array.new end + return nil if node_relationships.empty? + node_relationships + end + + def delete_node!(id) + relationships = get_node_relationships(id) + relationships.each { |r| delete_relationship(r["self"].split('/').last) } unless relationships.nil? + rescue_ij { delete("/node/#{id}") } end private diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index a6f5cae..64c5c68 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -266,6 +266,44 @@ end end + describe "delete_relationship" do + it "can delete an existing 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-2010', "met" => "college"}) + new_relationship[:id] = new_relationship["self"].split('/').last + Neography::Rest.delete_relationship(new_relationship[:id]) + relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + relationships.should be_nil + end + + it "returns nil if it tries to delete a relationship that does not exist" 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-2010', "met" => "college"}) + new_relationship[:id] = new_relationship["self"].split('/').last + existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id].to_i + 1000) + existing_relationship.should be_nil + end + + it "returns nil if it tries to delete a relationship that has already been deleted" 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-2010', "met" => "college"}) + new_relationship[:id] = new_relationship["self"].split('/').last + existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id]) + existing_relationship.should be_nil + existing_relationship = Neography::Rest.delete_relationship(new_relationship[:id]) + existing_relationship.should be_nil + end + end + describe "get_node_relationships" do it "can get a node's relationship" do new_node1 = Neography::Rest.create_node @@ -384,7 +422,12 @@ relationships[1].should be_nil end - + it "returns nil if there are no relationships" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + relationships = Neography::Rest.get_node_relationships(new_node1[:id]) + relationships.should be_nil + end end end \ No newline at end of file