Skip to content

Commit

Permalink
Refactor Node Relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 6afc11a commit 901f6ec
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 69 deletions.
12 changes: 1 addition & 11 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Rest
include NodeProperties
include Relationships
include RelationshipProperties
include NodeRelationships
extend Forwardable

attr_reader :connection
Expand All @@ -56,7 +57,6 @@ class Rest
def initialize(options = ENV['NEO4J_URL'] || {})
@connection = Connection.new(options)

@node_relationships ||= NodeRelationships.new(@connection)
@other_node_relationships ||= OtherNodeRelationships.new(@connection)
@node_indexes ||= NodeIndexes.new(@connection)
@node_auto_indexes ||= NodeAutoIndexes.new(@connection)
Expand Down Expand Up @@ -100,20 +100,10 @@ def get_relationship_end_node(rel)
get_node(rel["end"])
end


# node relationships

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

# node indexes

Expand Down
4 changes: 2 additions & 2 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def add_label(id, body)

def get_node_relationships(id, direction = nil, types = nil)
if types.nil?
get NodeRelationships.direction_path(:id => get_id(id), :direction => direction || 'all')
get "/node/%{id}/relationships/%{direction}" % {:id => get_id(id), :direction => direction || 'all'}
else
get NodeRelationships.type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))
get "/node/%{id}/relationships/%{direction}/%{types}" % {:id => get_id(id), :direction => direction, :types => Array(types).join('&')}
end
end

Expand Down
25 changes: 8 additions & 17 deletions lib/neography/rest/node_relationships.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
module Neography
class Rest
class NodeRelationships
extend Neography::Rest::Paths
module NodeRelationships
include Neography::Rest::Helpers

add_path :base, "/node/:id/relationships"
add_path :direction, "/node/:id/relationships/:direction"
add_path :type, "/node/:id/relationships/:direction/:types"

def initialize(connection)
@connection ||= connection
end

def create(type, from, to, properties = nil)

def create_relationship(type, from, to, properties = nil)
options = {
:body => {
:to => @connection.configuration + "/node/#{get_id(to)}",
Expand All @@ -21,16 +12,16 @@ def create(type, from, to, properties = nil)
}.to_json,
:headers => json_content_type }

@connection.post(base_path(:id => get_id(from)), options)
@connection.post("/node/%{id}/relationships" % {:id => get_id(from)}, options)
end

def get(id, direction = nil, types = nil)
def get_node_relationships(id, direction = nil, types = nil)
direction = parse_direction(direction)

if types.nil?
node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || []
node_relationships = @connection.get("/node/%{id}/relationships/%{direction}" % {:id => get_id(id), :direction => direction}) || []
else
node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || []
node_relationships = @connection.get("/node/%{id}/relationships/%{direction}/%{types}" % {:id => get_id(id), :direction => direction, :types => encode(Array(types).join('&'))}) || []
end

return [] if node_relationships.empty?
Expand Down
32 changes: 32 additions & 0 deletions spec/unit/rest/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

module Neography
class Rest
describe Helpers do

subject { Neography::Rest.new }

context "directions" do

[ :incoming, "incoming", :in, "in" ].each do |direction|
it "parses 'in' direction" do
subject.parse_direction(direction).should == "in"
end
end

[ :outgoing, "outgoing", :out, "out" ].each do |direction|
it "parses 'out' direction" do
subject.parse_direction(direction).should == "out"
end
end

it "parses 'all' direction by default" do
subject.parse_direction("foo").should == "all"
end

end


end
end
end
57 changes: 18 additions & 39 deletions spec/unit/rest/node_relationships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,52 @@ module Neography
class Rest
describe NodeRelationships do

let(:connection) { double(:configuration => "http://configuration") }
subject { NodeRelationships.new(connection) }
subject { Neography::Rest.new }

it "creates a relationship" do
body_hash = { "type" => "some_type",
"to" => "http://configuration/node/43",
"to" => "http://localhost:7474/node/43",
"data" => {"foo"=>"bar","baz"=>"qux"}
}
connection.should_receive(:post).with("/node/42/relationships", json_match(:body, body_hash))
subject.connection.should_receive(:post).with("/node/42/relationships", json_match(:body, body_hash))

subject.create("some_type", "42", "43", {:foo => "bar", :baz => "qux"})
subject.create_relationship("some_type", "42", "43", {:foo => "bar", :baz => "qux"})
end

it "returns the post results" do
connection.stub(:post).and_return("foo")
subject.connection.stub(:post).and_return("foo")

subject.create("some_type", "42", "43", {}).should == "foo"
subject.create_relationship("some_type", "42", "43", {}).should == "foo"
end

it "gets relationships" do
connection.should_receive(:get).with("/node/42/relationships/all")
subject.get("42")
subject.connection.should_receive(:get).with("/node/42/relationships/all")
subject.get_node_relationships("42")
end

it "gets relationships with direction" do
connection.should_receive(:get).with("/node/42/relationships/in")
subject.get("42", :in)
subject.connection.should_receive(:get).with("/node/42/relationships/in")
subject.get_node_relationships("42", :in)
end

it "gets relationships with direction and type" do
connection.should_receive(:get).with("/node/42/relationships/in/foo")
subject.get("42", :in, "foo")
subject.connection.should_receive(:get).with("/node/42/relationships/in/foo")
subject.get_node_relationships("42", :in, "foo")
end

it "gets relationships with direction and types" do
connection.should_receive(:get).with("/node/42/relationships/in/foo%26bar")
subject.get("42", :in, ["foo", "bar"])
subject.connection.should_receive(:get).with("/node/42/relationships/in/foo%26bar")
subject.get_node_relationships("42", :in, ["foo", "bar"])
end

it "returns empty array if no relationships were found" do
connection.stub(:get).and_return([])
subject.get("42", :in).should be_empty
subject.connection.stub(:get).and_return([])
subject.get_node_relationships("42", :in).should be_empty
end

it "returns empty array if no relationships were found by type" do
connection.stub(:get).and_return([])
subject.get("42", :in, "foo")
end

context "directions" do

[ :incoming, "incoming", :in, "in" ].each do |direction|
it "parses 'in' direction" do
NodeRelationships.new(nil).parse_direction(direction).should == "in"
end
end

[ :outgoing, "outgoing", :out, "out" ].each do |direction|
it "parses 'out' direction" do
NodeRelationships.new(nil).parse_direction(direction).should == "out"
end
end

it "parses 'all' direction by default" do
NodeRelationships.new(nil).parse_direction("foo").should == "all"
end

subject.connection.stub(:get).and_return([])
subject.get_node_relationships("42", :in, "foo")
end

end
Expand Down

0 comments on commit 901f6ec

Please sign in to comment.