-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Second step of major refactor of REST class.
- Loading branch information
Showing
11 changed files
with
628 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
module Neography | ||
class Rest | ||
class Batch | ||
include Neography::Rest::Paths | ||
include Neography::Rest::Helpers | ||
|
||
add_path :batch_path, "/batch" | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
end | ||
|
||
def execute(*args) | ||
batch({'Accept' => 'application/json;stream=true'}, *args) | ||
end | ||
|
||
def not_streaming(*args) | ||
batch({}, *args) | ||
end | ||
|
||
private | ||
|
||
def batch(accept_header, *args) | ||
batch = [] | ||
Array(args).each_with_index do |c,i| | ||
batch << {:id => i }.merge(get_batch(c)) | ||
end | ||
options = { | ||
:body => batch.to_json, | ||
:headers => json_content_type.merge(accept_header) | ||
} | ||
|
||
@connection.post(batch_path, options) | ||
end | ||
|
||
def get_batch(args) | ||
case args[0] | ||
when :get_node | ||
{:method => "GET", :to => "/node/#{get_id(args[1])}"} | ||
when :create_node | ||
{:method => "POST", :to => "/node/", :body => args[1]} | ||
when :create_unique_node | ||
{:method => "POST", :to => "/index/node/#{args[1]}?unique", :body => {:key => args[2], :value => args[3], :properties => args[4]}} | ||
when :set_node_property | ||
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first} | ||
when :reset_node_properties | ||
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties", :body => args[2]} | ||
when :get_relationship | ||
{:method => "GET", :to => "/relationship/#{get_id(args[1])}"} | ||
when :create_relationship | ||
{:method => "POST", :to => (args[2].is_a?(String) && args[2].start_with?("{") ? "" : "/node/") + "#{get_id(args[2])}/relationships", :body => {:to => (args[3].is_a?(String) && args[3].start_with?("{") ? "" : "/node/") + "#{get_id(args[3])}", :type => args[1], :data => args[4] } } | ||
when :create_unique_relationship | ||
{:method => "POST", :to => "/index/relationship/#{args[1]}?unique", :body => {:key => args[2], :value => args[3], :type => args[4], :start => (args[5].is_a?(String) && args[5].start_with?("{") ? "" : "/node/") + "#{get_id(args[5])}", :end=> (args[6].is_a?(String) && args[6].start_with?("{") ? "" : "/node/") + "#{get_id(args[6])}"} } | ||
when :delete_relationship | ||
{:method => "DELETE", :to => "/relationship/#{get_id(args[1])}"} | ||
when :set_relationship_property | ||
{:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first} | ||
when :reset_relationship_properties | ||
{:method => "PUT", :to => (args[1].is_a?(String) && args[1].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[1])}/properties", :body => args[2]} | ||
when :add_node_to_index | ||
{:method => "POST", :to => "/index/node/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/node/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } } | ||
when :add_relationship_to_index | ||
{:method => "POST", :to => "/index/relationship/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } } | ||
when :get_node_index | ||
{:method => "GET", :to => "/index/node/#{args[1]}/#{args[2]}/#{args[3]}"} | ||
when :get_relationship_index | ||
{:method => "GET", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{args[3]}"} | ||
when :get_node_relationships | ||
{:method => "GET", :to => "/node/#{get_id(args[1])}/relationships/#{args[2] || 'all'}"} | ||
when :execute_script | ||
{:method => "POST", :to => @connection.gremlin_path, :body => {:script => args[1], :params => args[2]}} | ||
when :execute_query | ||
if args[2] | ||
{:method => "POST", :to => @connection.cypher_path, :body => {:query => args[1], :params => args[2]}} | ||
else | ||
{:method => "POST", :to => @connection.cypher_path, :body => {:query => args[1]}} | ||
end | ||
when :remove_node_from_index | ||
case args.size | ||
when 5 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{args[2]}/#{args[3]}/#{get_id(args[4])}" } | ||
when 4 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{args[2]}/#{get_id(args[3])}" } | ||
when 3 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{get_id(args[2])}" } | ||
end | ||
when :remove_relationship_from_index | ||
case args.size | ||
when 5 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{args[3]}/#{get_id(args[4])}" } | ||
when 4 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{get_id(args[3])}" } | ||
when 3 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{get_id(args[2])}" } | ||
end | ||
when :delete_node | ||
{:method => "DELETE", :to => "/node/#{get_id(args[1])}"} | ||
else | ||
raise "Unknown option #{args[0]}" | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Neography | ||
class Rest | ||
class Clean | ||
include Neography::Rest::Paths | ||
include Neography::Rest::Helpers | ||
|
||
add_path :clean, "/cleandb/secret-key" | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
end | ||
|
||
|
||
def execute | ||
@connection.delete(clean) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Neography | ||
class Rest | ||
class Cypher | ||
include Neography::Rest::Helpers | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
end | ||
|
||
def query(query, parameters) | ||
options = { | ||
:body => { | ||
:query => query, | ||
:params => parameters | ||
}.to_json, | ||
:headers => json_content_type.merge({'Accept' => 'application/json;stream=true'}) | ||
} | ||
|
||
@connection.post(@connection.cypher_path, options) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Neography | ||
class Rest | ||
class Gremlin | ||
include Neography::Rest::Helpers | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
end | ||
|
||
def execute(script, parameters) | ||
options = { | ||
:body => { | ||
:script => script, | ||
:params => parameters, | ||
}.to_json, | ||
:headers => json_content_type | ||
} | ||
result = @connection.post(@connection.gremlin_path, options) | ||
result == "null" ? nil : result | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Neography | ||
class Rest | ||
class NodePaths | ||
include Neography::Rest::Paths | ||
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, algorithm) | ||
options = { :body => { | ||
"to" => @connection.configuration + "/node/#{get_id(to)}", | ||
"relationships" => relationships, | ||
"max_depth" => depth, | ||
"algorithm" => get_algorithm(algorithm) | ||
}.to_json, | ||
:headers => json_content_type | ||
} | ||
@connection.post(base(:id => get_id(from)), options) || Hash.new | ||
end | ||
|
||
def get_all(from, to, relationships, depth, algorithm) | ||
options = { :body => { | ||
"to" => @connection.configuration + "/node/#{get_id(to)}", | ||
"relationships" => relationships, | ||
"max_depth" => depth, | ||
"algorithm" => get_algorithm(algorithm) | ||
}.to_json, | ||
:headers => json_content_type | ||
} | ||
@connection.post(all(:id => get_id(from)), options) || Array.new | ||
end | ||
|
||
def shortest_weighted(from, to, relationships, weight_attribute, depth, algorithm) | ||
options = { :body => { | ||
"to" => @connection.configuration + "/node/#{get_id(to)}", | ||
"relationships" => relationships, | ||
"cost_property" => weight_attribute, | ||
"max_depth" => depth, | ||
"algorithm" => get_algorithm(algorithm) | ||
}.to_json, | ||
:headers => json_content_type | ||
} | ||
@connection.post(all(:id => get_id(from)), options) || Hash.new | ||
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 | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module Neography | ||
class Rest | ||
class NodeTraversal | ||
include Neography::Rest::Paths | ||
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"]), | ||
"relationships" => description["relationships"], | ||
"prune_evaluator" => description["prune evaluator"], | ||
"return_filter" => description["return filter"], | ||
"max_depth" => get_depth(description["depth"]) | ||
}.to_json, | ||
:headers => json_content_type | ||
} | ||
|
||
type = get_type(return_type) | ||
|
||
@connection.post(traversal(:id => get_id(id), :type => type), options) || Array.new | ||
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 | ||
|
||
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 | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.