Skip to content

Commit

Permalink
Make configuration a more 'rubyish'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dijk committed Sep 24, 2012
1 parent 317a44d commit bee8025
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 83 deletions.
53 changes: 39 additions & 14 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/<my directory>' 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:[email protected]:7474/mydirectory")


To Use:
Usage:

@neo = Neography::Rest.new # Inialize using all default parameters

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
64 changes: 48 additions & 16 deletions lib/neography/config.rb
Original file line number Diff line number Diff line change
@@ -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
95 changes: 42 additions & 53 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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 = {
Expand Down
23 changes: 23 additions & 0 deletions spec/neography_spec.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bee8025

Please sign in to comment.