Skip to content

Commit

Permalink
adding examples and get_relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 21, 2010
1 parent 7a4edc7 commit 34068ab
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
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.2)
neography (0.0.3)
httparty (~> 0.6.1)
json

Expand Down
10 changes: 9 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ To Use:
@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
rel2 = @neo.get_relationship(rel1) # Get a relationship
@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
Expand Down Expand Up @@ -109,10 +110,17 @@ See Neo4j API for:
* {Prune Evaluator}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/StopEvaluator.html]
* {Return Filter}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/ReturnableEvaluator.html]

=== Examples

A couple of examples borrowed from Matthew Deiters's Neo4jr-social

* {Facebook}[https://github.com/maxdemarzi/neography/blob/master/examples/facebook.rb]
* {Linked In}[https://github.com/maxdemarzi/neography/blob/master/examples/linkedin.rb]

=== To Do

* More tests
* examples
* More examples
* batch import with typhoeus ?
* create proper objects for Node and Relationship

Expand Down
43 changes: 43 additions & 0 deletions examples/facebook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rubygems'
require 'neography'

@neo = Neography::Rest.new()

def create_person(name)
@neo.create_node("name" => name)
end

def make_mutual_friends(node1, node2)
@neo.create_relationship("friends", node1, node2)
@neo.create_relationship("friends", node2, node1)
end

def suggestions_for(node)
existing_friends = @neo.traverse(node,"nodes", {"order" => "breadth first",
"uniqueness" => "node global",
"relationships" => {"type"=> "friends", "direction" => "in"},
"depth" => 1})

possible_friends = @neo.traverse(node,"nodes", {"order" => "breadth first",
"uniqueness" => "node global",
"relationships" => {"type"=> "friends", "direction" => "in"},
"depth" => 2})
possible_friends - existing_friends
end

johnathan = create_person('Johnathan')
mark = create_person('Mark')
phill = create_person('Phill')
mary = create_person('Mary')
luke = create_person('Luke')

make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, mary)
make_mutual_friends(mark, phill)
make_mutual_friends(phill, mary)
make_mutual_friends(phill, luke)

puts "Johnathan should become friends with #{suggestions_for(johnathan).map{|n| n["data"]["name"]}.join(', ')}"

# RESULT
# Johnathan should become friends with Mary, Phill
39 changes: 39 additions & 0 deletions examples/linkedin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rubygems'
require 'neography'

@neo = Neography::Rest.new()

def create_person(name)
@neo.create_node("name" => name)
end

def make_mutual_friends(node1, node2)
@neo.create_relationship("friends", node1, node2)
@neo.create_relationship("friends", node2, node1)
end

def degrees_of_separation(start_node, destination_node)
paths = @neo.get_paths(start_node, destination_node, {"type"=> "friends", "direction" => "in"}, depth=4, algorithm="allSimplePaths")
paths.each do |p|
p["names"] = p["nodes"].collect {|node| @neo.get_node_properties(node, "name")["name"] }
end

end

johnathan = create_person('Johnathan')
mark = create_person('Mark')
phill = create_person('Phill')
mary = create_person('Mary')

make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, phill)
make_mutual_friends(phill, mary)
make_mutual_friends(mark, mary)

degrees_of_separation(johnathan, mary).each do |path|
puts path["names"].join(' => friends => ')
end

# RESULT
# Johnathan => friends => Mark => friends => Phill => friends => Mary
# Johnathan => friends => Mark => friends => Mary
6 changes: 5 additions & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Rest
include HTTParty
attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger

def initialize(protocol='http://', server='localhost', port=7474, log_file='neography.log', log_enabled=true)
def initialize(protocol='http://', server='localhost', port=7474, log_file='neography.log', log_enabled=false)
@protocol = protocol
@server = server
@port = port
Expand Down Expand Up @@ -84,6 +84,10 @@ def create_relationship(type, from, to, props = nil)
post("/node/#{get_id(from)}/relationships", options)
end

def get_relationship(id)
get("/relationship/#{get_id(id)}")
end

def reset_relationship_properties(id, properties)
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
put("/relationship/#{get_id(id)}/properties", options)
Expand Down
21 changes: 21 additions & 0 deletions spec/integration/rest_relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
end
end

describe "get_relationship" do
it "can get a relationship that exists" do
new_node1 = @neo.create_node
new_node2 = @neo.create_node
new_relationship = @neo.create_relationship("friends", new_node1, new_node2)
existing_relationship = @neo.get_relationship(new_relationship)
existing_relationship.should_not be_nil
existing_relationship.should have_key("self")
existing_relationship["self"].should == new_relationship["self"]
end

it "returns nil if it tries to get a relationship that does not exist" do
new_node1 = @neo.create_node
new_node2 = @neo.create_node
new_relationship = @neo.create_relationship("friends", new_node1, new_node2)
fake_relationship = new_relationship["self"].split('/').last.to_i + 1000
existing_relationship = @neo.get_relationship(fake_relationship)
existing_relationship.should be_nil
end
end

describe "set_relationship_properties" do
it "can set a relationship's properties" do
new_node1 = @neo.create_node
Expand Down

0 comments on commit 34068ab

Please sign in to comment.