Skip to content

Commit

Permalink
First step of major refactor of REST class.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 7, 2012
1 parent 6289b7a commit db2f1dd
Show file tree
Hide file tree
Showing 14 changed files with 861 additions and 397 deletions.
12 changes: 12 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ def find_and_require_user_defined_code
require 'neography/version'

require 'neography/config'

require 'neography/rest_helpers'
require 'neography/rest_paths'
require 'neography/rest/nodes'
require 'neography/rest/node_properties'
require 'neography/rest/node_relationships'
require 'neography/rest/node_indexes'
require 'neography/rest/node_auto_indexes'
require 'neography/rest/relationships'
require 'neography/rest/relationship_properties'
require 'neography/connection'
require 'neography/rest'

require 'neography/neography'

require 'neography/property_container'
Expand Down
118 changes: 118 additions & 0 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module Neography
class Connection

USER_AGENT = "Neography/#{Neography::VERSION}"

attr_accessor :protocol, :server, :port, :directory,
:cypher_path, :gremlin_path,
:log_file, :log_enabled, :logger,
:max_threads,
:authentication, :username, :password,
:parser

def initialize(options=ENV['NEO4J_URL'] || {})
init = {: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
}

unless options.respond_to?(:each_pair)
url = URI.parse(options)
options = Hash.new
options[:protocol] = url.scheme + "://"
options[:server] = url.host
options[:port] = url.port
options[:directory] = url.path
options[:username] = url.user
options[:password] = url.password
options[:authentication] = 'basic' unless url.user.nil?
end

init.merge!(options)

@protocol = init[:protocol]
@server = init[:server]
@port = init[:port]
@directory = init[:directory]
@cypher_path = init[:cypher_path]
@gremlin_path = init[:gremlin_path]
@log_file = init[:log_file]
@log_enabled = init[:log_enabled]
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = init[:max_threads]
@authentication = Hash.new
@authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty?
@parser = init[:parser]
@user_agent = {"User-Agent" => USER_AGENT}
end

def configure(protocol, server, port, directory)
@protocol = protocol
@server = server
@port = port
@directory = directory
end

def configuration
@protocol + @server + ':' + @port.to_s + @directory + "/db/data"
end

def merge_options(options)
merged_options = options.merge!(@authentication).merge!(@parser)
merged_options[:headers].merge!(@user_agent) if merged_options[:headers]
merged_options
end

def get(path, options={})
evaluate_response(HTTParty.get(configuration + URI.encode(path), merge_options(options)))
end

def post(path, options={})
evaluate_response(HTTParty.post(configuration + URI.encode(path), merge_options(options)))
end

def put(path, options={})
evaluate_response(HTTParty.put(configuration + URI.encode(path), merge_options(options)))
end

def delete(path, options={})
evaluate_response(HTTParty.delete(configuration + URI.encode(path), merge_options(options)))
end

def evaluate_response(response)
code = response.code
body = response.body
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
@logger.debug "OK, no content returned" if @log_enabled
nil
when 400
@logger.error "Invalid data sent #{body}" if @log_enabled
nil
when 404
@logger.error "Not Found #{body}" if @log_enabled
nil
when 409
@logger.error "Node could not be deleted (still has relationships?)" if @log_enabled
nil
end
end

end
end
Loading

0 comments on commit db2f1dd

Please sign in to comment.