Skip to content

Commit

Permalink
adding get_node_relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 17, 2010
1 parent 081d8e0 commit d352e6a
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ 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.reset_node_properties(id, {"age" => 31}) # Reset a node's properties
Neography::Rest.set_node_properties(id, {"weight" => 200}) # Set a node's properties
Neography::Rest.get_node_properties(id) # Get just the node properties
Neography::Rest.get_node_properties(id, ["weight","age"]) # Get some of the node properties
Neography::Rest.remove_node_properties(id) # Remove all properties of a node
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.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


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

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

def get_node_relationships(id, dir=nil, types=nil)
case dir
when :incoming, "incoming"
dir = "in"
when :outgoing, "outgoing"
dir = "out"
else
dir = "all"
end

if types.nil?
rescue_ij { get("/node/#{id}/relationships/#{dir}") }
else
rescue_ij { get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") }
end
end

private

# Rescue from Invalid JSON error thrown by Crack Gem
Expand Down
121 changes: 121 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,125 @@
end
end

describe "get_node_relationships" do
it "can get a node's 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-2005', "met" => "college"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id])
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node1[:id]
relationships[0]["end"].split('/').last.should == new_node2[:id]
relationships[0]["type"].should == "friends"
relationships[0]["data"]["met"].should == "college"
relationships[0]["data"]["since"].should == '10-1-2005'
end

it "can get a node's multiple relationships" 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_node3 = Neography::Rest.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id])
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node1[:id]
relationships[0]["end"].split('/').last.should == new_node2[:id]
relationships[0]["type"].should == "friends"
relationships[0]["data"]["met"].should == "college"
relationships[0]["data"]["since"].should == '10-1-2005'
relationships[1]["start"].split('/').last.should == new_node1[:id]
relationships[1]["end"].split('/').last.should == new_node3[:id]
relationships[1]["type"].should == "enemies"
relationships[1]["data"]["met"].should == "work"
relationships[1]["data"]["since"].should == '10-2-2010'
end

it "can get a node's outgoing 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_node3 = Neography::Rest.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "outgoing")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node1[:id]
relationships[0]["end"].split('/').last.should == new_node2[:id]
relationships[0]["type"].should == "friends"
relationships[0]["data"]["met"].should == "college"
relationships[0]["data"]["since"].should == '10-1-2005'
relationships[1].should be_nil
end

it "can get a node's incoming 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_node3 = Neography::Rest.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node3[:id]
relationships[0]["end"].split('/').last.should == new_node1[:id]
relationships[0]["type"].should == "enemies"
relationships[0]["data"]["met"].should == "work"
relationships[0]["data"]["since"].should == '10-2-2010'
relationships[1].should be_nil
end

it "can get a specific type of node relationships" 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_node3 = Neography::Rest.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "all", "friends")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node1[:id]
relationships[0]["end"].split('/').last.should == new_node2[:id]
relationships[0]["type"].should == "friends"
relationships[0]["data"]["met"].should == "college"
relationships[0]["data"]["since"].should == '10-1-2005'
relationships[1].should be_nil
end

it "can get a specific type and direction of a node relationships" 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_node3 = Neography::Rest.create_node
new_node3[:id] = new_node3["self"].split('/').last
new_node4 = Neography::Rest.create_node
new_node4[:id] = new_node4["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node4[:id], new_node1[:id], {"since" => '10-3-2010', "met" => "gym"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming", "enemies")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node4[:id]
relationships[0]["end"].split('/').last.should == new_node1[:id]
relationships[0]["type"].should == "enemies"
relationships[0]["data"]["met"].should == "gym"
relationships[0]["data"]["since"].should == '10-3-2010'
relationships[1].should be_nil
end


end

end

0 comments on commit d352e6a

Please sign in to comment.