-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding get_node_relationships_to method
- Loading branch information
1 parent
5c18437
commit fd13a05
Showing
3 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Neography | ||
class Rest | ||
class OtherNodeRelationships | ||
extend Neography::Rest::Paths | ||
include Neography::Rest::Helpers | ||
|
||
add_path :base, "/node/:id/traverse/relationship" | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
end | ||
|
||
def get(id, other_id, direction = "all", types = [nil]) | ||
|
||
body = case parse_direction(direction) | ||
when "all" | ||
"position.endNode().getId() == " + get_id(other_id) | ||
when "in" | ||
"position.length() > 0 && position.lastRelationship().getStartNode().getId() == " + get_id(other_id) | ||
when "out" | ||
"position.length() > 0 && position.lastRelationship().getEndNode().getId() == " + get_id(other_id) | ||
end | ||
|
||
relationships = {:relationships => types.map{|row| Hash[{:type => row}].merge({:direction => parse_direction(direction)})} } | ||
|
||
if types.first.nil? | ||
relationships = {} | ||
end | ||
|
||
options = { | ||
:body => {:order => "breadth_first", | ||
:uniqueness => "relationship_global", | ||
:max_depth => 1, | ||
:return_filter => {:language => "javascript", | ||
:body => body } | ||
}.merge(relationships).to_json, | ||
:headers => json_content_type | ||
} | ||
#puts options.inspect | ||
node_relationships = @connection.post(base_path(:id => get_id(id)), options) || [] | ||
|
||
return nil if node_relationships.empty? | ||
node_relationships | ||
end | ||
|
||
def parse_direction(direction) | ||
case direction | ||
when :incoming, "incoming", :in, "in" | ||
"in" | ||
when :outgoing, "outgoing", :out, "out" | ||
"out" | ||
else | ||
"all" | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
require 'spec_helper' | ||
|
||
describe Neography::Rest do | ||
before(:each) do | ||
@neo = Neography::Rest.new | ||
end | ||
|
||
describe "get_relationship" do | ||
it "can get a relationship that exists" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2) | ||
existing_relationships = @neo.get_node_relationships_to(new_node1, new_node2) | ||
existing_relationships[0].should_not be_nil | ||
existing_relationships[0].should have_key("self") | ||
existing_relationships[0]["self"].should == new_relationship["self"] | ||
end | ||
|
||
it "returns nil if it tries to get a relationship that does not exist" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2) | ||
existing_relationship = @neo.get_node_relationships_to(new_node1, new_node3) | ||
existing_relationship.should be_nil | ||
end | ||
end | ||
|
||
describe "get_node_relationships" do | ||
it "can get a node's relationship" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node2) | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node1["self"] | ||
relationships[0]["end"].should == new_node2["self"] | ||
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 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
new_relationship = @neo.create_relationship("enemies", new_node1, new_node2, {"since" => '10-2-2010', "met" => "work"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node2) | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node1["self"] | ||
relationships[0]["end"].should == new_node2["self"] | ||
relationships[0]["type"].should == "friends" | ||
relationships[0]["data"]["met"].should == "college" | ||
relationships[0]["data"]["since"].should == '10-1-2005' | ||
relationships[1]["start"].should == new_node1["self"] | ||
relationships[1]["end"].should == new_node2["self"] | ||
relationships[1]["type"].should == "enemies" | ||
relationships[1]["data"]["met"].should == "work" | ||
relationships[1]["data"]["since"].should == '10-2-2010' | ||
end | ||
|
||
it "can get all of a node's outgoing relationship" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
new_relationship = @neo.create_relationship("enemies", new_node2, new_node1, {"since" => '10-2-2010', "met" => "work"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node2, "out") | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node1["self"] | ||
relationships[0]["end"].should == new_node2["self"] | ||
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 all of a node's incoming relationship" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
new_relationship = @neo.create_relationship("enemies", new_node3, new_node1, {"since" => '10-2-2010', "met" => "work"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node3, "in") | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node3["self"] | ||
relationships[0]["end"].should == new_node1["self"] | ||
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 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node3, {"since" => '10-2-2010', "met" => "work"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node2, "all", "friends") | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node1["self"] | ||
relationships[0]["end"].should == new_node2["self"] | ||
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 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
new_node3 = @neo.create_node | ||
new_node4 = @neo.create_node | ||
new_relationship = @neo.create_relationship("friends", new_node1, new_node2, {"since" => '10-1-2005', "met" => "college"}) | ||
new_relationship = @neo.create_relationship("enemies", new_node1, new_node3, {"since" => '10-2-2010', "met" => "work"}) | ||
new_relationship = @neo.create_relationship("enemies", new_node4, new_node1, {"since" => '10-3-2010', "met" => "gym"}) | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node4, "in", "enemies") | ||
relationships.should_not be_nil | ||
relationships[0]["start"].should == new_node4["self"] | ||
relationships[0]["end"].should == new_node1["self"] | ||
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 | ||
|
||
it "returns nil if there are no relationships" do | ||
new_node1 = @neo.create_node | ||
new_node2 = @neo.create_node | ||
relationships = @neo.get_node_relationships_to(new_node1, new_node2) | ||
relationships.should be_nil | ||
end | ||
end | ||
|
||
end |