Skip to content

Commit

Permalink
Refactoring Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 385282d commit 7bac0c7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 113 deletions.
34 changes: 1 addition & 33 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Rest
include SchemaIndexes
include Constraints
include Transactions
include Nodes
extend Forwardable

attr_reader :connection
Expand All @@ -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)
Expand All @@ -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|
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 21 additions & 29 deletions lib/neography/rest/nodes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
95 changes: 47 additions & 48 deletions spec/unit/rest/nodes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -43,46 +42,46 @@ 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
options = {
: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

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
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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"}
])
Expand All @@ -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
Expand Down

0 comments on commit 7bac0c7

Please sign in to comment.