Skip to content

Commit

Permalink
adding set_relationship_properties and reset_relationship_properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 18, 2010
1 parent bb3bda9 commit 2b93d8e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and
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.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties
Neography::Rest.set_relationship_properties(id, {"weight" => 200}) # Set a relationship's properties
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
Expand Down
12 changes: 12 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def create_relationship(type, from, to, props = nil)
rescue_ij { post("/node/#{from}/relationships", options) }
end

def reset_relationship_properties(id, properties)
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
rescue_ij { put("/relationship/#{id}/properties", options) }
end

def get_relationship_properties(id, properties = nil)
if properties.nil?
rescue_ij { get("/relationship/#{id}/properties") }
Expand All @@ -92,6 +97,13 @@ def remove_relationship_properties(id, properties = nil)
end
end

def set_relationship_properties(id, properties)
properties.each do |key, value|
options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} }
rescue_ij { put("/relationship/#{id}/properties/#{key}", options) }
end
end

def delete_relationship(id)
rescue_ij { delete("/relationship/#{id}") }
end
Expand Down
58 changes: 58 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,64 @@
end
end

describe "set_relationship_properties" do
it "can set 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])
new_relationship[:id] = new_relationship["self"].split('/').last
Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"})
Neography::Rest.set_relationship_properties(new_relationship[:id], {"roommates" => "no"})
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
relationship_properties["since"].should == '10-1-2010'
relationship_properties["met"].should == "college"
relationship_properties["roommates"].should == "no"
end

it "it fails to set 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
Neography::Rest.set_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"})
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000)
relationship_properties.should be_nil
end
end

describe "reset_relationship_properties" do
it "can reset 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])
new_relationship[:id] = new_relationship["self"].split('/').last
Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"})
Neography::Rest.reset_relationship_properties(new_relationship[:id], {"roommates" => "no"})
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
relationship_properties["since"].should be_nil
relationship_properties["met"].should be_nil
relationship_properties["roommates"].should == "no"
end

it "it fails to reset 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
Neography::Rest.reset_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"})
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000)
relationship_properties.should be_nil
end
end

describe "get_relationship_properties" do
it "can get all of a relationship's properties" do
new_node1 = Neography::Rest.create_node
Expand Down

0 comments on commit 2b93d8e

Please sign in to comment.