Skip to content

Commit

Permalink
Rubified some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 8, 2012
1 parent 3b9e97f commit bbe019c
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 76 deletions.
30 changes: 16 additions & 14 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class Connection
:parser

def initialize(options=ENV['NEO4J_URL'] || {})
init = {:protocol => Neography::Config.protocol,
:server => Neography::Config.server,
:port => Neography::Config.port,
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,
: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,
Expand All @@ -28,13 +29,14 @@ def initialize(options=ENV['NEO4J_URL'] || {})

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 = {
: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

Expand All @@ -50,7 +52,7 @@ def initialize(options=ENV['NEO4J_URL'] || {})
@log_enabled = init[:log_enabled]
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = init[:max_threads]
@authentication = Hash.new
@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}
Expand Down
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
8 changes: 4 additions & 4 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ def reset_relationship_properties(id, properties)
@relationship_properties.reset(id, properties)
end

def get_relationship_properties(id, properties = nil)
@relationship_properties.get(id, properties)
def get_relationship_properties(id, *properties)
@relationship_properties.get(id, *properties.flatten)
end

def remove_relationship_properties(id, properties = nil)
@relationship_properties.remove(id, properties)
def remove_relationship_properties(id, *properties)
@relationship_properties.remove(id, *properties.flatten)
end

def set_relationship_properties(id, properties)
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/node_auto_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def initialize(connection)
end

def get(key, value)
index = @connection.get(key_value_path(:key => key, :value => value)) || Array.new
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)) || Array.new
@connection.get(key_value_path(:key => key, :value => value)) || []
end

def query(query_expression)
@connection.get(query_index_path(:query => query_expression)) || Array.new
@connection.get(query_index_path(:query => query_expression)) || []
end

def status
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ def add(index, key, value, id)
end

def get(index, key, value)
index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || Array.new
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_value2_path(:index => index, :key => key, :value => value)) || Array.new
@connection.get(key_value2_path(:index => index, :key => key, :value => value)) || []
end

def find_by_query(index, query)
@connection.get(query_path(:index => index, :query => query)) || Array.new
@connection.get(query_path(:index => index, :query => query)) || []
end

def remove(index, id)
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/node_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get(from, to, relationships, depth, algorithm)
}.to_json,
:headers => json_content_type
}
@connection.post(base_path(:id => get_id(from)), options) || Hash.new
@connection.post(base_path(:id => get_id(from)), options) || {}
end

def get_all(from, to, relationships, depth, algorithm)
Expand All @@ -32,7 +32,7 @@ def get_all(from, to, relationships, depth, algorithm)
}.to_json,
:headers => json_content_type
}
@connection.post(all_path(:id => get_id(from)), options) || Array.new
@connection.post(all_path(:id => get_id(from)), options) || []
end

def shortest_weighted(from, to, relationships, weight_attribute, depth, algorithm)
Expand All @@ -45,7 +45,7 @@ def shortest_weighted(from, to, relationships, weight_attribute, depth, algorith
}.to_json,
:headers => json_content_type
}
@connection.post(all_path(:id => get_id(from)), options) || Hash.new
@connection.post(all_path(:id => get_id(from)), options) || {}
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/neography/rest/node_relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def get(id, direction, types)
direction = get_direction(direction)

if types.nil?
node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || Array.new
node_relationships = @connection.get(direction_path(:id => get_id(id), :direction => direction)) || []
else
node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || Array.new
node_relationships = @connection.get(type_path(:id => get_id(id), :direction => direction, :types => Array(types).join('&'))) || []
end
return nil if node_relationships.empty?
node_relationships
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/rest/node_traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def traverse(id, return_type, description)

type = get_type(return_type)

@connection.post(traversal_path(:id => get_id(id), :type => type), options) || Array.new
@connection.post(traversal_path(:id => get_id(id), :type => type), options) || []
end

private
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get(id)
end

def get_each(*nodes)
gotten_nodes = Array.new
gotten_nodes = []
Array(nodes).flatten.each do |node|
gotten_nodes << get(node)
end
Expand Down Expand Up @@ -50,7 +50,7 @@ def create_empty

def create_multiple(nodes)
nodes = Array.new(nodes) if nodes.kind_of? Fixnum
created_nodes = Array.new
created_nodes = []
nodes.each do |node|
created_nodes << create(node)
end
Expand Down Expand Up @@ -85,7 +85,7 @@ def create_multiple_threaded(nodes)
end
end

created_nodes = Array.new
created_nodes = []

while created_nodes.size < nodes.size
created_nodes << responses.pop
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/relationship_auto_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def initialize(connection)
end

def get(key, value)
index = @connection.get(key_value_path(:key => key, :value => value)) || Array.new
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)) || Array.new
@connection.get(key_value_path(:key => key, :value => value)) || []
end

def query(query_expression)
@connection.get(query_index_path(:query => query_expression)) || Array.new
@connection.get(query_index_path(:query => query_expression)) || []
end

def status
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/relationship_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ def add(index, key, value, id)
end

def get(index, key, value)
index = @connection.get(key_value_path(:index => index, :key => key, :value => value)) || Array.new
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)) || Array.new
@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)) || Array.new
@connection.get(query_path(:index => index, :query => query)) || []
end

def remove(index, id)
Expand Down
42 changes: 25 additions & 17 deletions lib/neography/rest/relationship_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,40 @@ def initialize(connection)
@connection = connection
end

def get(id, properties)
if properties.nil?
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
relationship_properties = Hash.new
Array(properties).each do |property|
value = @connection.get(single_path(:id => get_id(id), :property => property))
relationship_properties[property] = value unless value.nil?
end
return nil if relationship_properties.empty?
relationship_properties
get_each(id, *properties)
end
end

def reset(id, properties)
options = { :body => properties.to_json, :headers => json_content_type }
@connection.put(all_path(:id => get_id(id)), options)
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.nil?
def remove(id, *properties)
if properties.none?
@connection.delete(all_path(id: get_id(id)))
else
Array(properties).each do |property|
@connection.delete(single_path(:id => get_id(id), :property => property))
end
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

Expand Down

0 comments on commit bbe019c

Please sign in to comment.