Skip to content

Commit

Permalink
Second step of major refactor of REST class.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 8, 2012
1 parent db2f1dd commit b8f1312
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 287 deletions.
8 changes: 8 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ def find_and_require_user_defined_code
require 'neography/rest/node_relationships'
require 'neography/rest/node_indexes'
require 'neography/rest/node_auto_indexes'
require 'neography/rest/node_traversal'
require 'neography/rest/node_paths'
require 'neography/rest/relationships'
require 'neography/rest/relationship_properties'
require 'neography/rest/relationship_indexes'
require 'neography/rest/relationship_auto_indexes'
require 'neography/rest/cypher'
require 'neography/rest/gremlin'
require 'neography/rest/batch'
require 'neography/rest/clean'
require 'neography/connection'
require 'neography/rest'

Expand Down
413 changes: 134 additions & 279 deletions lib/neography/rest.rb

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions lib/neography/rest/batch.rb
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
20 changes: 20 additions & 0 deletions lib/neography/rest/clean.rb
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
24 changes: 24 additions & 0 deletions lib/neography/rest/cypher.rb
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
24 changes: 24 additions & 0 deletions lib/neography/rest/gremlin.rb
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
16 changes: 8 additions & 8 deletions lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_auto(type, provider)
create("node_auto_index", type, provider)
end

def create_unique_node(index, key, value, props)
def create_unique(index, key, value, props)
options = {
:body => (
{ :properties => props,
Expand All @@ -55,7 +55,7 @@ def create_unique_node(index, key, value, props)
@connection.post(unique(:index => index), options)
end

def add_node(index, key, value, id)
def add(index, key, value, id)
options = {
:body => (
{ :uri => @connection.configuration + "/node/#{get_id(id)}",
Expand All @@ -68,30 +68,30 @@ def add_node(index, key, value, id)
@connection.post(base(:index => index), options)
end

def get_node(index, key, value)
def get(index, key, value)
index = @connection.get(key_value(:index => index, :key => key, :value => value)) || Array.new
return nil if index.empty?
index
end

# TODO FIX BUG %20
def find_node_by_value(index, key, value)
def find_by_value(index, key, value)
@connection.get(key_value2(:index => index, :key => key, :value => value)) || Array.new
end

def find_node_by_query(index, query)
def find_by_query(index, query)
@connection.get(query(:index => index, :query => query)) || Array.new
end

def remove_node(index, id)
def remove(index, id)
@connection.delete(node(:index => index, :id => get_id(id)))
end

def remove_node_by_key(index, id, key)
def remove_by_key(index, id, key)
@connection.delete(key(:index => index, :id => get_id(id), :key => key))
end

def remove_node_by_value(index, id, key, value)
def remove_by_value(index, id, key, value)
@connection.delete(value(:index => index, :id => get_id(id), :key => key, :value => value))
end

Expand Down
68 changes: 68 additions & 0 deletions lib/neography/rest/node_paths.rb
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
81 changes: 81 additions & 0 deletions lib/neography/rest/node_traversal.rb
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
Loading

0 comments on commit b8f1312

Please sign in to comment.