-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding examples and get_relationship
- Loading branch information
1 parent
7a4edc7
commit 34068ab
Showing
6 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters