Skip to content

Commit

Permalink
adding get_relationship_properties and remove_relationship_properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 18, 2010
1 parent 8245c12 commit bb3bda9
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 8 deletions.
19 changes: 12 additions & 7 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ 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(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

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

Neography::Rest.get_relationship_properties(id) # Get just the relationship properties
Neography::Rest.get_relationship_properties(id, ["since","met"]) # Get some of the relationship properties
Neography::Rest.remove_relationship_properties(id) # Remove all properties of a relationship
Neography::Rest.remove_relationship_properties(id, "since") # Remove one property of a relationship
Neography::Rest.remove_relationship_properties(id, ["since","met"]) # Remove multiple properties of a relationship

... and a work in progress more rubyish layer that's not quite ready for use yet.

Expand Down
24 changes: 24 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ def create_relationship(type, from, to, props = nil)
rescue_ij { post("/node/#{from}/relationships", options) }
end

def get_relationship_properties(id, properties = nil)
if properties.nil?
rescue_ij { get("/relationship/#{id}/properties") }
else
relationship_properties = Hash.new
properties.to_a.each do |property|
value = rescue_ij { get("/relationship/#{id}/properties/#{property}") }
relationship_properties[property] = value unless value.nil?
end
return nil if relationship_properties.empty?
relationship_properties
end
end

def remove_relationship_properties(id, properties = nil)
if properties.nil?
rescue_ij { delete("/relationship/#{id}/properties") }
else
properties.to_a.each do |property|
rescue_ij { delete("/relationship/#{id}/properties/#{property}") }
end
end
end

def delete_relationship(id)
rescue_ij { delete("/relationship/#{id}") }
end
Expand Down
113 changes: 112 additions & 1 deletion spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
end

describe "get_node_properties" do
it "can get all a node's properties" do
it "can get all of a node's properties" do
new_node = Neography::Rest.create_node("weight" => 200, "eyes" => "brown")
new_node[:id] = new_node["self"].split('/').last
node_properties = Neography::Rest.get_node_properties(new_node[:id])
Expand Down Expand Up @@ -266,6 +266,117 @@
end
end

describe "get_relationship_properties" do
it "can get all of a relationship's properties" 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
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
relationship_properties["since"].should == '10-1-2010'
relationship_properties["met"].should == "college"
end

it "can get some of a relationship's properties" 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", "roommates" => "no"})
new_relationship[:id] = new_relationship["self"].split('/').last
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "roommates"])
relationship_properties["since"].should == '10-1-2010'
relationship_properties["met"].should be_nil
relationship_properties["roommates"].should == "no"
end

it "returns nil if it gets the properties on a relationship that does not have any" 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])
new_relationship[:id] = new_relationship["self"].split('/').last
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
relationship_properties.should be_nil
end

it "returns nil if it tries to get some of the properties on a relationship that does not have any" 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])
new_relationship[:id] = new_relationship["self"].split('/').last
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "roommates"])
relationship_properties.should be_nil
end

it "returns nil if it fails to get properties on 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])
new_relationship[:id] = new_relationship["self"].split('/').last
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000)
relationship_properties.should be_nil
end
end

describe "remove_relationship_properties" do
it "can remove a relationship's properties" 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.remove_relationship_properties(new_relationship[:id])
Neography::Rest.get_relationship_properties(new_relationship[:id]).should be_nil
end

it "returns nil if it fails to remove the properties of 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
Neography::Rest.remove_relationship_properties(new_relationship[:id])
Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000).should be_nil
end

it "can remove a specific relationship property" 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.remove_relationship_properties(new_relationship[:id], "met")
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["met", "since"])
relationship_properties["met"].should be_nil
relationship_properties["since"].should == '10-1-2010'
end

it "can remove more than one relationship property" 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", "roommates" => "no"})
new_relationship[:id] = new_relationship["self"].split('/').last
Neography::Rest.remove_relationship_properties(new_relationship[:id], ["met", "since"])
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id], ["since", "met", "roommates"])
relationship_properties["met"].should be_nil
relationship_properties["since"].should be_nil
relationship_properties["roommates"].should == "no"
end
end

describe "delete_relationship" do
it "can delete an existing relationship" do
new_node1 = Neography::Rest.create_node
Expand Down

0 comments on commit bb3bda9

Please sign in to comment.