/
node_paths.rb
49 lines (42 loc) · 1.72 KB
/
node_paths.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Neography
class Rest
module NodePaths
include Neography::Rest::Helpers
def get_path(from, to, relationships, depth = 1, algorithm = "shortestPath")
options = path_options(to, relationships, depth, algorithm)
@connection.post("/node/%{id}/path" % {:id => get_id(from)}, options) || {}
end
def get_paths(from, to, relationships, depth = 1, algorithm = "allPaths")
options = path_options(to, relationships, depth, algorithm)
@connection.post("/node/%{id}/paths" % {:id => get_id(from)}, options) || []
end
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("/node/%{id}/paths" % {:id => get_id(from)}, options) || {}
end
private
def get_algorithm(algorithm)
case algorithm
when :shortest, "shortest", :shortestPath, "shortestPath", :short, "short"
"shortestPath"
when :allSimplePaths, "allSimplePaths", :simple, "simple"
"allSimplePaths"
when :dijkstra, "dijkstra"
"dijkstra"
else
"allPaths"
end
end
def path_options(to, relationships, depth, algorithm, extra_body = {})
options = { :body => {
"to" => @connection.configuration + "/node/#{get_id(to)}",
"relationships" => relationships,
"max_depth" => depth,
"algorithm" => get_algorithm(algorithm)
}.merge(extra_body).to_json,
:headers => json_content_type
}
end
end
end
end