Skip to content

Commit 34068ab

Browse files
committed
adding examples and get_relationship
1 parent 7a4edc7 commit 34068ab

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
neography (0.0.2)
4+
neography (0.0.3)
55
httparty (~> 0.6.1)
66
json
77

README.rdoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ To Use:
6262
@neo.remove_node_properties(node1, ["weight","age"]) # Remove multiple properties of a node
6363

6464
rel1 = @neo.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2
65+
rel2 = @neo.get_relationship(rel1) # Get a relationship
6566
@neo.get_node_relationships(node1) # Get all relationships
6667
@neo.get_node_relationships(node1, "in") # Get only incoming relationships
6768
@neo.get_node_relationships(node1, "all", "enemies") # Get all relationships of type enemies
@@ -109,10 +110,17 @@ See Neo4j API for:
109110
* {Prune Evaluator}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/StopEvaluator.html]
110111
* {Return Filter}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/ReturnableEvaluator.html]
111112

113+
=== Examples
114+
115+
A couple of examples borrowed from Matthew Deiters's Neo4jr-social
116+
117+
* {Facebook}[https://github.com/maxdemarzi/neography/blob/master/examples/facebook.rb]
118+
* {Linked In}[https://github.com/maxdemarzi/neography/blob/master/examples/linkedin.rb]
119+
112120
=== To Do
113121

114122
* More tests
115-
* examples
123+
* More examples
116124
* batch import with typhoeus ?
117125
* create proper objects for Node and Relationship
118126

examples/facebook.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'rubygems'
2+
require 'neography'
3+
4+
@neo = Neography::Rest.new()
5+
6+
def create_person(name)
7+
@neo.create_node("name" => name)
8+
end
9+
10+
def make_mutual_friends(node1, node2)
11+
@neo.create_relationship("friends", node1, node2)
12+
@neo.create_relationship("friends", node2, node1)
13+
end
14+
15+
def suggestions_for(node)
16+
existing_friends = @neo.traverse(node,"nodes", {"order" => "breadth first",
17+
"uniqueness" => "node global",
18+
"relationships" => {"type"=> "friends", "direction" => "in"},
19+
"depth" => 1})
20+
21+
possible_friends = @neo.traverse(node,"nodes", {"order" => "breadth first",
22+
"uniqueness" => "node global",
23+
"relationships" => {"type"=> "friends", "direction" => "in"},
24+
"depth" => 2})
25+
possible_friends - existing_friends
26+
end
27+
28+
johnathan = create_person('Johnathan')
29+
mark = create_person('Mark')
30+
phill = create_person('Phill')
31+
mary = create_person('Mary')
32+
luke = create_person('Luke')
33+
34+
make_mutual_friends(johnathan, mark)
35+
make_mutual_friends(mark, mary)
36+
make_mutual_friends(mark, phill)
37+
make_mutual_friends(phill, mary)
38+
make_mutual_friends(phill, luke)
39+
40+
puts "Johnathan should become friends with #{suggestions_for(johnathan).map{|n| n["data"]["name"]}.join(', ')}"
41+
42+
# RESULT
43+
# Johnathan should become friends with Mary, Phill

examples/linkedin.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'rubygems'
2+
require 'neography'
3+
4+
@neo = Neography::Rest.new()
5+
6+
def create_person(name)
7+
@neo.create_node("name" => name)
8+
end
9+
10+
def make_mutual_friends(node1, node2)
11+
@neo.create_relationship("friends", node1, node2)
12+
@neo.create_relationship("friends", node2, node1)
13+
end
14+
15+
def degrees_of_separation(start_node, destination_node)
16+
paths = @neo.get_paths(start_node, destination_node, {"type"=> "friends", "direction" => "in"}, depth=4, algorithm="allSimplePaths")
17+
paths.each do |p|
18+
p["names"] = p["nodes"].collect {|node| @neo.get_node_properties(node, "name")["name"] }
19+
end
20+
21+
end
22+
23+
johnathan = create_person('Johnathan')
24+
mark = create_person('Mark')
25+
phill = create_person('Phill')
26+
mary = create_person('Mary')
27+
28+
make_mutual_friends(johnathan, mark)
29+
make_mutual_friends(mark, phill)
30+
make_mutual_friends(phill, mary)
31+
make_mutual_friends(mark, mary)
32+
33+
degrees_of_separation(johnathan, mary).each do |path|
34+
puts path["names"].join(' => friends => ')
35+
end
36+
37+
# RESULT
38+
# Johnathan => friends => Mark => friends => Phill => friends => Mary
39+
# Johnathan => friends => Mark => friends => Mary

lib/neography/rest.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Rest
33
include HTTParty
44
attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger
55

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

87+
def get_relationship(id)
88+
get("/relationship/#{get_id(id)}")
89+
end
90+
8791
def reset_relationship_properties(id, properties)
8892
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
8993
put("/relationship/#{get_id(id)}/properties", options)

spec/integration/rest_relationship_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@
3232
end
3333
end
3434

35+
describe "get_relationship" do
36+
it "can get a relationship that exists" do
37+
new_node1 = @neo.create_node
38+
new_node2 = @neo.create_node
39+
new_relationship = @neo.create_relationship("friends", new_node1, new_node2)
40+
existing_relationship = @neo.get_relationship(new_relationship)
41+
existing_relationship.should_not be_nil
42+
existing_relationship.should have_key("self")
43+
existing_relationship["self"].should == new_relationship["self"]
44+
end
45+
46+
it "returns nil if it tries to get a relationship that does not exist" do
47+
new_node1 = @neo.create_node
48+
new_node2 = @neo.create_node
49+
new_relationship = @neo.create_relationship("friends", new_node1, new_node2)
50+
fake_relationship = new_relationship["self"].split('/').last.to_i + 1000
51+
existing_relationship = @neo.get_relationship(fake_relationship)
52+
existing_relationship.should be_nil
53+
end
54+
end
55+
3556
describe "set_relationship_properties" do
3657
it "can set a relationship's properties" do
3758
new_node1 = @neo.create_node

0 commit comments

Comments
 (0)