Created
June 9, 2010 03:34
-
-
Save mdeiters/431000 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'json' | |
require 'rest_client' | |
def create_person(name) | |
JSON.parse RestClient.post( "http://localhost:8988/neo4jr-social/nodes", :name => name ) | |
end | |
def make_mutual_friends(node1, node2) | |
RestClient.post "http://localhost:8988/neo4jr-social/nodes/#{node1['node_id']}/relationships", :to => node2['node_id'], :type => 'friends' | |
RestClient.post "http://localhost:8988/neo4jr-social/nodes/#{node2['node_id']}/relationships", :to => node1['node_id'], :type => 'friends' | |
end | |
luke = create_person('Luke Skywalker') | |
han_solo = create_person('Han Solo') | |
chewy = create_person('Chewbacca') | |
lando = create_person('Lando') | |
akbar = create_person('General Akbar') | |
make_mutual_friends(luke, han_solo) | |
make_mutual_friends(han_solo, chewy) | |
make_mutual_friends(han_solo, lando) | |
make_mutual_friends(lando, akbar) | |
suggestions = JSON.parse RestClient.get("http://localhost:8988/neo4jr-social/nodes/#{luke['node_id']}/recommendations?type=friends") | |
friend_suggestions = suggestions.map{|n| n['name']}.join(', ') | |
puts "Luke Skywalker should become friends with #{friend_suggestions}" | |
#RESULT: | |
# Luke Skywalker should become friends with Chewbacca, Lando |
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
require 'rubygems' | |
require 'json' | |
require 'rest_client' | |
def create_person(name) | |
response = RestClient.post "http://localhost:8988/neo4jr-social/nodes", :name => name | |
JSON.parse(response) | |
end | |
def make_mutual_friends(node1, node2) | |
RestClient.post "http://localhost:8988/neo4jr-social/nodes/#{node1['node_id']}/relationships", | |
:to => node2['node_id'], | |
:type => 'friends' | |
RestClient.post "http://localhost:8988/neo4jr-social/nodes/#{node2['node_id']}/relationships", | |
:to => node1['node_id'], | |
:type => 'friends' | |
end | |
def get_degrees_of_seperation(start_node, destination_node) | |
url = "http://localhost:8988/neo4jr-social/nodes/#{start_node['node_id']}/paths?to=#{destination_node['node_id']}&type=friends&depth=3&direction=outgoing" | |
response = RestClient.get(url) | |
JSON.parse(response) | |
end | |
luke = create_person('Luke Skywalker') | |
han_solo = create_person('Han Solo') | |
chewy = create_person('Chewbacca') | |
lando = create_person('Lando') | |
akbar = create_person('General Akbar') | |
make_mutual_friends(luke, han_solo) | |
make_mutual_friends(han_solo, chewy) | |
make_mutual_friends(han_solo, lando) | |
make_mutual_friends(lando, akbar) | |
degrees = get_degrees_of_seperation(luke, lando) | |
degrees.each do |path| | |
puts path.map{|node| node['name'] || node['type']}.join(' => ') | |
end | |
#RESULT: | |
# Luke Skywalker => friends => Han Solo => friends => Lando |
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
# http://github.com/fmeyer/rgl | |
require 'rubygems' | |
require 'rgl/adjacency' | |
require 'rgl/dot' | |
graph = RGL::DirectedAdjacencyGraph.new | |
graph.add_edge 'Johnathan', 'Mark' | |
graph.add_edge 'Mark', 'Mary' | |
graph.add_edge 'Phill', 'Mary' | |
graph.add_edge 'Phill', 'Luke' | |
# Use DOT to visualize this graph: | |
# http://graphviz.org/Download_macos.php | |
graph.write_to_graphic_file('jpg') | |
`open graph.jpg` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment