Skip to content

Commit

Permalink
adding get_node_relationships_to method
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 12, 2013
1 parent 5c18437 commit fd13a05
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'neography/rest/nodes'
require 'neography/rest/node_properties'
require 'neography/rest/node_relationships'
require 'neography/rest/other_node_relationships'
require 'neography/rest/node_indexes'
require 'neography/rest/node_auto_indexes'
require 'neography/rest/node_traversal'
Expand Down Expand Up @@ -44,6 +45,7 @@ def initialize(options = ENV['NEO4J_URL'] || {})
@nodes = Nodes.new(@connection)
@node_properties = NodeProperties.new(@connection)
@node_relationships = NodeRelationships.new(@connection)
@other_node_relationships = OtherNodeRelationships.new(@connection)
@node_indexes = NodeIndexes.new(@connection)
@node_auto_indexes = NodeAutoIndexes.new(@connection)
@node_traversal = NodeTraversal.new(@connection)
Expand Down Expand Up @@ -142,7 +144,7 @@ def get_relationship_start_node(rel)
def get_relationship_end_node(rel)
get_node(rel["end"])
end

# relationship properties

def get_relationship_properties(id, *properties)
Expand All @@ -167,6 +169,10 @@ def get_node_relationships(id, dir = nil, types = nil)
@node_relationships.get(id, dir, types)
end

def get_node_relationships_to(id, other_id, dir = "all", types = nil)
@other_node_relationships.get(id, other_id, dir, Array(types || [nil]))
end

def create_relationship(type, from, to, props = nil)
@node_relationships.create(type, from, to, props)
end
Expand Down
59 changes: 59 additions & 0 deletions lib/neography/rest/other_node_relationships.rb
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
137 changes: 137 additions & 0 deletions spec/integration/rest_other_node_relationship_spec.rb
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

0 comments on commit fd13a05

Please sign in to comment.