Skip to content

Commit

Permalink
Refactor Node Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent f2b345c commit 489f46e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 45 deletions.
17 changes: 1 addition & 16 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Rest
include RelationshipIndexes
include RelationshipAutoIndexes
include NodeTraversal
include NodePaths
extend Forwardable

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

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

@cypher ||= Cypher.new(@connection)
@gremlin ||= Gremlin.new(@connection)
@extensions ||= Extensions.new(@connection)
Expand Down Expand Up @@ -102,20 +101,6 @@ def get_relationship_end_node(rel)
get_node(rel["end"])
end

# paths

def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath")
@node_paths.get(from, to, relationships, depth, algorithm)
end

def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths")
@node_paths.get_all(from, to, relationships, depth, algorithm)
end

def get_shortest_weighted_path(from, to, relationships, weight_attr = "weight", depth = 1, algorithm = "dijkstra")
@node_paths.shortest_weighted(from, to, relationships, weight_attr, depth, algorithm)
end

# cypher query

def execute_query(query, params = {}, cypher_options = nil)
Expand Down
24 changes: 8 additions & 16 deletions lib/neography/rest/node_paths.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
module Neography
class Rest
class NodePaths
extend Neography::Rest::Paths
module NodePaths
include Neography::Rest::Helpers

add_path :base, "/node/:id/path"
add_path :all, "/node/:id/paths"

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

def get(from, to, relationships, depth = 1, algorithm = "shortestPath")

def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath")
options = path_options(to, relationships, depth, algorithm)
@connection.post(base_path(:id => get_id(from)), options) || {}
@connection.post("/node/%{id}/path" % {:id => get_id(from)}, options) || {}
end

def get_all(from, to, relationships, depth = 1, algorithm = "allPaths")
def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths")
options = path_options(to, relationships, depth, algorithm)
@connection.post(all_path(:id => get_id(from)), options) || []
@connection.post("/node/%{id}/paths" % {:id => get_id(from)}, options) || []
end

def shortest_weighted(from, to, relationships, weight_attribute = "weight", depth = 1, algorithm = "dijkstra")
def get_shortest_weighted_path(from, to, relationships, weight_attribute = "weight", depth = 1, algorithm = "dijkstra")
options = path_options(to, relationships, depth, algorithm, { :cost_property => weight_attribute })
@connection.post(all_path(:id => get_id(from)), options) || {}
@connection.post("/node/%{id}/paths" % {:id => get_id(from)}, options) || {}
end

private
Expand Down
23 changes: 10 additions & 13 deletions spec/unit/rest/node_paths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,50 @@ module Neography
class Rest
describe NodePaths do

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

it "gets a shortest path between two nodes" do
expected_body = {
"to" => "http://configuration/node/43",
"to" => "http://localhost:7474/node/43",
"relationships" => "relationships",
"max_depth" => 3,
"algorithm" => "shortestPath"
}

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

subject.get("42", "43", "relationships", 3, "shortestPath")
subject.get_path("42", "43", "relationships", 3, "shortestPath")
end

it "gets all shortest paths between two nodes" do
expected_body = {
"to" => "http://configuration/node/43",
"to" => "http://localhost:7474/node/43",
"relationships" => "relationships",
"max_depth" => 3,
"algorithm" => "shortestPath"
}

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

subject.get_all("42", "43", "relationships", 3, "shortestPath")
subject.get_paths("42", "43", "relationships", 3, "shortestPath")
end

it "gets all shortest weighted paths between two nodes" do
expected_body = {
"to" => "http://configuration/node/43",
"to" => "http://localhost:7474/node/43",
"relationships" => "relationships",
"cost_property" => "cost",
"max_depth" => 3,
"algorithm" => "shortestPath"
}

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

subject.shortest_weighted("42", "43", "relationships", "cost", 3, "shortestPath")
subject.get_shortest_weighted_path("42", "43", "relationships", "cost", 3, "shortestPath")
end

context "algorithm" do

subject { NodePaths.new(nil) }

[ :shortest, "shortest", :shortestPath, "shortestPath", :short, "short" ].each do |algorithm|
it "parses shortestPath" do
subject.send(:get_algorithm, algorithm).should == "shortestPath"
Expand Down

0 comments on commit 489f46e

Please sign in to comment.