Skip to content

Commit

Permalink
Moves duplicate code to superclasses, cleaned up other duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 9, 2012
1 parent 0292553 commit 1275c33
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 368 deletions.
4 changes: 4 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def find_and_require_user_defined_code
require 'neography/rest/helpers'
require 'neography/rest/paths'

require 'neography/rest/properties'
require 'neography/rest/indexes'
require 'neography/rest/auto_indexes'

require 'neography/rest/nodes'
require 'neography/rest/node_properties'
require 'neography/rest/node_relationships'
Expand Down
4 changes: 2 additions & 2 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_node_index(index, key, value)

def find_node_index(*args)
case args.size
when 3 then index = @node_indexes.find_by_value(args[0], args[1], args[2])
when 3 then index = @node_indexes.find_by_key_value(args[0], args[1], args[2])
when 2 then index = @node_indexes.find_by_query(args[0], args[1])
end
return nil if index.empty?
Expand Down Expand Up @@ -261,7 +261,7 @@ def get_relationship_index(index, key, value)

def find_relationship_index(*args)
case args.size
when 3 then index = @relationship_indexes.find_by_key_query(args[0], args[1], args[2])
when 3 then index = @relationship_indexes.find_by_key_value(args[0], args[1], args[2])
when 2 then index = @relationship_indexes.find_by_query(args[0], args[1])
end
return nil if index.empty?
Expand Down
54 changes: 54 additions & 0 deletions lib/neography/rest/auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Neography
class Rest
class AutoIndexes
include Neography::Rest::Helpers

def initialize(connection)
@connection = connection
end

def get(key, value)
index = @connection.get(key_value_path(:key => key, :value => value)) || []
return nil if index.empty?
index
end

def find(key, value)
@connection.get(key_value_path(:key => key, :value => value)) || []
end

def query(query_expression)
@connection.get(query_index_path(:query => query_expression)) || []
end

def status
@connection.get(index_status_path)
end

def status=(value)
options = {
:body => value.to_json,
:headers => json_content_type
}
@connection.put(index_status_path, options)
end

def properties
@connection.get(index_properties_path)
end

def add_property(property)
options = {
:body => property,
:headers => json_content_type
}
@connection.post(index_properties_path, options)
end

def remove_property(property)
@connection.delete(index_property_path(:property => property))
end

end
end
end
76 changes: 76 additions & 0 deletions lib/neography/rest/indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Neography
class Rest
class Indexes
include Neography::Rest::Helpers

def initialize(connection, index_type)
@connection = connection
@index_type = index_type
end

def list
@connection.get(all_path)
end

def create(name, type, provider)
options = {
:body => (
{ :name => name,
:config => {
:type => type,
:provider => provider
}
}
).to_json,
:headers => json_content_type
}
@connection.post(all_path, options)
end

def create_auto(type, provider)
create("#{@index_type}_auto_index", type, provider)
end

def add(index, key, value, id)
options = {
:body => (
{ :uri => @connection.configuration + "/#{@index_type}/#{get_id(id)}",
:key => key,
:value => value
}
).to_json,
:headers => json_content_type
}

@connection.post(base_path(:index => index), options)
end

def get(index, key, value)
index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || []
return nil if index.empty?
index
end

def find_by_key_value(index, key, value)
@connection.get(key_value_path(:index => index, :key => key, :value => value)) || []
end

def find_by_query(index, query)
@connection.get(query_path(:index => index, :query => query)) || []
end

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

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

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

end
end
end
49 changes: 1 addition & 48 deletions lib/neography/rest/node_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,14 @@
module Neography
class Rest
class NodeAutoIndexes
class NodeAutoIndexes < AutoIndexes
include Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :key_value, "/index/auto/node/:key/:value"
add_path :query_index, "/index/auto/node/?query=:query"
add_path :index_status, "/index/auto/node/status"
add_path :index_properties, "/index/auto/node/properties"
add_path :index_property, "/index/auto/node/properties/:property"

def initialize(connection)
@connection = connection
end

def get(key, value)
index = @connection.get(key_value_path(:key => key, :value => value)) || []
return nil if index.empty?
index
end

def find(key, value)
@connection.get(key_value_path(:key => key, :value => value)) || []
end

def query(query_expression)
@connection.get(query_index_path(:query => query_expression)) || []
end

def status
@connection.get(index_status_path)
end

def status=(value)
options = {
:body => value.to_json,
:headers => json_content_type
}
@connection.put(index_status_path, options)
end

def properties
@connection.get(index_properties_path)
end

def add_property(property)
options = {
:body => property,
:headers => json_content_type
}
@connection.post(index_properties_path, options)
end

def remove_property(property)
@connection.delete(index_property_path(:property => property))
end

end
end
end
69 changes: 3 additions & 66 deletions lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,20 @@
module Neography
class Rest
class NodeIndexes
class NodeIndexes < Indexes
include Neography::Rest::Paths
include Neography::Rest::Helpers

add_path :all, "/index/node"
add_path :base, "/index/node/:index"
add_path :unique, "/index/node/:index?unique"
add_path :node, "/index/node/:index/:id"
add_path :id, "/index/node/:index/:id"
add_path :key, "/index/node/:index/:key/:id"
add_path :value, "/index/node/:index/:key/:value/:id"
add_path :key_value, "/index/node/:index/:key/:value"
add_path :query, "/index/node/:index?query=:query"

def initialize(connection)
@connection = connection
end

def list
@connection.get(all_path)
end

def create(name, type, provider)
options = {
:body => (
{ :name => name,
:config => {
:type => type,
:provider => provider
}
}
).to_json,
:headers => json_content_type
}
@connection.post(all_path, options)
end

def create_auto(type, provider)
create("node_auto_index", type, provider)
super(connection, :node)
end

def create_unique(index, key, value, properties)
Expand All @@ -53,46 +30,6 @@ def create_unique(index, key, value, properties)
@connection.post(unique_path(:index => index), options)
end

def add(index, key, value, id)
options = {
:body => (
{ :uri => @connection.configuration + "/node/#{get_id(id)}",
:key => key,
:value => value
}
).to_json,
:headers => json_content_type
}
@connection.post(base_path(:index => index), options)
end

def get(index, key, value)
index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || []
return nil if index.empty?
index
end

# TODO FIX BUG %20
def find_by_value(index, key, value)
@connection.get(key_value_path(:index => index, :key => key, :value => value)) || []
end

def find_by_query(index, query)
@connection.get(query_path(:index => index, :query => query)) || []
end

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

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

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

end
end
end
39 changes: 14 additions & 25 deletions lib/neography/rest/node_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,17 @@ def initialize(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
}
options = path_options(to, relationships, depth, algorithm)
@connection.post(base_path(:id => get_id(from)), options) || {}
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
}
options = path_options(to, relationships, depth, algorithm)
@connection.post(all_path(:id => get_id(from)), options) || []
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
}
options = path_options(to, relationships, depth, algorithm, { :cost_property => weight_attribute })
@connection.post(all_path(:id => get_id(from)), options) || {}
end

Expand All @@ -63,6 +41,17 @@ def get_algorithm(algorithm)
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

0 comments on commit 1275c33

Please sign in to comment.