Skip to content

Commit

Permalink
Merge branch 'rest_refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dijk committed Sep 22, 2012
2 parents b323fa4 + 5bb7d01 commit b4834e5
Show file tree
Hide file tree
Showing 16 changed files with 538 additions and 99 deletions.
281 changes: 222 additions & 59 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Batch
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :batch, "/batch"
Expand All @@ -22,7 +22,7 @@ def not_streaming(*args)

def batch(accept_header, *args)
batch = []
Array(args).each_with_index do |c,i|
Array(args).each_with_index do |c, i|
batch << {:id => i }.merge(get_batch(c))
end
options = {
Expand All @@ -34,63 +34,226 @@ def batch(accept_header, *args)
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]}"
if respond_to?(args[0].to_sym, true)
send(args[0].to_sym, *args[1..-1])
else
raise "Unknown option #{args[0]}"
end
end

# Nodes

def get_node(id)
get Nodes.base_path(:id => get_id(id))
end

def delete_node(id)
delete Nodes.base_path(:id => get_id(id))
end

def create_node(body)
post Nodes.index_path do
body
end
end

# NodeIndexes

def create_unique_node(index, key, value, properties)
post NodeIndexes.unique_path(:index => index) do
{
:key => key,
:value => value,
:properties => properties
}
end
end

def add_node_to_index(index, key, value, id)
post NodeIndexes.base_path(:index => index) do
{
:uri => build_node_uri(id),
:key => key,
:value => value
}
end
end

def get_node_index(index, key, value)
get NodeIndexes.key_value_path(:index => index, :key => key, :value => value)
end

def remove_node_from_index(index, key_or_id, value_or_id = nil, id = nil)
delete remove_from_index_path(NodeIndexes, index, key_or_id, value_or_id, id)
end

# NodeProperties

def set_node_property(id, property)
put NodeProperties.single_path(:id => get_id(id), :property => property.keys.first) do
property.values.first
end
end

def reset_node_properties(id, body)
put NodeProperties.all_path(:id => get_id(id)) do
body
end
end

# NodeRelationships

def get_node_relationships(id, direction = nil)
get NodeRelationships.direction_path(:id => get_id(id), :direction => direction || 'all')
end

# Relationships

def get_relationship(id)
get Relationships.base_path(:id => get_id(id))
end

def delete_relationship(id)
delete Relationships.base_path(:id => get_id(id))
end

def create_relationship(type, from, to, data)
post build_node_uri(from) + "/relationships" do
{
:to => build_node_uri(to),
:type => type,
:data => data
}
end
end

# RelationshipIndexes

def create_unique_relationship(index, key, value, type, from, to)
post RelationshipIndexes.unique_path(:index => index) do
{
:key => key,
:value => value,
:type => type,
:start => build_node_uri(from),
:end => build_node_uri(to)
}
end
end

def add_relationship_to_index(index, key, value, id)
post RelationshipIndexes.base_path(:index => index) do
{
:uri => build_relationship_uri(id),
:key => key,
:value => value
}
end
end

def get_relationship_index(index, key, value)
get RelationshipIndexes.key_value_path(:index => index, :key => key, :value => value)
end

def remove_relationship_from_index(index, key_or_id, value_or_id = nil, id = nil)
delete remove_from_index_path(RelationshipIndexes, index, key_or_id, value_or_id, id)
end

# RelationshipProperties

def set_relationship_property(id, property)
put RelationshipProperties.single_path(:id => get_id(id), :property => property.keys.first) do
property.values.first
end
end

def reset_relationship_properties(id, body)
put build_relationship_uri(id) + "/properties" do
body
end
end

# Cypher

def execute_query(query, params = nil)
request = post @connection.cypher_path do
{
:query => query
}
end

request[:body].merge!({ :params => params }) if params

request
end

# Gremlin

def execute_script(script, params = nil)
post @connection.gremlin_path do
{
:script => script,
:params => params
}
end
end

# Similar between nodes and relationships

def remove_from_index_path(klass, index, key_or_id, value_or_id = nil, id = nil)
if id
klass.value_path(:index => index, :key => key_or_id, :value => value_or_id, :id => get_id(id))
elsif value_or_id
klass.key_path(:index => index, :key => key_or_id, :id => get_id(value_or_id))
else
klass.id_path(:index => index, :id => get_id(key_or_id))
end
end

def get(to, &block)
request "GET", to, &block
end

def delete(to, &block)
request "DELETE", to, &block
end

def post(to, &block)
request "POST", to, &block
end

def put(to, &block)
request "PUT", to, &block
end

def request(method, to, &block)
request = {
:method => method,
:to => to
}
request.merge!({ :body => yield }) if block_given?
request
end

# Helper methods

def build_node_uri(value)
build_uri(value, "node")
end

def build_relationship_uri(value)
build_uri(value, "relationship")
end

def build_uri(value, type)
path_or_variable(value, type) + "#{get_id(value)}"
end

def path_or_variable(value, type)
if value.is_a?(String) && value.start_with?("{")
""
else
"/#{type}/"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/clean.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Clean
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :clean, "/cleandb/secret-key"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeAutoIndexes < AutoIndexes
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :key_value, "/index/auto/node/:key/:value"
add_path :query_index, "/index/auto/node/?query=:query"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeIndexes < Indexes
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :all, "/index/node"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_paths.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodePaths
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :base, "/node/:id/path"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_properties.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeProperties < Properties
include Neography::Rest::Paths
extend Neography::Rest::Paths

add_path :all, "/node/:id/properties"
add_path :single, "/node/:id/properties/:property"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_relationships.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeRelationships
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :base, "/node/:id/relationships"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_traversal.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class NodeTraversal
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :traversal, "/node/:id/traverse/:type"
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/nodes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neography
class Rest
class Nodes
include Neography::Rest::Paths
extend Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :index, "/node"
Expand Down
Loading

0 comments on commit b4834e5

Please sign in to comment.