diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index ae319c8..a83d457 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -47,6 +47,7 @@ class Rest include NodeProperties include Relationships include RelationshipProperties + include NodeRelationships extend Forwardable attr_reader :connection @@ -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) @@ -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 diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index dbf36e5..131828e 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -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 diff --git a/lib/neography/rest/node_relationships.rb b/lib/neography/rest/node_relationships.rb index ee088dd..dc88fbe 100644 --- a/lib/neography/rest/node_relationships.rb +++ b/lib/neography/rest/node_relationships.rb @@ -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)}", @@ -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? diff --git a/spec/unit/rest/helpers_spec.rb b/spec/unit/rest/helpers_spec.rb new file mode 100644 index 0000000..72d28f5 --- /dev/null +++ b/spec/unit/rest/helpers_spec.rb @@ -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 diff --git a/spec/unit/rest/node_relationships_spec.rb b/spec/unit/rest/node_relationships_spec.rb index d11d700..c9d1a56 100644 --- a/spec/unit/rest/node_relationships_spec.rb +++ b/spec/unit/rest/node_relationships_spec.rb @@ -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