-
Notifications
You must be signed in to change notification settings - Fork 135
/
linkedin.rb
39 lines (30 loc) · 1.08 KB
/
linkedin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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