diff --git a/lib/neography.rb b/lib/neography.rb index 03c7f1b..7661e95 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -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' diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 87a408d..e6990fe 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -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? @@ -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? diff --git a/lib/neography/rest/auto_indexes.rb b/lib/neography/rest/auto_indexes.rb new file mode 100644 index 0000000..891b1b8 --- /dev/null +++ b/lib/neography/rest/auto_indexes.rb @@ -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 diff --git a/lib/neography/rest/indexes.rb b/lib/neography/rest/indexes.rb new file mode 100644 index 0000000..8cd5664 --- /dev/null +++ b/lib/neography/rest/indexes.rb @@ -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 diff --git a/lib/neography/rest/node_auto_indexes.rb b/lib/neography/rest/node_auto_indexes.rb index ae752da..0491a68 100644 --- a/lib/neography/rest/node_auto_indexes.rb +++ b/lib/neography/rest/node_auto_indexes.rb @@ -1,8 +1,7 @@ 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" @@ -10,52 +9,6 @@ class NodeAutoIndexes 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 diff --git a/lib/neography/rest/node_indexes.rb b/lib/neography/rest/node_indexes.rb index ce8e8fd..634004e 100644 --- a/lib/neography/rest/node_indexes.rb +++ b/lib/neography/rest/node_indexes.rb @@ -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) @@ -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 diff --git a/lib/neography/rest/node_paths.rb b/lib/neography/rest/node_paths.rb index 1b0cb47..48f6c84 100644 --- a/lib/neography/rest/node_paths.rb +++ b/lib/neography/rest/node_paths.rb @@ -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 @@ -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 diff --git a/lib/neography/rest/node_properties.rb b/lib/neography/rest/node_properties.rb index 29f3f2a..8c7806f 100644 --- a/lib/neography/rest/node_properties.rb +++ b/lib/neography/rest/node_properties.rb @@ -1,60 +1,11 @@ module Neography class Rest - class NodeProperties + class NodeProperties < Properties include Neography::Rest::Paths - include Neography::Rest::Helpers add_path :all, "/node/:id/properties" add_path :single, "/node/:id/properties/:property" - def initialize(connection) - @connection = connection - end - - def set(id, properties) - properties.each do |property, value| - options = { :body => value.to_json, :headers => json_content_type } - @connection.put(single_path(:id => get_id(id), :property => property), options) - end - end - - def reset(id, properties) - options = { :body => properties.to_json, :headers => json_content_type } - @connection.put(all_path(:id => get_id(id)), options) - end - - def get(id, *properties) - if properties.none? - @connection.get(all_path(:id => get_id(id))) - else - get_each(id, *properties) - end - end - - def get_each(id, *properties) - node_properties = properties.inject({}) do |memo, property| - value = @connection.get(single_path(:id => get_id(id), :property => property)) - memo[property] = value unless value.nil? - memo - end - return nil if node_properties.empty? - node_properties - end - - def remove(id, *properties) - if properties.none? - @connection.delete(all_path(:id => get_id(id))) - else - remove_each(id, *properties) - end - end - - def remove_each(id, *properties) - properties.each do |property| - @connection.delete(single_path(:id => get_id(id), :property => property)) - end - end - end end end diff --git a/lib/neography/rest/properties.rb b/lib/neography/rest/properties.rb new file mode 100644 index 0000000..5f61f67 --- /dev/null +++ b/lib/neography/rest/properties.rb @@ -0,0 +1,56 @@ +module Neography + class Rest + class Properties + include Neography::Rest::Helpers + + def initialize(connection) + @connection = connection + end + + def set(id, properties) + properties.each do |property, value| + options = { :body => value.to_json, :headers => json_content_type } + @connection.put(single_path(:id => get_id(id), :property => property), options) + end + end + + def reset(id, properties) + options = { :body => properties.to_json, :headers => json_content_type } + @connection.put(all_path(:id => get_id(id)), options) + end + + def get(id, *properties) + if properties.none? + @connection.get(all_path(:id => get_id(id))) + else + get_each(id, *properties) + end + end + + def get_each(id, *properties) + retrieved_properties = properties.inject({}) do |memo, property| + value = @connection.get(single_path(:id => get_id(id), :property => property)) + memo[property] = value unless value.nil? + memo + end + return nil if retrieved_properties.empty? + retrieved_properties + end + + def remove(id, *properties) + if properties.none? + @connection.delete(all_path(:id => get_id(id))) + else + remove_each(id, *properties) + end + end + + def remove_each(id, *properties) + properties.each do |property| + @connection.delete(single_path(:id => get_id(id), :property => property)) + end + end + + end + end +end diff --git a/lib/neography/rest/relationship_auto_indexes.rb b/lib/neography/rest/relationship_auto_indexes.rb index 7b282bd..0594a15 100644 --- a/lib/neography/rest/relationship_auto_indexes.rb +++ b/lib/neography/rest/relationship_auto_indexes.rb @@ -1,8 +1,7 @@ module Neography class Rest - class RelationshipAutoIndexes + class RelationshipAutoIndexes < AutoIndexes include Neography::Rest::Paths - include Neography::Rest::Helpers add_path :key_value, "/index/auto/relationship/:key/:value" add_path :query_index, "/index/auto/relationship/?query=:query" @@ -10,52 +9,6 @@ class RelationshipAutoIndexes add_path :index_properties, "/index/auto/relationship/properties" add_path :index_property, "/index/auto/relationship/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 diff --git a/lib/neography/rest/relationship_indexes.rb b/lib/neography/rest/relationship_indexes.rb index 97f578a..872b37a 100644 --- a/lib/neography/rest/relationship_indexes.rb +++ b/lib/neography/rest/relationship_indexes.rb @@ -1,46 +1,20 @@ module Neography class Rest - class RelationshipIndexes + class RelationshipIndexes < Indexes include Neography::Rest::Paths include Neography::Rest::Helpers - add_path :all, "/index/relationship" - add_path :base, "/index/relationship/:index" - add_path :unique, "/index/relationship/:index?unique" - - add_path :relationship, "/index/relationship/:index/:id" - add_path :key, "/index/relationship/:index/:key/:id" - add_path :value, "/index/relationship/:index/:key/:value/:id" - add_path :key_value, "/index/relationship/:index/:key/:value" - - add_path :key_query, "/index/relationship/:index/:key?query=:query" - add_path :query, "/index/relationship/:index?query=:query" + add_path :all, "/index/relationship" + add_path :base, "/index/relationship/:index" + add_path :unique, "/index/relationship/:index?unique" + add_path :id, "/index/relationship/:index/:id" + add_path :key, "/index/relationship/:index/:key/:id" + add_path :value, "/index/relationship/:index/:key/:value/:id" + add_path :key_value, "/index/relationship/:index/:key/:value" + add_path :query, "/index/relationship/: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("relationship_auto_index", type, provider) + super(connection, :relationship) end def create_unique(index, key, value, type, from, to) @@ -56,46 +30,6 @@ def create_unique(index, key, value, type, from, to) @connection.post(unique_path(:index => index), options) end - def add(index, key, value, id) - options = { - :body => ( - { :uri => @connection.configuration + "/relationship/#{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_query(index, key, query) - @connection.get(key_query_path(:index => index, :key => key, :query => query)) || [] - end - - def find_by_query(index, query) - @connection.get(query_path(:index => index, :query => query)) || [] - end - - def remove(index, id) - @connection.delete(relationship_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 diff --git a/lib/neography/rest/relationship_properties.rb b/lib/neography/rest/relationship_properties.rb index 8f25d45..324d250 100644 --- a/lib/neography/rest/relationship_properties.rb +++ b/lib/neography/rest/relationship_properties.rb @@ -1,60 +1,11 @@ module Neography class Rest - class RelationshipProperties + class RelationshipProperties < Properties include Neography::Rest::Paths - include Neography::Rest::Helpers add_path :all, "/relationship/:id/properties" add_path :single, "/relationship/:id/properties/:property" - def initialize(connection) - @connection = connection - end - - def set(id, properties) - properties.each do |key, value| - options = { :body => value.to_json, :headers => json_content_type } - @connection.put(single_path(:id => get_id(id), :property => key), options) - end - end - - def reset(id, properties) - options = { :body => properties.to_json, :headers => json_content_type } - @connection.put(all_path(:id => get_id(id)), options) - end - - def get(id, *properties) - if properties.none? - @connection.get(all_path(:id => get_id(id))) - else - get_each(id, *properties) - end - end - - def get_each(id, *properties) - relationship_properties = properties.inject({}) do |memo, property| - value = @connection.get(single_path(:id => get_id(id), :property => property)) - memo[property] = value unless value.nil? - memo - end - return nil if relationship_properties.empty? - relationship_properties - end - - def remove(id, *properties) - if properties.none? - @connection.delete(all_path(:id => get_id(id))) - else - remove_each(id, *properties) - end - end - - def remove_each(id, *properties) - properties.each do |property| - @connection.delete(single_path(:id => get_id(id), :property => property)) - end - end - end end end diff --git a/spec/unit/rest/node_indexes_spec.rb b/spec/unit/rest/node_indexes_spec.rb index 10d5382..f50a0c1 100644 --- a/spec/unit/rest/node_indexes_spec.rb +++ b/spec/unit/rest/node_indexes_spec.rb @@ -73,7 +73,7 @@ class Rest it "finds by key and value" do connection.should_receive(:get).with("/index/node/some_index/some_key/some_value") - subject.find_by_value("some_index", "some_key", "some_value") + subject.find_by_key_value("some_index", "some_key", "some_value") end it "finds by query" do diff --git a/spec/unit/rest/relationship_indexes_spec.rb b/spec/unit/rest/relationship_indexes_spec.rb index 6cacf41..4db7988 100644 --- a/spec/unit/rest/relationship_indexes_spec.rb +++ b/spec/unit/rest/relationship_indexes_spec.rb @@ -74,8 +74,8 @@ class Rest end it "finds by key query" do - connection.should_receive(:get).with("/index/relationship/some_index/some_key?query=some_query") - subject.find_by_key_query("some_index", "some_key", "some_query") + connection.should_receive(:get).with("/index/relationship/some_index/some_key/some_value") + subject.find_by_key_value("some_index", "some_key", "some_value") end it "finds by query" do