Skip to content

Commit 2b93d8e

Browse files
committed
adding set_relationship_properties and reset_relationship_properties
1 parent bb3bda9 commit 2b93d8e

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.rdoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and
5353
Neography::Rest.get_node_relationships(id, "incoming", "enemies") # Get only incoming relationships of type enemies
5454
Neography::Rest.delete_relationship(id) # Delete a relationship
5555

56+
Neography::Rest.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties
57+
Neography::Rest.set_relationship_properties(id, {"weight" => 200}) # Set a relationship's properties
5658
Neography::Rest.get_relationship_properties(id) # Get just the relationship properties
5759
Neography::Rest.get_relationship_properties(id, ["since","met"]) # Get some of the relationship properties
5860
Neography::Rest.remove_relationship_properties(id) # Remove all properties of a relationship

lib/neography/rest.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def create_relationship(type, from, to, props = nil)
6868
rescue_ij { post("/node/#{from}/relationships", options) }
6969
end
7070

71+
def reset_relationship_properties(id, properties)
72+
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
73+
rescue_ij { put("/relationship/#{id}/properties", options) }
74+
end
75+
7176
def get_relationship_properties(id, properties = nil)
7277
if properties.nil?
7378
rescue_ij { get("/relationship/#{id}/properties") }
@@ -92,6 +97,13 @@ def remove_relationship_properties(id, properties = nil)
9297
end
9398
end
9499

100+
def set_relationship_properties(id, properties)
101+
properties.each do |key, value|
102+
options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} }
103+
rescue_ij { put("/relationship/#{id}/properties/#{key}", options) }
104+
end
105+
end
106+
95107
def delete_relationship(id)
96108
rescue_ij { delete("/relationship/#{id}") }
97109
end

spec/integration/rest_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,64 @@
266266
end
267267
end
268268

269+
describe "set_relationship_properties" do
270+
it "can set a relationship's properties" do
271+
new_node1 = Neography::Rest.create_node
272+
new_node1[:id] = new_node1["self"].split('/').last
273+
new_node2 = Neography::Rest.create_node
274+
new_node2[:id] = new_node2["self"].split('/').last
275+
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id])
276+
new_relationship[:id] = new_relationship["self"].split('/').last
277+
Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"})
278+
Neography::Rest.set_relationship_properties(new_relationship[:id], {"roommates" => "no"})
279+
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
280+
relationship_properties["since"].should == '10-1-2010'
281+
relationship_properties["met"].should == "college"
282+
relationship_properties["roommates"].should == "no"
283+
end
284+
285+
it "it fails to set properties on a relationship that does not exist" do
286+
new_node1 = Neography::Rest.create_node
287+
new_node1[:id] = new_node1["self"].split('/').last
288+
new_node2 = Neography::Rest.create_node
289+
new_node2[:id] = new_node2["self"].split('/').last
290+
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id])
291+
new_relationship[:id] = new_relationship["self"].split('/').last
292+
Neography::Rest.set_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"})
293+
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000)
294+
relationship_properties.should be_nil
295+
end
296+
end
297+
298+
describe "reset_relationship_properties" do
299+
it "can reset a relationship's properties" do
300+
new_node1 = Neography::Rest.create_node
301+
new_node1[:id] = new_node1["self"].split('/').last
302+
new_node2 = Neography::Rest.create_node
303+
new_node2[:id] = new_node2["self"].split('/').last
304+
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id])
305+
new_relationship[:id] = new_relationship["self"].split('/').last
306+
Neography::Rest.set_relationship_properties(new_relationship[:id], {"since" => '10-1-2010', "met" => "college"})
307+
Neography::Rest.reset_relationship_properties(new_relationship[:id], {"roommates" => "no"})
308+
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id])
309+
relationship_properties["since"].should be_nil
310+
relationship_properties["met"].should be_nil
311+
relationship_properties["roommates"].should == "no"
312+
end
313+
314+
it "it fails to reset properties on a relationship that does not exist" do
315+
new_node1 = Neography::Rest.create_node
316+
new_node1[:id] = new_node1["self"].split('/').last
317+
new_node2 = Neography::Rest.create_node
318+
new_node2[:id] = new_node2["self"].split('/').last
319+
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id])
320+
new_relationship[:id] = new_relationship["self"].split('/').last
321+
Neography::Rest.reset_relationship_properties(new_relationship[:id].to_i + 10000, {"since" => '10-1-2010', "met" => "college"})
322+
relationship_properties = Neography::Rest.get_relationship_properties(new_relationship[:id].to_i + 10000)
323+
relationship_properties.should be_nil
324+
end
325+
end
326+
269327
describe "get_relationship_properties" do
270328
it "can get all of a relationship's properties" do
271329
new_node1 = Neography::Rest.create_node

0 commit comments

Comments
 (0)