Skip to content

Commit

Permalink
adding get_path
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 18, 2010
1 parent 2bedc62 commit 93e4fbd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and

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

Neography::Rest.reset_relationship_properties(id, {"age" => 31}) # Reset a relationship's properties
Expand Down
44 changes: 35 additions & 9 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,7 @@ def delete_relationship(id)
end

def get_node_relationships(id, dir=nil, types=nil)
case dir
when :incoming, "incoming"
dir = "in"
when :outgoing, "outgoing"
dir = "out"
else
dir = "all"
end
dir = get_dir(dir)

if types.nil?
node_relationships = rescue_ij { get("/node/#{id}/relationships/#{dir}") } || Array.new
Expand Down Expand Up @@ -152,9 +145,19 @@ def get_index(key, value)
index
end

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

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

private

# Rescue from Invalid JSON error thrown by Crack Gem
# Rescue from Invalid JSON error thrown by Crack Gem

def rescue_ij(&block)
begin
Expand All @@ -166,6 +169,29 @@ def rescue_ij(&block)
response
end

def get_dir(dir)
case dir
when :incoming, "incoming", :in, "in"
"in"
when :outgoing, "outgoing", :out, "out"
"out"
else
"all"
end
end

def get_algorithm(algorithm)
case algorithm
when :shortest, "shortest", :shortestPath, "shortestPath", :short, "short"
"shortestPath"
when :allSimplePaths, "allSimplePaths", :simple, "simple"
"allSimplePaths"
else
"allPaths"
end
end


end

end
Expand Down
59 changes: 56 additions & 3 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "outgoing")
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "out")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node1[:id]
relationships[0]["end"].split('/').last.should == new_node2[:id]
Expand All @@ -540,7 +540,7 @@
new_node3[:id] = new_node3["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node3[:id], new_node1[:id], {"since" => '10-2-2010', "met" => "work"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming")
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node3[:id]
relationships[0]["end"].split('/').last.should == new_node1[:id]
Expand Down Expand Up @@ -581,7 +581,7 @@
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node1[:id], new_node3[:id], {"since" => '10-2-2010', "met" => "work"})
new_relationship = Neography::Rest.create_relationship("enemies", new_node4[:id], new_node1[:id], {"since" => '10-3-2010', "met" => "gym"})
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "incoming", "enemies")
relationships = Neography::Rest.get_node_relationships(new_node1[:id], "in", "enemies")
relationships.should_not be_nil
relationships[0]["start"].split('/').last.should == new_node4[:id]
relationships[0]["end"].split('/').last.should == new_node1[:id]
Expand Down Expand Up @@ -646,5 +646,58 @@
end
end

describe "get path" do
it "can get a path between two nodes" do
new_node1 = Neography::Rest.create_node
new_node1[:id] = new_node1["self"].split('/').last
new_node2 = Neography::Rest.create_node
new_node2[:id] = new_node2["self"].split('/').last
new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2005', "met" => "college"})
path = Neography::Rest.get_path(new_node1[:id], new_node2[:id], {"type"=> "friends", "direction" => "out"})
path["start"].should == new_node1["self"]
path["end"].should == new_node2["self"]
path["nodes"].should == [new_node1["self"], new_node2["self"]]
end

it "can get the shortest path between two nodes" do
pending
end

it "can get a simple path between two nodes" do
pending
end

it "can get a path between two nodes of max depth 3" do
pending
end

it "can get a path between two nodes of a specific relationship" do
pending
end
end

describe "get paths" do
it "can get the shortest paths between two nodes" do
pending
end

it "can get all paths between two nodes" do
pending
end

it "can get all simple paths between two nodes" do
pending
end

it "can get paths between two nodes of max depth 3" do
pending
end

it "can get paths between two nodes of a specific relationship" do
pending
end

end


end

0 comments on commit 93e4fbd

Please sign in to comment.