From bee80257323419aebb55367fda62c0475efb8dee Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Mon, 24 Sep 2012 20:13:30 +0200 Subject: [PATCH] Make configuration a more 'rubyish'. --- README.rdoc | 53 +++++++++++++++------ lib/neography.rb | 11 +++++ lib/neography/config.rb | 64 ++++++++++++++++++------- lib/neography/connection.rb | 95 ++++++++++++++++--------------------- spec/neography_spec.rb | 23 +++++++++ spec/unit/config_spec.rb | 46 ++++++++++++++++++ 6 files changed, 209 insertions(+), 83 deletions(-) create mode 100644 spec/neography_spec.rb create mode 100644 spec/unit/config_spec.rb diff --git a/README.rdoc b/README.rdoc index 3440eef..8947d00 100644 --- a/README.rdoc +++ b/README.rdoc @@ -84,25 +84,49 @@ rake neo4j:install requires wget to be installed. It will download and install === Documentation - @neo = Neography::Rest.new({:protocol => 'http://', - :server => 'localhost', - :port => 7474, - :directory => '', # use '/' or leave out for default - :authentication => 'basic', # 'basic', 'digest' or leave out for default - :username => 'your username', #leave out for default - :password => 'your password', #leave out for default - :log_file => 'neography.log', - :log_enabled => false, - :max_threads => 20, - :cypher_path => '/cypher', - :gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script'}) +Configure Neography as follows: + + # these are the default values: + Neography.configure do |config| + config.protocol = "http://" + config.server = "localhost" + config.port = 7474 + config.directory = "" # prefix this path with '/' + config.cypher_path = "/cypher" + config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" + config.log_file = "neography.log" + config.log_enabled = false + config.max_threads = 20 + config.authentication = nil # 'basic' or 'digest' + config.username = nil + config.password = nil + config.parser = {:parser => MultiJsonParser} + end + +Then initialize as follows: + + @neo = Neography::Rest.new + +Or you can override the configured values as follows: + + @neo = Neography::Rest.new({ :protocol => 'http://', + :server => 'localhost', + :port => 7474, + :directory => '', + :authentication => 'basic', + :username => 'your username', + :password => 'your password', + :log_file => 'neography.log', + :log_enabled => false, + :max_threads => 20, + :cypher_path => '/cypher', + :gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script' }) Quick initializer (assumes basic authorization if username is given): @neo = Neography::Rest.new("http://username:password@myserver.com:7474/mydirectory") - -To Use: +Usage: @neo = Neography::Rest.new # Inialize using all default parameters @@ -155,6 +179,7 @@ To Use: @neo.find_node_index(index, query) # advanced query of the node index with the given query @neo.get_node_auto_index(key, value) # exact query of the node auto index with the given key/value pair @neo.find_node_auto_index(query) # advanced query of the node auto index with the given query + @neo.list_relationship_indexes # gives names and query templates for relationship indices @neo.create_relationship_index(name, "fulltext", provider) # creates a relationship index with "fulltext" option @neo.create_relationship_auto_index("fulltext", provider) # creates a relationship auto index with "fulltext" option diff --git a/lib/neography.rb b/lib/neography.rb index 26c435d..605e1f1 100644 --- a/lib/neography.rb +++ b/lib/neography.rb @@ -49,3 +49,14 @@ def find_and_require_user_defined_code find_and_require_user_defined_code +module Neography + + def self.configure + yield configuration + end + + def self.configuration + @configuration ||= Config.new + end + +end diff --git a/lib/neography/config.rb b/lib/neography/config.rb index a97a321..3f519af 100644 --- a/lib/neography/config.rb +++ b/lib/neography/config.rb @@ -1,20 +1,52 @@ module Neography - class Config - class << self; attr_accessor :protocol, :server, :port, :directory, :cypher_path, :gremlin_path, :log_file, :log_enabled, :logger, :max_threads, :authentication, :username, :password, :parser end + class Config + + attr_accessor :protocol, :server, :port, :directory, + :cypher_path, :gremlin_path, + :log_file, :log_enabled, + :max_threads, + :authentication, :username, :password, + :parser + + def initialize + set_defaults + end + + def to_hash + { + :protocol => @protocol, + :server => @server, + :port => @port, + :directory => @directory, + :cypher_path => @cypher_path, + :gremlin_path => @gremlin_path, + :log_file => @log_file, + :log_enabled => @log_enabled, + :max_threads => @max_threads, + :authentication => @authentication, + :username => @username, + :password => @password, + :parser => @parser + } + end + + private + + def set_defaults + @protocol = "http://" + @server = "localhost" + @port = 7474 + @directory = "" + @cypher_path = "/cypher" + @gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" + @log_file = "neography.log" + @log_enabled = false + @max_threads = 20 + @authentication = nil + @username = nil + @password = nil + @parser = {:parser => MultiJsonParser} + end - @protocol = 'http://' - @server = 'localhost' - @port = 7474 - @directory = '' - @cypher_path = '/cypher' - @gremlin_path = '/ext/GremlinPlugin/graphdb/execute_script' - @log_file = 'neography.log' - @log_enabled = false - @logger = Logger.new(@log_file) if @log_enabled - @max_threads = 20 - @authentication = {} - @username = nil - @password = nil - @parser = {:parser => MultiJsonParser} end end diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index e69d117..84b9054 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -11,36 +11,8 @@ class Connection :parser def initialize(options = ENV['NEO4J_URL'] || {}) - options = parse_string_options(options) unless options.is_a? Hash - config = initial_configuration - config.merge!(options) - - @protocol = config[:protocol] - @server = config[:server] - @port = config[:port] - @directory = config[:directory] - @cypher_path = config[:cypher_path] - @gremlin_path = config[:gremlin_path] - @log_file = config[:log_file] - @log_enabled = config[:log_enabled] - @max_threads = config[:max_threads] - @parser = config[:parser] - @user_agent = { "User-Agent" => USER_AGENT } - - @authentication = {} - - unless config[:authentication].empty? - @authentication = { - "#{config[:authentication]}_auth".to_sym => { - :username => config[:username], - :password => config[:password] - } - } - end - - if @log_enabled - @logger = Logger.new(@log_file) - end + config = merge_configuration(options) + save_local_configuration(config) end def configure(protocol, server, port, directory) @@ -51,7 +23,7 @@ def configure(protocol, server, port, directory) end def configuration - @protocol + @server + ':' + @port.to_s + @directory + "/db/data" + "#{@protocol}#{@server}:#{@port}#{@directory}/db/data" end def merge_options(options) @@ -78,21 +50,56 @@ def delete(path, options={}) private + def merge_configuration(options) + options = parse_string_options(options) unless options.is_a? Hash + config = Neography.configuration.to_hash + config.merge(options) + end + + def save_local_configuration(config) + @protocol = config[:protocol] + @server = config[:server] + @port = config[:port] + @directory = config[:directory] + @cypher_path = config[:cypher_path] + @gremlin_path = config[:gremlin_path] + @log_file = config[:log_file] + @log_enabled = config[:log_enabled] + @max_threads = config[:max_threads] + @parser = config[:parser] + + @user_agent = { "User-Agent" => USER_AGENT } + + @authentication = {} + if config[:authentication] + @authentication = { + "#{config[:authentication]}_auth".to_sym => { + :username => config[:username], + :password => config[:password] + } + } + end + + if @log_enabled + @logger = Logger.new(@log_file) + end + end + def evaluate_response(response) code = response.code body = response.body - case code - when 200 + case code + when 200 @logger.debug "OK" if @log_enabled response.parsed_response when 201 @logger.debug "OK, created #{body}" if @log_enabled response.parsed_response - when 204 + when 204 @logger.debug "OK, no content returned" if @log_enabled nil when 400 - @logger.error "Invalid data sent #{body}" if @log_enabled + @logger.error "Invalid data sent #{body}" if @log_enabled nil when 404 @logger.error "Not Found #{body}" if @log_enabled @@ -103,24 +110,6 @@ def evaluate_response(response) end end - def initial_configuration - { - :protocol => Neography::Config.protocol, - :server => Neography::Config.server, - :port => Neography::Config.port, - :directory => Neography::Config.directory, - :cypher_path => Neography::Config.cypher_path, - :gremlin_path => Neography::Config.gremlin_path, - :log_file => Neography::Config.log_file, - :log_enabled => Neography::Config.log_enabled, - :max_threads => Neography::Config.max_threads, - :authentication => Neography::Config.authentication, - :username => Neography::Config.username, - :password => Neography::Config.password, - :parser => Neography::Config.parser - } - end - def parse_string_options(options) url = URI.parse(options) options = { diff --git a/spec/neography_spec.rb b/spec/neography_spec.rb new file mode 100644 index 0000000..543dc43 --- /dev/null +++ b/spec/neography_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe Neography do + + describe "::configure" do + + it "returns the same configuration" do + Neography.configuration.should == Neography.configuration + end + + it "returns the Config" do + Neography.configuration.should be_a Neography::Config + end + + it "yields the configuration" do + Neography.configure do |config| + config.should == Neography.configuration + end + end + + end + +end diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb new file mode 100644 index 0000000..4d8e99e --- /dev/null +++ b/spec/unit/config_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +module Neography + describe Config do + + subject(:config) + + context "defaults" do + + its(:protocol) { should == 'http://' } + its(:server) { should == 'localhost' } + its(:port) { should == 7474 } + its(:directory) { should == '' } + its(:cypher_path) { should == '/cypher' } + its(:gremlin_path) { should == '/ext/GremlinPlugin/graphdb/execute_script' } + its(:log_file) { should == 'neography.log' } + its(:log_enabled) { should == false } + its(:max_threads) { should == 20 } + its(:authentication) { should == nil } + its(:username) { should == nil } + its(:password) { should == nil } + its(:parser) { should == { :parser => MultiJsonParser } } + + it "has a hash representation" do + expected_hash = { + :protocol => 'http://', + :server => 'localhost', + :port => 7474, + :directory => '', + :cypher_path => '/cypher', + :gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script', + :log_file => 'neography.log', + :log_enabled => false, + :max_threads => 20, + :authentication => nil, + :username => nil, + :password => nil, + :parser => { :parser => MultiJsonParser } + } + config.to_hash.should == expected_hash + end + + end + + end +end