neography 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- neography (0.0.2)
4
+ neography (0.0.3)
5
5
  httparty (~> 0.6.1)
6
6
  json
7
7
 
@@ -62,6 +62,7 @@ To Use:
62
62
  @neo.remove_node_properties(node1, ["weight","age"]) # Remove multiple properties of a node
63
63
 
64
64
  rel1 = @neo.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2
65
+ rel2 = @neo.get_relationship(rel1) # Get a relationship
65
66
  @neo.get_node_relationships(node1) # Get all relationships
66
67
  @neo.get_node_relationships(node1, "in") # Get only incoming relationships
67
68
  @neo.get_node_relationships(node1, "all", "enemies") # Get all relationships of type enemies
@@ -109,10 +110,17 @@ See Neo4j API for:
109
110
  * {Prune Evaluator}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/StopEvaluator.html]
110
111
  * {Return Filter}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/ReturnableEvaluator.html]
111
112
 
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
+
112
120
  === To Do
113
121
 
114
122
  * More tests
115
- * examples
123
+ * More examples
116
124
  * batch import with typhoeus ?
117
125
  * create proper objects for Node and Relationship
118
126
 
@@ -0,0 +1,40 @@
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
+ @neo.traverse(node,"nodes", {"order" => "breadth first",
17
+ "uniqueness" => "node global",
18
+ "relationships" => {"type"=> "friends", "direction" => "in"},
19
+ "return filter" => {
20
+ "language" => "javascript",
21
+ "body" => "position.length() == 2;"},
22
+ "depth" => 2})
23
+ end
24
+
25
+ johnathan = create_person('Johnathan')
26
+ mark = create_person('Mark')
27
+ phill = create_person('Phill')
28
+ mary = create_person('Mary')
29
+ luke = create_person('Luke')
30
+
31
+ make_mutual_friends(johnathan, mark)
32
+ make_mutual_friends(mark, mary)
33
+ make_mutual_friends(mark, phill)
34
+ make_mutual_friends(phill, mary)
35
+ make_mutual_friends(phill, luke)
36
+
37
+ puts "Johnathan should become friends with #{suggestions_for(johnathan).map{|n| n["data"]["name"]}.join(', ')}"
38
+
39
+ # RESULT
40
+ # Johnathan should become friends with Mary, Phill
@@ -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
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'neography'
3
+
4
+ @neo = Neography::Rest.new
5
+
6
+ def create_node(level, type)
7
+ @neo.create_node("NODE_LEVEL" => level, "TYPE" => type)
8
+ end
9
+
10
+ def create_other_node(type)
11
+ @neo.create_node("TYPE" => type)
12
+ end
13
+
14
+
15
+ def get_nodes_by_level(level, node)
16
+ starting_id = node["self"].split('/').last
17
+
18
+ @neo.traverse(node,"nodes", {"order" => "breadth first",
19
+ "uniqueness" => "node global",
20
+ "relationships" => {"type"=> "linked", "direction" => "out"},
21
+ "prune evaluator" => {
22
+ "language" => "javascript",
23
+ "body" => "position.startNode().hasProperty('NODE_LEVEL')
24
+ && position.startNode().getProperty('NODE_LEVEL')==5
25
+ && position.startNode().getId()!=#{starting_id};"},
26
+ "return filter" => {
27
+ "language" => "javascript",
28
+ "body" => "position.endNode().hasProperty('NODE_LEVEL') && position.endNode().getProperty('NODE_LEVEL')==5;"}})
29
+ end
30
+
31
+ node1 = create_node(5, "N")
32
+ node2 = create_node(5, "N")
33
+ node3 = create_node(5, "N")
34
+ node4 = create_node(5, "N")
35
+ node5 = create_node(5, "N")
36
+ node6 = create_node(5, "N")
37
+ node7 = create_node(5, "N")
38
+
39
+ node8 = create_other_node("Y")
40
+ node9 = create_other_node("Y")
41
+ node10 = create_other_node("Y")
42
+
43
+ node11 = create_node(6, "N")
44
+ node12 = create_node(7, "N")
45
+ node13 = create_node(8, "N")
46
+
47
+
48
+ @neo.create_relationship("linked", node1, node2)
49
+ @neo.create_relationship("linked", node2, node3)
50
+ @neo.create_relationship("linked", node3, node4)
51
+ @neo.create_relationship("linked", node4, node5)
52
+ @neo.create_relationship("linked", node5, node6)
53
+ @neo.create_relationship("linked", node6, node7)
54
+
55
+ @neo.create_relationship("linked", node2, node8)
56
+ @neo.create_relationship("linked", node3, node9)
57
+ @neo.create_relationship("linked", node4, node10)
58
+
59
+ @neo.create_relationship("linked", node5, node11)
60
+ @neo.create_relationship("linked", node6, node12)
61
+ @neo.create_relationship("linked", node7, node13)
62
+
63
+ puts "The node levels returned are #{get_nodes_by_level(5, node1).map{|n| n["data"]["NODE_LEVEL"]}.join(', ')}"
64
+
65
+ # The node levels returned are 5, 5, 5, 5, 5, 5, 5
@@ -3,7 +3,7 @@ module Neography
3
3
  include HTTParty
4
4
  attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger
5
5
 
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)
7
7
  @protocol = protocol
8
8
  @server = server
9
9
  @port = port
@@ -84,6 +84,10 @@ module Neography
84
84
  post("/node/#{get_id(from)}/relationships", options)
85
85
  end
86
86
 
87
+ def get_relationship(id)
88
+ get("/relationship/#{get_id(id)}")
89
+ end
90
+
87
91
  def reset_relationship_properties(id, properties)
88
92
  options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
89
93
  put("/relationship/#{get_id(id)}/properties", options)
@@ -298,6 +302,7 @@ module Neography
298
302
  end
299
303
 
300
304
  def get_depth(depth)
305
+ return nil if depth.nil?
301
306
  return 1 if depth.to_i == 0
302
307
  depth.to_i
303
308
  end
@@ -1,3 +1,3 @@
1
1
  module Neography
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -32,6 +32,27 @@ describe Neography::Rest do
32
32
  end
33
33
  end
34
34
 
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
+
35
56
  describe "set_relationship_properties" do
36
57
  it "can set a relationship's properties" do
37
58
  new_node1 = @neo.create_node
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Max De Marzi
@@ -108,6 +108,9 @@ files:
108
108
  - LICENSE
109
109
  - README.rdoc
110
110
  - Rakefile
111
+ - examples/facebook.rb
112
+ - examples/linkedin.rb
113
+ - examples/traversal_example1.rb
111
114
  - lib/neography.rb
112
115
  - lib/neography/rest.rb
113
116
  - lib/neography/version.rb