diff --git a/README.rdoc b/README.rdoc index c67d5e3..6d5459d 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 99ad13e..edb70a4 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -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") } @@ -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 diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index 1684090..c58790a 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -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