Skip to content

Commit

Permalink
Merge pull request #58 from rdvdijk/master
Browse files Browse the repository at this point in the history
Major refactor of Rest class
  • Loading branch information
maxdemarzi committed Sep 8, 2012
2 parents faf7f0c + 0292553 commit 92b0097
Show file tree
Hide file tree
Showing 41 changed files with 2,580 additions and 639 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
httparty (0.8.3)
httparty (0.9.0)
multi_json (~> 1.0)
multi_xml
json (1.7.5)
Expand Down
21 changes: 21 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,28 @@ 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/node_traversal'
require 'neography/rest/node_paths'
require 'neography/rest/relationships'
require 'neography/rest/relationship_properties'
require 'neography/rest/relationship_indexes'
require 'neography/rest/relationship_auto_indexes'
require 'neography/rest/cypher'
require 'neography/rest/gremlin'
require 'neography/rest/batch'
require 'neography/rest/clean'
require 'neography/connection'
require 'neography/rest'

require 'neography/neography'

require 'neography/property_container'
Expand Down
120 changes: 120 additions & 0 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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 = {
:protocol => url.scheme + "://",
:server => url.host,
:port => url.port,
:directory => url.path,
:username => url.user,
: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 = {}
@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 + path, merge_options(options)))
end

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

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

def delete(path, options={})
evaluate_response(HTTParty.delete(configuration + 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
44 changes: 24 additions & 20 deletions lib/neography/node_traverser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ def order(o)
end

def filter(body)
@filter = Hash.new
@filter["language"] = "javascript"
@filter["body"] = body
@filter = {
"language" => "javascript",
"body" => body
}
self
end

def prune(body)
@prune = Hash.new
@prune["language"] = "javascript"
@prune["body"] = body
@prune = {
"language" => "javascript",
"body" => body
}
self
end

Expand All @@ -79,9 +81,10 @@ def depth(d)
end

def include_start_node
@filter = Hash.new
@filter["language"] = "builtin"
@filter["name"] = "all"
@filter = {
"language" => "builtin",
"name" => "all"
}
self
end

Expand All @@ -108,13 +111,14 @@ def each
end

def iterator
options = Hash.new
options["order"] = @order
options["uniqueness"] = @uniqueness
options["relationships"] = @relationships
options["prune evaluator"] = @prune unless @prune.nil?
options["return filter"] = @filter unless @filter.nil?
options["depth"] = @depth unless @depth.nil?
options = {
"order" => @order,
"uniqueness" => @uniqueness,
"relationships" => @relationships
}
options["prune evaluator"] = @prune unless @prune.nil?
options["return filter"] = @filter unless @filter.nil?
options["depth"] = @depth unless @depth.nil?

if @relationships[0]["type"].empty?
rels = @from.neo_server.get_node_relationships(@from, @relationships[0]["direction"])
Expand All @@ -124,11 +128,11 @@ def iterator
when "out"
rels.collect { |r| @from.neo_server.get_node(r["end"]) } #.uniq
else
rels.collect { |r|
rels.collect { |r|
if @from.neo_id == r["start"].split('/').last
@from.neo_server.get_node(r["end"])
@from.neo_server.get_node(r["end"])
else
@from.neo_server.get_node(r["start"])
@from.neo_server.get_node(r["start"])
end
} #.uniq
end
Expand All @@ -139,4 +143,4 @@ def iterator

end

end
end
Loading

0 comments on commit 92b0097

Please sign in to comment.