diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 0b7c7f3..4920717 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -44,6 +44,7 @@ class Rest include SchemaIndexes include Constraints include Transactions + include Nodes extend Forwardable attr_reader :connection @@ -53,7 +54,6 @@ class Rest def initialize(options = ENV['NEO4J_URL'] || {}) @connection = Connection.new(options) - @nodes ||= Nodes.new(@connection) @node_properties ||= NodeProperties.new(@connection) @node_relationships ||= NodeRelationships.new(@connection) @other_node_relationships ||= OtherNodeRelationships.new(@connection) @@ -75,38 +75,6 @@ def initialize(options = ENV['NEO4J_URL'] || {}) @spatial ||= Spatial.new(@connection) end - - - # nodes - - def get_root - @nodes.root - end - - def get_node(id) - @nodes.get(id) - end - - def get_nodes(*args) - @nodes.get_each(*args) - end - - def create_node(*args) - @nodes.create(*args) - end - - def create_nodes(args) - @nodes.create_multiple(args) - end - - def create_nodes_threaded(args) - @nodes.create_multiple_threaded(args) - end - - def delete_node(id) - @nodes.delete(id) - end - def delete_node!(id) relationships = get_node_relationships(get_id(id)) relationships.each do |relationship| diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index 56f774e..35d5c28 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -39,15 +39,15 @@ def get_batch(args) # Nodes def get_node(id) - get Nodes.base_path(:id => get_id(id)) + get "/node/%{id}" % {:id => get_id(id)} end def delete_node(id) - delete Nodes.base_path(:id => get_id(id)) + delete "/node/%{id}" % {:id => get_id(id)} end def create_node(body) - post Nodes.index_path do + post "/node" do body end end diff --git a/lib/neography/rest/nodes.rb b/lib/neography/rest/nodes.rb index 0a18764..613e16c 100644 --- a/lib/neography/rest/nodes.rb +++ b/lib/neography/rest/nodes.rb @@ -1,67 +1,59 @@ module Neography class Rest - class Nodes - extend Neography::Rest::Paths + module Nodes include Neography::Rest::Helpers - add_path :index, "/node" - add_path :base, "/node/:id" - - def initialize(connection) - @connection ||= connection - end - - def get(id) - @connection.get(base_path(:id => get_id(id))) + def get_node(id) + @connection.get("/node/%{id}" % {:id => get_id(id)}) end - def get_each(*nodes) + def get_nodes(*nodes) gotten_nodes = [] Array(nodes).flatten.each do |node| - gotten_nodes << get(node) + gotten_nodes << get_node(node) end gotten_nodes end - def root + def get_root root_node = @connection.get('/')["reference_node"] - @connection.get(base_path(:id => get_id(root_node))) + @connection.get("/node/%{id}" % {:id => get_id(root_node)}) end - def create(*args) + def create_node(*args) if args[0].respond_to?(:each_pair) && args[0] - create_with_attributes(args[0]) + create_node_with_attributes(args[0]) else - create_empty + create_empty_node end end - def create_with_attributes(attributes) + def create_node_with_attributes(attributes) options = { :body => attributes.delete_if { |k, v| v.nil? }.to_json, :headers => json_content_type } - @connection.post(index_path, options) + @connection.post("/node", options) end - def create_empty - @connection.post(index_path) + def create_empty_node + @connection.post("/node") end - def delete(id) - @connection.delete(base_path(:id => get_id(id))) + def delete_node(id) + @connection.delete("/node/%{id}" % {:id => get_id(id)}) end - def create_multiple(nodes) + def create_nodes(nodes) nodes = Array.new(nodes) if nodes.kind_of? Fixnum created_nodes = [] nodes.each do |node| - created_nodes << create(node) + created_nodes << create_node(node) end created_nodes end - def create_multiple_threaded(nodes) + def create_nodes_threaded(nodes) nodes = Array.new(nodes) if nodes.kind_of? Fixnum node_queue = Queue.new @@ -77,12 +69,12 @@ def create_multiple_threaded(nodes) until node_queue.empty? do node = node_queue.pop if node.respond_to?(:each_pair) - responses.push( @connection.post(index_path, { + responses.push( @connection.post("/node", { :body => node.to_json, :headers => json_content_type } ) ) else - responses.push( @connection.post(index_path) ) + responses.push( @connection.post("/node") ) end end self.join diff --git a/spec/unit/rest/nodes_spec.rb b/spec/unit/rest/nodes_spec.rb index 6348fae..a177957 100644 --- a/spec/unit/rest/nodes_spec.rb +++ b/spec/unit/rest/nodes_spec.rb @@ -4,35 +4,34 @@ module Neography class Rest describe Nodes do - let(:connection) { double } - subject { Nodes.new(connection) } + subject { Neography::Rest.new } context "get nodes" do it "gets single nodes" do - connection.should_receive(:get).with("/node/42") - subject.get("42") + subject.connection.should_receive(:get).with("/node/42") + subject.get_node("42") end it "gets multiple nodes" do - connection.should_receive(:get).with("/node/42") - connection.should_receive(:get).with("/node/43") - subject.get_each("42", "43") + subject.connection.should_receive(:get).with("/node/42") + subject.connection.should_receive(:get).with("/node/43") + subject.get_nodes("42", "43") end it "returns multiple nodes in an array" do - connection.stub(:get).and_return("foo", "bar") - subject.get_each("42", "43").should == [ "foo", "bar" ] + subject.connection.stub(:get).and_return("foo", "bar") + subject.get_nodes("42", "43").should == [ "foo", "bar" ] end it "gets the root node" do - connection.stub(:get).with("/").and_return({ "reference_node" => "42" }) - connection.should_receive(:get).with("/node/42") - subject.root + subject.connection.stub(:get).with("/").and_return({ "reference_node" => "42" }) + subject.connection.should_receive(:get).with("/node/42") + subject.get_root end it "returns the root node" do - connection.stub(:get).and_return({ "reference_node" => "42" }, "foo") - subject.root.should == "foo" + subject.connection.stub(:get).and_return({ "reference_node" => "42" }, "foo") + subject.get_root.should == "foo" end end @@ -43,13 +42,13 @@ class Rest :body => '{"foo":"bar","baz":"qux"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options) - subject.create_with_attributes({:foo => "bar", :baz => "qux"}) + subject.connection.should_receive(:post).with("/node", options) + subject.create_node_with_attributes({:foo => "bar", :baz => "qux"}) end it "returns the created node" do - connection.stub(:post).and_return("foo") - subject.create_with_attributes({}).should == "foo" + subject.connection.stub(:post).and_return("foo") + subject.create_node_with_attributes({}).should == "foo" end it "creates with attributes using #create method" do @@ -57,23 +56,23 @@ class Rest :body => '{"foo":"bar","baz":"qux"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options) - subject.create({:foo => "bar", :baz => "qux"}) + subject.connection.should_receive(:post).with("/node", options) + subject.create_node({:foo => "bar", :baz => "qux"}) end it "creates empty nodes" do - connection.should_receive(:post).with("/node") - subject.create_empty + subject.connection.should_receive(:post).with("/node") + subject.create_empty_node end it "returns an empty node" do - connection.stub(:post).and_return("foo") - subject.create_empty.should == "foo" + subject.connection.stub(:post).and_return("foo") + subject.create_empty_node.should == "foo" end it "creates empty nodes using #create method" do - connection.should_receive(:post).with("/node") - subject.create + subject.connection.should_receive(:post).with("/node") + subject.create_node end end @@ -81,8 +80,8 @@ class Rest context "delete nodes" do it "deletes a node" do - connection.should_receive(:delete).with("/node/42") - subject.delete("42") + subject.connection.should_receive(:delete).with("/node/42") + subject.delete_node("42") end end @@ -98,18 +97,18 @@ class Rest :body => '{"foo2":"bar2","baz2":"qux2"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options1) - connection.should_receive(:post).with("/node", options2) + subject.connection.should_receive(:post).with("/node", options1) + subject.connection.should_receive(:post).with("/node", options2) - subject.create_multiple([ + subject.create_nodes([ {:foo1 => "bar1", :baz1 => "qux1"}, {:foo2 => "bar2", :baz2 => "qux2"} ]) end it "returns multiple nodes with attributes in an array" do - connection.stub(:post).and_return("foo", "bar") - subject.create_multiple([{},{}]).should == ["foo", "bar"] + subject.connection.stub(:post).and_return("foo", "bar") + subject.create_nodes([{},{}]).should == ["foo", "bar"] end # exotic? @@ -118,23 +117,23 @@ class Rest :body => '{"foo1":"bar1","baz1":"qux1"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options1) - connection.should_receive(:post).with("/node") + subject.connection.should_receive(:post).with("/node", options1) + subject.connection.should_receive(:post).with("/node") - subject.create_multiple([ + subject.create_nodes([ {:foo1 => "bar1", :baz1 => "qux1"}, "not a hash" # ? ]) end it "creates multiple empty nodes" do - connection.should_receive(:post).with("/node").twice - subject.create_multiple(2) + subject.connection.should_receive(:post).with("/node").twice + subject.create_nodes(2) end it "returns multiple empty nodes in an array" do - connection.stub(:post).and_return("foo", "bar") - subject.create_multiple(2).should == ["foo", "bar"] + subject.connection.stub(:post).and_return("foo", "bar") + subject.create_nodes(2).should == ["foo", "bar"] end end @@ -152,10 +151,10 @@ class Rest :body => '{"foo2":"bar2","baz2":"qux2"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options1) - connection.should_receive(:post).with("/node", options2) + subject.connection.should_receive(:post).with("/node", options1) + subject.connection.should_receive(:post).with("/node", options2) - subject.create_multiple_threaded([ + subject.create_nodes_threaded([ {:foo1 => "bar1", :baz1 => "qux1"}, {:foo2 => "bar2", :baz2 => "qux2"} ]) @@ -167,18 +166,18 @@ class Rest :body => '{"foo1":"bar1","baz1":"qux1"}', :headers => json_content_type } - connection.should_receive(:post).with("/node", options1) - connection.should_receive(:post).with("/node") + subject.connection.should_receive(:post).with("/node", options1) + subject.connection.should_receive(:post).with("/node") - subject.create_multiple_threaded([ + subject.create_nodes_threaded([ {:foo1 => "bar1", :baz1 => "qux1"}, "not a hash" # ? ]) end it "creates multiple empty nodes" do - connection.should_receive(:post).with("/node").twice - subject.create_multiple_threaded(2) + subject.connection.should_receive(:post).with("/node").twice + subject.create_nodes_threaded(2) end end