Skip to content

Commit

Permalink
Refactor Node Traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 92dcccd commit f2b345c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 167 deletions.
8 changes: 1 addition & 7 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Rest
include NodeAutoIndexes
include RelationshipIndexes
include RelationshipAutoIndexes
include NodeTraversal
extend Forwardable

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

@node_traversal ||= NodeTraversal.new(@connection)
@node_paths ||= NodePaths.new(@connection)

@cypher ||= Cypher.new(@connection)
Expand Down Expand Up @@ -102,12 +102,6 @@ def get_relationship_end_node(rel)
get_node(rel["end"])
end

# traversal

def traverse(id, return_type, description)
@node_traversal.traverse(id, return_type, description)
end

# paths

def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath")
Expand Down
47 changes: 47 additions & 0 deletions lib/neography/rest/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,53 @@ def escape(value)
encode(value.to_s)
end
end

def parse_order(order)
case order
when :breadth, "breadth", "breadth first", "breadthFirst", :wide, "wide"
"breadth first"
else
"depth first"
end
end

def parse_uniqueness(uniqueness)
case uniqueness
when :nodeglobal, "node global", "nodeglobal", "node_global"
"node global"
when :nodepath, "node path", "nodepath", "node_path"
"node path"
when :noderecent, "node recent", "noderecent", "node_recent"
"node recent"
when :relationshipglobal, "relationship global", "relationshipglobal", "relationship_global"
"relationship global"
when :relationshippath, "relationship path", "relationshippath", "relationship_path"
"relationship path"
when :relationshiprecent, "relationship recent", "relationshiprecent", "relationship_recent"
"relationship recent"
else
"none"
end
end

def parse_depth(depth)
return nil if depth.nil?
return 1 if depth.to_i == 0
depth.to_i
end

def parse_type(type)
case type
when :relationship, "relationship", :relationships, "relationships"
"relationship"
when :path, "path", :paths, "paths"
"path"
when :fullpath, "fullpath", :fullpaths, "fullpaths"
"fullpath"
else
"node"
end
end

end
end
Expand Down
70 changes: 7 additions & 63 deletions lib/neography/rest/node_traversal.rb
Original file line number Diff line number Diff line change
@@ -1,79 +1,23 @@
module Neography
class Rest
class NodeTraversal
extend Neography::Rest::Paths
module NodeTraversal
include Neography::Rest::Helpers

add_path :traversal, "/node/:id/traverse/:type"

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


def traverse(id, return_type, description)
options = { :body => {
"order" => get_order(description["order"]),
"uniqueness" => get_uniqueness(description["uniqueness"]),
"order" => parse_order(description["order"]),
"uniqueness" => parse_uniqueness(description["uniqueness"]),
"relationships" => description["relationships"],
"prune_evaluator" => description["prune evaluator"],
"return_filter" => description["return filter"],
"max_depth" => get_depth(description["depth"])
"max_depth" => parse_depth(description["depth"])
}.to_json,
:headers => json_content_type
}

type = get_type(return_type)

@connection.post(traversal_path(:id => get_id(id), :type => type), options) || []
end

private

def get_order(order)
case order
when :breadth, "breadth", "breadth first", "breadthFirst", :wide, "wide"
"breadth first"
else
"depth first"
end
end

def get_uniqueness(uniqueness)
case uniqueness
when :nodeglobal, "node global", "nodeglobal", "node_global"
"node global"
when :nodepath, "node path", "nodepath", "node_path"
"node path"
when :noderecent, "node recent", "noderecent", "node_recent"
"node recent"
when :relationshipglobal, "relationship global", "relationshipglobal", "relationship_global"
"relationship global"
when :relationshippath, "relationship path", "relationshippath", "relationship_path"
"relationship path"
when :relationshiprecent, "relationship recent", "relationshiprecent", "relationship_recent"
"relationship recent"
else
"none"
end
end

def get_depth(depth)
return nil if depth.nil?
return 1 if depth.to_i == 0
depth.to_i
end
type = parse_type(return_type)

def get_type(type)
case type
when :relationship, "relationship", :relationships, "relationships"
"relationship"
when :path, "path", :paths, "paths"
"path"
when :fullpath, "fullpath", :fullpaths, "fullpaths"
"fullpath"
else
"node"
end
@connection.post("/node/%{id}/traverse/%{type}" % {:id => get_id(id), :type => type}, options) || []
end

end
Expand Down
92 changes: 92 additions & 0 deletions spec/unit/rest/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,98 @@ class Rest
end

end

context "options" do
let(:traversal) { NodeTraversal.new(nil) }

context "order" do
[ :breadth, "breadth", "breadth first", "breadthFirst", :wide, "wide" ].each do |order|
it "parses breadth first" do
subject.send(:parse_order, order).should == "breadth first"
end
end

it "parses depth first by default" do
subject.send(:parse_order, "foo").should == "depth first"
end
end

context "uniqueness" do
[ :nodeglobal, "node global", "nodeglobal", "node_global" ].each do |order|
it "parses node global" do
subject.send(:parse_uniqueness, order).should == "node global"
end
end

[ :nodepath, "node path", "nodepath", "node_path" ].each do |order|
it "parses node path" do
subject.send(:parse_uniqueness, order).should == "node path"
end
end

[ :noderecent, "node recent", "noderecent", "node_recent" ].each do |order|
it "parses node recent" do
subject.send(:parse_uniqueness, order).should == "node recent"
end
end

[ :relationshipglobal, "relationship global", "relationshipglobal", "relationship_global" ].each do |order|
it "parses relationship global" do
subject.send(:parse_uniqueness, order).should == "relationship global"
end
end

[ :relationshippath, "relationship path", "relationshippath", "relationship_path" ].each do |order|
it "parses relationship path" do
subject.send(:parse_uniqueness, order).should == "relationship path"
end
end

[ :relationshiprecent, "relationship recent", "relationshiprecent", "relationship_recent" ].each do |order|
it "parses relationship recent" do
subject.send(:parse_uniqueness, order).should == "relationship recent"
end
end

it "parses none by default" do
subject.send(:parse_uniqueness, "foo").should == "none"
end
end

context "depth" do
it "parses nil as nil" do
subject.send(:parse_depth, nil).should be_nil
end
it "parses 0 as 1" do
subject.send(:parse_depth, "0").should == 1
end
it "parses integers" do
subject.send(:parse_depth, "42").should == 42
end
end

context "type" do
[ :relationship, "relationship", :relationships, "relationships" ].each do |type|
it "parses relationship" do
subject.send(:parse_type, type).should == "relationship"
end
end
[ :path, "path", :paths, "paths" ].each do |type|
it "parses path" do
subject.send(:parse_type, type).should == "path"
end
end
[ :fullpath, "fullpath", :fullpaths, "fullpaths" ].each do |type|
it "parses fullpath" do
subject.send(:parse_type, type).should == "fullpath"
end
end

it "parses node by default" do
subject.send(:parse_type, "foo").should == "node"
end
end
end


end
Expand Down
101 changes: 4 additions & 97 deletions spec/unit/rest/node_traversal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ module Neography
class Rest
describe NodeTraversal do

let(:connection) { double }
subject { NodeTraversal.new(connection) }

subject { Neography::Rest.new }

it "traverses" do
description = {
"order" => :breadth,
Expand All @@ -26,102 +25,10 @@ class Rest
"max_depth" => 4
}

connection.should_receive(:post).with("/node/42/traverse/relationship", json_match(:body, expected_body))
subject.connection.should_receive(:post).with("/node/42/traverse/relationship", json_match(:body, expected_body))

subject.traverse("42", :relationship, description)
end

context "options" do
let(:traversal) { NodeTraversal.new(nil) }

context "order" do
[ :breadth, "breadth", "breadth first", "breadthFirst", :wide, "wide" ].each do |order|
it "parses breadth first" do
subject.send(:get_order, order).should == "breadth first"
end
end

it "parses depth first by default" do
subject.send(:get_order, "foo").should == "depth first"
end
end

context "uniqueness" do
[ :nodeglobal, "node global", "nodeglobal", "node_global" ].each do |order|
it "parses node global" do
subject.send(:get_uniqueness, order).should == "node global"
end
end

[ :nodepath, "node path", "nodepath", "node_path" ].each do |order|
it "parses node path" do
subject.send(:get_uniqueness, order).should == "node path"
end
end

[ :noderecent, "node recent", "noderecent", "node_recent" ].each do |order|
it "parses node recent" do
subject.send(:get_uniqueness, order).should == "node recent"
end
end

[ :relationshipglobal, "relationship global", "relationshipglobal", "relationship_global" ].each do |order|
it "parses relationship global" do
subject.send(:get_uniqueness, order).should == "relationship global"
end
end

[ :relationshippath, "relationship path", "relationshippath", "relationship_path" ].each do |order|
it "parses relationship path" do
subject.send(:get_uniqueness, order).should == "relationship path"
end
end

[ :relationshiprecent, "relationship recent", "relationshiprecent", "relationship_recent" ].each do |order|
it "parses relationship recent" do
subject.send(:get_uniqueness, order).should == "relationship recent"
end
end

it "parses none by default" do
subject.send(:get_uniqueness, "foo").should == "none"
end
end

context "depth" do
it "parses nil as nil" do
subject.send(:get_depth, nil).should be_nil
end
it "parses 0 as 1" do
subject.send(:get_depth, "0").should == 1
end
it "parses integers" do
subject.send(:get_depth, "42").should == 42
end
end

context "type" do
[ :relationship, "relationship", :relationships, "relationships" ].each do |type|
it "parses relationship" do
subject.send(:get_type, type).should == "relationship"
end
end
[ :path, "path", :paths, "paths" ].each do |type|
it "parses path" do
subject.send(:get_type, type).should == "path"
end
end
[ :fullpath, "fullpath", :fullpaths, "fullpaths" ].each do |type|
it "parses fullpath" do
subject.send(:get_type, type).should == "fullpath"
end
end

it "parses node by default" do
subject.send(:get_type, "foo").should == "node"
end
end
end
end

end
end
Expand Down

0 comments on commit f2b345c

Please sign in to comment.