From fd13a0553c715160f7051e8541099758064ad6e4 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Fri, 12 Apr 2013 02:25:04 -0500 Subject: [PATCH] adding get_node_relationships_to method --- lib/neography/rest.rb | 8 +- .../rest/other_node_relationships.rb | 59 ++++++++ .../rest_other_node_relationship_spec.rb | 137 ++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 lib/neography/rest/other_node_relationships.rb create mode 100644 spec/integration/rest_other_node_relationship_spec.rb diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 95e6ce5..acac67f 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -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' @@ -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) @@ -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) @@ -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 diff --git a/lib/neography/rest/other_node_relationships.rb b/lib/neography/rest/other_node_relationships.rb new file mode 100644 index 0000000..a99b960 --- /dev/null +++ b/lib/neography/rest/other_node_relationships.rb @@ -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 diff --git a/spec/integration/rest_other_node_relationship_spec.rb b/spec/integration/rest_other_node_relationship_spec.rb new file mode 100644 index 0000000..fa2364f --- /dev/null +++ b/spec/integration/rest_other_node_relationship_spec.rb @@ -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