Skip to content

Commit

Permalink
added find_id and cleaned up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 21, 2010
1 parent cafaf2e commit c3a00a0
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 581 deletions.
5 changes: 5 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Maintainer:
Max De Marzi <maxdemarzi at gmail dot com>

Contributors:
* Peter Neubauer
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
neography (0.0.1)
neography (0.0.2)
httparty (~> 0.6.1)
json

Expand Down
78 changes: 39 additions & 39 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,45 @@ To Use:

@neo = Neography::Rest.new

@neo.get_root # Get the root node
@neo.create_node # Create an empty node
@neo.create_node("age" => 31, "name" => "Max") # Create a node with some properties
@neo.get_node(id) # Get a node and its properties
@neo.delete_node(id) # Delete an unrelated node
@neo.delete_node!(id) # Delete a node and all its relationships

@neo.reset_node_properties(id, {"age" => 31}) # Reset a node's properties
@neo.set_node_properties(id, {"weight" => 200}) # Set a node's properties
@neo.get_node_properties(id) # Get just the node properties
@neo.get_node_properties(id, ["weight","age"]) # Get some of the node properties
@neo.remove_node_properties(id) # Remove all properties of a node
@neo.remove_node_properties(id, "weight") # Remove one property of a node
@neo.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node

@neo.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2
@neo.get_node_relationships(id) # Get all relationships
@neo.get_node_relationships(id, "in") # Get only incoming relationships
@neo.get_node_relationships(id, "all", "enemies") # Get all relationships of type enemies
@neo.get_node_relationships(id, "in", "enemies") # Get only incoming relationships of type enemies
@neo.delete_relationship(id) # Delete a relationship

@neo.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties
@neo.set_relationship_properties(id, {"weight" => 200}) # Set a relationship's properties
@neo.get_relationship_properties(id) # Get just the relationship properties
@neo.get_relationship_properties(id, ["since","met"]) # Get some of the relationship properties
@neo.remove_relationship_properties(id) # Remove all properties of a relationship
@neo.remove_relationship_properties(id, "since") # Remove one property of a relationship
@neo.remove_relationship_properties(id, ["since","met"]) # Remove multiple properties of a relationship

@neo.list_indexes # doesn't really seam to do what the api says it does
@neo.add_to_index(key, value, id) # adds a node to an index with the given key/value pair
@neo.remove_from_index(key, value, id) # removes a node to an index with the given key/value pair
@neo.get_index(key, value) # gets an index with the given key/value pair

@neo.get_path(from, to, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes
@neo.get_paths(from, to, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes

nodes = @neo.traverse(id, # the id of the node where the traversal starts
@neo.get_root # Get the root node
node1 = @neo.create_node # Create an empty node
node2 = @neo.create_node("age" => 31, "name" => "Max") # Create a node with some properties
@neo.get_node(node2) # Get a node and its properties
@neo.delete_node(node2) # Delete an unrelated node
@neo.delete_node!(node2) # Delete a node and all its relationships

@neo.reset_node_properties(node1, {"age" => 31}) # Reset a node's properties
@neo.set_node_properties(node1, {"weight" => 200}) # Set a node's properties
@neo.get_node_properties(node1) # Get just the node properties
@neo.get_node_properties(node1, ["weight","age"]) # Get some of the node properties
@neo.remove_node_properties(node1) # Remove all properties of a node
@neo.remove_node_properties(node1, "weight") # Remove one property of a node
@neo.remove_node_properties(node1, ["weight","age"]) # Remove multiple properties of a node

rel1 = @neo.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2
@neo.get_node_relationships(node1) # Get all relationships
@neo.get_node_relationships(node1, "in") # Get only incoming relationships
@neo.get_node_relationships(node1, "all", "enemies") # Get all relationships of type enemies
@neo.get_node_relationships(node1, "in", "enemies") # Get only incoming relationships of type enemies
@neo.delete_relationship(rel1) # Delete a relationship

@neo.reset_relationship_properties(rel1, {"age" => 31}) # Reset a relationship's properties
@neo.set_relationship_properties(rel1, {"weight" => 200}) # Set a relationship's properties
@neo.get_relationship_properties(rel1) # Get just the relationship properties
@neo.get_relationship_properties(rel1, ["since","met"]) # Get some of the relationship properties
@neo.remove_relationship_properties(rel1) # Remove all properties of a relationship
@neo.remove_relationship_properties(rel1, "since") # Remove one property of a relationship
@neo.remove_relationship_properties(rel1, ["since","met"]) # Remove multiple properties of a relationship

@neo.list_indexes # doesn't really seam to do what the api says it does
@neo.add_to_index(key, value, node1) # adds a node to an index with the given key/value pair
@neo.remove_from_index(key, value, node1) # removes a node to an index with the given key/value pair
@neo.get_index(key, value) # gets an index with the given key/value pair

@neo.get_path(node1, node2, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes
@neo.get_paths(node1, node2, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes

nodes = @neo.traverse(node1, # the node where the traversal starts
"nodes", # return_type (can be "nodes", "relationships" or "paths"
{"order" => "breadth first", # "breadth first" or "depth first" traversal order
"uniqueness" => "node global", # See Uniqueness in API documentation for options.
Expand Down
71 changes: 41 additions & 30 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ def create_node(*args)
end

def get_node(id)
get("/node/#{id}")
get("/node/#{get_id(id)}")
end

def reset_node_properties(id, properties)
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
put("/node/#{id}/properties", options)
put("/node/#{get_id(id)}/properties", options)
end

def get_node_properties(id, properties = nil)
if properties.nil?
get("/node/#{id}/properties")
get("/node/#{get_id(id)}/properties")
else
node_properties = Hash.new
properties.to_a.each do |property|
value = get("/node/#{id}/properties/#{property}")
value = get("/node/#{get_id(id)}/properties/#{property}")
node_properties[property] = value unless value.nil?
end
return nil if node_properties.empty?
Expand All @@ -60,42 +60,42 @@ def get_node_properties(id, properties = nil)

def remove_node_properties(id, properties = nil)
if properties.nil?
delete("/node/#{id}/properties")
delete("/node/#{get_id(id)}/properties")
else
properties.to_a.each do |property|
delete("/node/#{id}/properties/#{property}")
delete("/node/#{get_id(id)}/properties/#{property}")
end
end
end

def set_node_properties(id, properties)
properties.each do |key, value|
options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} }
put("/node/#{id}/properties/#{key}", options)
put("/node/#{get_id(id)}/properties/#{key}", options)
end
end

def delete_node(id)
delete("/node/#{id}")
delete("/node/#{get_id(id)}")
end

def create_relationship(type, from, to, props = nil)
options = { :body => {:to => self.configuration + "/node/#{to}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} }
post("/node/#{from}/relationships", options)
options = { :body => {:to => self.configuration + "/node/#{get_id(to)}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} }
post("/node/#{get_id(from)}/relationships", options)
end

def reset_relationship_properties(id, properties)
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
put("/relationship/#{id}/properties", options)
put("/relationship/#{get_id(id)}/properties", options)
end

def get_relationship_properties(id, properties = nil)
if properties.nil?
get("/relationship/#{id}/properties")
get("/relationship/#{get_id(id)}/properties")
else
relationship_properties = Hash.new
properties.to_a.each do |property|
value = get("/relationship/#{id}/properties/#{property}")
value = get("/relationship/#{get_id(id)}/properties/#{property}")
relationship_properties[property] = value unless value.nil?
end
return nil if relationship_properties.empty?
Expand All @@ -105,54 +105,54 @@ def get_relationship_properties(id, properties = nil)

def remove_relationship_properties(id, properties = nil)
if properties.nil?
delete("/relationship/#{id}/properties")
delete("/relationship/#{get_id(id)}/properties")
else
properties.to_a.each do |property|
delete("/relationship/#{id}/properties/#{property}")
delete("/relationship/#{get_id(id)}/properties/#{property}")
end
end
end

def set_relationship_properties(id, properties)
properties.each do |key, value|
options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} }
put("/relationship/#{id}/properties/#{key}", options)
put("/relationship/#{get_id(id)}/properties/#{key}", options)
end
end

def delete_relationship(id)
delete("/relationship/#{id}")
delete("/relationship/#{get_id(id)}")
end

def get_node_relationships(id, dir=nil, types=nil)
dir = get_dir(dir)

if types.nil?
node_relationships = get("/node/#{id}/relationships/#{dir}") || Array.new
node_relationships = get("/node/#{get_id(id)}/relationships/#{dir}") || Array.new
else
node_relationships = get("/node/#{id}/relationships/#{dir}/#{types.to_a.join('&')}") || Array.new
node_relationships = get("/node/#{get_id(id)}/relationships/#{dir}/#{types.to_a.join('&')}") || Array.new
end
return nil if node_relationships.empty?
node_relationships
end

def delete_node!(id)
relationships = get_node_relationships(id)
relationships = get_node_relationships(get_id(id))
relationships.each { |r| delete_relationship(r["self"].split('/').last) } unless relationships.nil?
delete("/node/#{id}")
delete("/node/#{get_id(id)}")
end

def list_indexes
get("/index")
end

def add_to_index(key, value, id)
options = { :body => (self.configuration + "/node/#{id}").to_json, :headers => {'Content-Type' => 'application/json'} }
options = { :body => (self.configuration + "/node/#{get_id(id)}").to_json, :headers => {'Content-Type' => 'application/json'} }
post("/index/node/#{key}/#{value}", options)
end

def remove_from_index(key, value, id)
delete("/index/node/#{key}/#{value}/#{id}")
delete("/index/node/#{key}/#{value}/#{get_id(id)}")
end

def get_index(key, value)
Expand All @@ -168,17 +168,17 @@ def traverse(id, return_type, description)
"prune evaluator" => description["prune evaluator"],
"return filter" => description["return filter"],
"max depth" => get_depth(description["depth"]), }.to_json, :headers => {'Content-Type' => 'application/json'} }
traversal = post("/node/#{id}/traverse/#{get_type(return_type)}", options) || Array.new
traversal = post("/node/#{get_id(id)}/traverse/#{get_type(return_type)}", options) || Array.new
end

def get_path(from, to, relationships, depth=1, algorithm="shortestPath")
options = { :body => {"to" => self.configuration + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
path = post("/node/#{from}/path", options) || Hash.new
options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
path = post("/node/#{get_id(from)}/path", options) || Hash.new
end

def get_paths(from, to, relationships, depth=1, algorithm="allPaths")
options = { :body => {"to" => self.configuration + "/node/#{to}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
paths = post("/node/#{from}/paths", options) || Array.new
options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "max depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} }
paths = post("/node/#{get_id(from)}/paths", options) || Array.new
end

private
Expand All @@ -190,10 +190,10 @@ def evaluate_response(response)
case code
when 200
@logger.debug "OK" if @log_enabled
response
response.parsed_response
when 201
@logger.debug "OK, created #{body}" if @log_enabled
response
response.parsed_response
when 204
@logger.debug "OK, no content returned" if @log_enabled
nil
Expand Down Expand Up @@ -225,6 +225,17 @@ def delete(path,options={})
evaluate_response(HTTParty.delete(configuration + path, options))
end

def get_id(id)
case id
when Hash
id["self"].split('/').last
when String
id.split('/').last
else
id
end
end

def get_dir(dir)
case dir
when :incoming, "incoming", :in, "in"
Expand Down
15 changes: 6 additions & 9 deletions spec/integration/rest_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@
describe "add to index" do
it "can add a node to an index" do
new_node = @neo.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node[:id])
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node[:id])
@neo.remove_from_index(key, value, new_node)
end
end

describe "remove from index" do
it "can remove a node from an index" do
new_node = @neo.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node[:id])
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node[:id])
@neo.remove_from_index(key, value, new_node)
new_index = @neo.get_index(key, value)
new_index.should be_nil
end
Expand All @@ -42,13 +40,12 @@
describe "get index" do
it "can get an index" do
new_node = @neo.create_node
new_node[:id] = new_node["self"].split('/').last
key = generate_text(6)
value = generate_text
@neo.add_to_index(key, value, new_node[:id])
@neo.add_to_index(key, value, new_node)
new_index = @neo.get_index(key, value)
new_index.should_not be_nil
@neo.remove_from_index(key, value, new_node[:id])
@neo.remove_from_index(key, value, new_node)
end
end

Expand Down
Loading

0 comments on commit c3a00a0

Please sign in to comment.