Skip to content

Commit

Permalink
added delete_node (and its relationship) and delete_relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 18, 2010
1 parent d352e6a commit 8245c12
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
12 changes: 7 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
22 changes: 17 additions & 5 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
45 changes: 44 additions & 1 deletion spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 8245c12

Please sign in to comment.