From 34068abead362305a41541533b222c15c5bdcce8 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Sun, 21 Nov 2010 15:24:13 -0800 Subject: [PATCH] adding examples and get_relationship --- Gemfile.lock | 2 +- README.rdoc | 10 ++++- examples/facebook.rb | 43 ++++++++++++++++++++++ examples/linkedin.rb | 39 ++++++++++++++++++++ lib/neography/rest.rb | 6 ++- spec/integration/rest_relationship_spec.rb | 21 +++++++++++ 6 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 examples/facebook.rb create mode 100644 examples/linkedin.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1aef492..17d0604 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - neography (0.0.2) + neography (0.0.3) httparty (~> 0.6.1) json diff --git a/README.rdoc b/README.rdoc index cf90d2d..ed64ffa 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 @@ -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 diff --git a/examples/facebook.rb b/examples/facebook.rb new file mode 100644 index 0000000..c9069e4 --- /dev/null +++ b/examples/facebook.rb @@ -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 \ No newline at end of file diff --git a/examples/linkedin.rb b/examples/linkedin.rb new file mode 100644 index 0000000..6643e99 --- /dev/null +++ b/examples/linkedin.rb @@ -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 \ No newline at end of file diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 3c0556f..19d3bfe 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -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 @@ -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) diff --git a/spec/integration/rest_relationship_spec.rb b/spec/integration/rest_relationship_spec.rb index 0792962..281b60c 100644 --- a/spec/integration/rest_relationship_spec.rb +++ b/spec/integration/rest_relationship_spec.rb @@ -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