neography-calamitates 1.2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. data/.gitignore +15 -0
  2. data/.project +12 -0
  3. data/.travis.yml +4 -0
  4. data/CONTRIBUTORS +18 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +19 -0
  7. data/README.md +261 -0
  8. data/Rakefile +14 -0
  9. data/examples/facebook.rb +40 -0
  10. data/examples/facebook_v2.rb +25 -0
  11. data/examples/greatest.rb +43 -0
  12. data/examples/linkedin.rb +39 -0
  13. data/examples/linkedin_v2.rb +22 -0
  14. data/examples/traversal_example1.rb +65 -0
  15. data/examples/traversal_example2.rb +54 -0
  16. data/lib/neography.rb +45 -0
  17. data/lib/neography/config.rb +52 -0
  18. data/lib/neography/connection.rb +203 -0
  19. data/lib/neography/equal.rb +21 -0
  20. data/lib/neography/errors.rb +45 -0
  21. data/lib/neography/index.rb +52 -0
  22. data/lib/neography/multi_json_parser.rb +28 -0
  23. data/lib/neography/neography.rb +10 -0
  24. data/lib/neography/node.rb +53 -0
  25. data/lib/neography/node_path.rb +29 -0
  26. data/lib/neography/node_relationship.rb +37 -0
  27. data/lib/neography/node_traverser.rb +146 -0
  28. data/lib/neography/path_traverser.rb +100 -0
  29. data/lib/neography/property.rb +61 -0
  30. data/lib/neography/property_container.rb +29 -0
  31. data/lib/neography/railtie.rb +19 -0
  32. data/lib/neography/relationship.rb +70 -0
  33. data/lib/neography/relationship_traverser.rb +80 -0
  34. data/lib/neography/rest.rb +470 -0
  35. data/lib/neography/rest/auto_indexes.rb +64 -0
  36. data/lib/neography/rest/batch.rb +277 -0
  37. data/lib/neography/rest/clean.rb +19 -0
  38. data/lib/neography/rest/cypher.rb +33 -0
  39. data/lib/neography/rest/extensions.rb +25 -0
  40. data/lib/neography/rest/gremlin.rb +24 -0
  41. data/lib/neography/rest/helpers.rb +38 -0
  42. data/lib/neography/rest/indexes.rb +100 -0
  43. data/lib/neography/rest/node_auto_indexes.rb +14 -0
  44. data/lib/neography/rest/node_indexes.rb +50 -0
  45. data/lib/neography/rest/node_labels.rb +60 -0
  46. data/lib/neography/rest/node_paths.rb +57 -0
  47. data/lib/neography/rest/node_properties.rb +11 -0
  48. data/lib/neography/rest/node_relationships.rb +42 -0
  49. data/lib/neography/rest/node_traversal.rb +81 -0
  50. data/lib/neography/rest/nodes.rb +102 -0
  51. data/lib/neography/rest/other_node_relationships.rb +48 -0
  52. data/lib/neography/rest/paths.rb +36 -0
  53. data/lib/neography/rest/properties.rb +56 -0
  54. data/lib/neography/rest/relationship_auto_indexes.rb +14 -0
  55. data/lib/neography/rest/relationship_indexes.rb +35 -0
  56. data/lib/neography/rest/relationship_properties.rb +11 -0
  57. data/lib/neography/rest/relationship_types.rb +18 -0
  58. data/lib/neography/rest/relationships.rb +23 -0
  59. data/lib/neography/rest/schema_indexes.rb +34 -0
  60. data/lib/neography/rest/transactions.rb +102 -0
  61. data/lib/neography/tasks.rb +158 -0
  62. data/lib/neography/version.rb +3 -0
  63. data/neography.gemspec +32 -0
  64. data/spec/integration/authorization_spec.rb +48 -0
  65. data/spec/integration/index_spec.rb +70 -0
  66. data/spec/integration/neography_spec.rb +10 -0
  67. data/spec/integration/node_encoding_spec.rb +71 -0
  68. data/spec/integration/node_path_spec.rb +222 -0
  69. data/spec/integration/node_relationship_spec.rb +381 -0
  70. data/spec/integration/node_spec.rb +251 -0
  71. data/spec/integration/parsing_spec.rb +13 -0
  72. data/spec/integration/performance_spec.rb +17 -0
  73. data/spec/integration/relationship_spec.rb +37 -0
  74. data/spec/integration/rest_batch_spec.rb +512 -0
  75. data/spec/integration/rest_batch_streaming_spec.rb +32 -0
  76. data/spec/integration/rest_bulk_spec.rb +106 -0
  77. data/spec/integration/rest_experimental_spec.rb +22 -0
  78. data/spec/integration/rest_gremlin_fail_spec.rb +46 -0
  79. data/spec/integration/rest_header_spec.rb +14 -0
  80. data/spec/integration/rest_index_spec.rb +468 -0
  81. data/spec/integration/rest_labels_spec.rb +128 -0
  82. data/spec/integration/rest_node_spec.rb +274 -0
  83. data/spec/integration/rest_other_node_relationship_spec.rb +137 -0
  84. data/spec/integration/rest_path_spec.rb +231 -0
  85. data/spec/integration/rest_plugin_spec.rb +177 -0
  86. data/spec/integration/rest_relationship_spec.rb +354 -0
  87. data/spec/integration/rest_relationship_types_spec.rb +18 -0
  88. data/spec/integration/rest_schema_index_spec.rb +32 -0
  89. data/spec/integration/rest_transaction_spec.rb +166 -0
  90. data/spec/integration/rest_traverse_spec.rb +149 -0
  91. data/spec/matchers.rb +33 -0
  92. data/spec/neography_spec.rb +23 -0
  93. data/spec/spec_helper.rb +45 -0
  94. data/spec/unit/config_spec.rb +46 -0
  95. data/spec/unit/connection_spec.rb +211 -0
  96. data/spec/unit/node_spec.rb +100 -0
  97. data/spec/unit/properties_spec.rb +140 -0
  98. data/spec/unit/relationship_spec.rb +118 -0
  99. data/spec/unit/rest/batch_spec.rb +243 -0
  100. data/spec/unit/rest/clean_spec.rb +17 -0
  101. data/spec/unit/rest/cypher_spec.rb +21 -0
  102. data/spec/unit/rest/extensions_spec.rb +29 -0
  103. data/spec/unit/rest/gremlin_spec.rb +26 -0
  104. data/spec/unit/rest/labels_spec.rb +73 -0
  105. data/spec/unit/rest/node_auto_indexes_spec.rb +67 -0
  106. data/spec/unit/rest/node_indexes_spec.rb +141 -0
  107. data/spec/unit/rest/node_paths_spec.rb +80 -0
  108. data/spec/unit/rest/node_properties_spec.rb +80 -0
  109. data/spec/unit/rest/node_relationships_spec.rb +78 -0
  110. data/spec/unit/rest/node_traversal_spec.rb +128 -0
  111. data/spec/unit/rest/nodes_spec.rb +188 -0
  112. data/spec/unit/rest/paths_spec.rb +69 -0
  113. data/spec/unit/rest/relationship_auto_indexes_spec.rb +67 -0
  114. data/spec/unit/rest/relationship_indexes_spec.rb +132 -0
  115. data/spec/unit/rest/relationship_properties_spec.rb +80 -0
  116. data/spec/unit/rest/relationship_types_spec.rb +16 -0
  117. data/spec/unit/rest/relationships_spec.rb +22 -0
  118. data/spec/unit/rest/schema_index_spec.rb +31 -0
  119. data/spec/unit/rest/transactions_spec.rb +44 -0
  120. metadata +372 -0
@@ -0,0 +1,102 @@
1
+ module Neography
2
+ class Rest
3
+ class Nodes
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :index, "/node"
8
+ add_path :base, "/node/:id"
9
+
10
+ def initialize(connection)
11
+ @connection = connection
12
+ end
13
+
14
+ def get(id)
15
+ @connection.get(base_path(:id => get_id(id)))
16
+ end
17
+
18
+ def get_each(*nodes)
19
+ gotten_nodes = []
20
+ Array(nodes).flatten.each do |node|
21
+ gotten_nodes << get(node)
22
+ end
23
+ gotten_nodes
24
+ end
25
+
26
+ def root
27
+ root_node = @connection.get('/')["reference_node"]
28
+ @connection.get(base_path(:id => get_id(root_node)))
29
+ end
30
+
31
+ def create(*args)
32
+ if args[0].respond_to?(:each_pair) && args[0]
33
+ create_with_attributes(args[0])
34
+ else
35
+ create_empty
36
+ end
37
+ end
38
+
39
+ def create_with_attributes(attributes)
40
+ options = {
41
+ :body => attributes.delete_if { |k, v| v.nil? }.to_json,
42
+ :headers => json_content_type
43
+ }
44
+ @connection.post(index_path, options)
45
+ end
46
+
47
+ def create_empty
48
+ @connection.post(index_path)
49
+ end
50
+
51
+ def delete(id)
52
+ @connection.delete(base_path(:id => get_id(id)))
53
+ end
54
+
55
+ def create_multiple(nodes)
56
+ nodes = Array.new(nodes) if nodes.kind_of? Fixnum
57
+ created_nodes = []
58
+ nodes.each do |node|
59
+ created_nodes << create(node)
60
+ end
61
+ created_nodes
62
+ end
63
+
64
+ def create_multiple_threaded(nodes)
65
+ nodes = Array.new(nodes) if nodes.kind_of? Fixnum
66
+
67
+ node_queue = Queue.new
68
+ thread_pool = []
69
+ responses = Queue.new
70
+
71
+ nodes.each do |node|
72
+ node_queue.push node
73
+ end
74
+
75
+ [nodes.size, @connection.max_threads].min.times do
76
+ thread_pool << Thread.new do
77
+ until node_queue.empty? do
78
+ node = node_queue.pop
79
+ if node.respond_to?(:each_pair)
80
+ responses.push( @connection.post(index_path, {
81
+ :body => node.to_json,
82
+ :headers => json_content_type
83
+ } ) )
84
+ else
85
+ responses.push( @connection.post(index_path) )
86
+ end
87
+ end
88
+ self.join
89
+ end
90
+ end
91
+
92
+ created_nodes = []
93
+
94
+ while created_nodes.size < nodes.size
95
+ created_nodes << responses.pop
96
+ end
97
+ created_nodes
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,48 @@
1
+ module Neography
2
+ class Rest
3
+ class OtherNodeRelationships
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :base, "/node/:id/traverse/relationship"
8
+
9
+ def initialize(connection)
10
+ @connection = connection
11
+ end
12
+
13
+ def get(id, other_id, direction = "all", types = [nil])
14
+
15
+ body = case parse_direction(direction)
16
+ when "all"
17
+ "position.endNode().getId() == " + get_id(other_id)
18
+ when "in"
19
+ "position.length() > 0 && position.lastRelationship().getStartNode().getId() == " + get_id(other_id)
20
+ when "out"
21
+ "position.length() > 0 && position.lastRelationship().getEndNode().getId() == " + get_id(other_id)
22
+ end
23
+
24
+ relationships = {:relationships => types.map{|row| Hash[{:type => row}].merge({:direction => parse_direction(direction)})} }
25
+
26
+ if types.first.nil?
27
+ relationships = {}
28
+ end
29
+
30
+ options = {
31
+ :body => {:order => "breadth_first",
32
+ :uniqueness => "relationship_global",
33
+ :max_depth => 1,
34
+ :return_filter => {:language => "javascript",
35
+ :body => body }
36
+ }.merge(relationships).to_json,
37
+ :headers => json_content_type
38
+ }
39
+
40
+ node_relationships = @connection.post(base_path(:id => get_id(id)), options) || []
41
+
42
+ return nil if node_relationships.empty?
43
+ node_relationships
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,36 @@
1
+ module Neography
2
+ class Rest
3
+ module Paths
4
+
5
+ def add_path(key, path)
6
+ method_name = :"#{key}_path"
7
+
8
+ metaclass = (class << self; self; end)
9
+ metaclass.instance_eval do
10
+ define_method method_name do |*attributes|
11
+ if attributes.any?
12
+ build_path(path, *attributes)
13
+ else
14
+ path
15
+ end
16
+ end
17
+ end
18
+
19
+ define_method method_name do |*attributes|
20
+ self.class.send(method_name, *attributes)
21
+ end
22
+ end
23
+
24
+ def build_path(path, attributes)
25
+ path.gsub(/:([\w_]*)/) do
26
+ encode(attributes[$1.to_sym].to_s)
27
+ end
28
+ end
29
+
30
+ def encode(value)
31
+ CGI.escape(value).gsub("+", "%20")
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ module Neography
2
+ class Rest
3
+ class Properties
4
+ include Neography::Rest::Helpers
5
+
6
+ def initialize(connection)
7
+ @connection = connection
8
+ end
9
+
10
+ def set(id, properties)
11
+ properties.each do |property, value|
12
+ options = { :body => value.to_json, :headers => json_content_type }
13
+ @connection.put(single_path(:id => get_id(id), :property => property), options)
14
+ end
15
+ end
16
+
17
+ def reset(id, properties)
18
+ options = { :body => properties.to_json, :headers => json_content_type }
19
+ @connection.put(all_path(:id => get_id(id)), options)
20
+ end
21
+
22
+ def get(id, *properties)
23
+ if properties.none?
24
+ @connection.get(all_path(:id => get_id(id)))
25
+ else
26
+ get_each(id, *properties)
27
+ end
28
+ end
29
+
30
+ def get_each(id, *properties)
31
+ retrieved_properties = properties.flatten.inject({}) do |memo, property|
32
+ value = @connection.get(single_path(:id => get_id(id), :property => property))
33
+ memo[property] = value unless value.nil?
34
+ memo
35
+ end
36
+ return nil if retrieved_properties.empty?
37
+ retrieved_properties
38
+ end
39
+
40
+ def remove(id, *properties)
41
+ if properties.none?
42
+ @connection.delete(all_path(:id => get_id(id)))
43
+ else
44
+ remove_each(id, *properties)
45
+ end
46
+ end
47
+
48
+ def remove_each(id, *properties)
49
+ properties.flatten.each do |property|
50
+ @connection.delete(single_path(:id => get_id(id), :property => property))
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ module Neography
2
+ class Rest
3
+ class RelationshipAutoIndexes < AutoIndexes
4
+ extend Neography::Rest::Paths
5
+
6
+ add_path :key_value, "/index/auto/relationship/:key/:value"
7
+ add_path :query_index, "/index/auto/relationship/?query=:query"
8
+ add_path :index_status, "/index/auto/relationship/status"
9
+ add_path :index_properties, "/index/auto/relationship/properties"
10
+ add_path :index_property, "/index/auto/relationship/properties/:property"
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ module Neography
2
+ class Rest
3
+ class RelationshipIndexes < Indexes
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :all, "/index/relationship"
8
+ add_path :base, "/index/relationship/:index"
9
+ add_path :unique, "/index/relationship/:index?unique"
10
+ add_path :id, "/index/relationship/:index/:id"
11
+ add_path :key, "/index/relationship/:index/:key/:id"
12
+ add_path :value, "/index/relationship/:index/:key/:value/:id"
13
+ add_path :key_value, "/index/relationship/:index/:key/:value"
14
+ add_path :query, "/index/relationship/:index?query=:query"
15
+
16
+ def initialize(connection)
17
+ super(connection, :relationship)
18
+ end
19
+
20
+ def create_unique(index, key, value, type, from, to)
21
+ body = {
22
+ :key => key,
23
+ :value => value,
24
+ :type => type,
25
+ :start => @connection.configuration + "/node/#{get_id(from)}",
26
+ :end => @connection.configuration + "/node/#{get_id(to)}"
27
+ }
28
+ options = { :body => body.to_json, :headers => json_content_type }
29
+
30
+ @connection.post(unique_path(:index => index), options)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module Neography
2
+ class Rest
3
+ class RelationshipProperties < Properties
4
+ extend Neography::Rest::Paths
5
+
6
+ add_path :all, "/relationship/:id/properties"
7
+ add_path :single, "/relationship/:id/properties/:property"
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module Neography
2
+ class Rest
3
+ class RelationshipTypes
4
+ extend Neography::Rest::Paths
5
+
6
+ add_path :all, "/relationship/types"
7
+
8
+ def initialize(connection)
9
+ @connection = connection
10
+ end
11
+
12
+ def list
13
+ @connection.get(all_path)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Neography
2
+ class Rest
3
+ class Relationships
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :base, "/relationship/:id"
8
+
9
+ def initialize(connection)
10
+ @connection = connection
11
+ end
12
+
13
+ def get(id)
14
+ @connection.get(base_path(:id => get_id(id)))
15
+ end
16
+
17
+ def delete(id)
18
+ @connection.delete(base_path(:id => get_id(id)))
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ module Neography
2
+ class Rest
3
+ class SchemaIndexes
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :base, "/schema/index/:label"
8
+ add_path :drop, "/schema/index/:label/:index"
9
+
10
+ def initialize(connection)
11
+ @connection = connection
12
+ end
13
+
14
+ def list(label)
15
+ @connection.get(base_path(:label => label))
16
+ end
17
+
18
+ def drop(label, index)
19
+ @connection.delete(drop_path(:label => label, :index => index))
20
+ end
21
+
22
+ def create(label, keys = [])
23
+ options = {
24
+ :body => (
25
+ { :property_keys => keys
26
+ }
27
+ ).to_json,
28
+ :headers => json_content_type
29
+ }
30
+ @connection.post(base_path(:label => label), options)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,102 @@
1
+ module Neography
2
+ class Rest
3
+ class Transactions
4
+ extend Neography::Rest::Paths
5
+ include Neography::Rest::Helpers
6
+
7
+ add_path :base, "/transaction"
8
+ add_path :tx, "/transaction/:id"
9
+ add_path :commit, "/transaction/:id/commit"
10
+
11
+ def initialize(connection)
12
+ @connection = connection
13
+ end
14
+
15
+ def begin(statements = [], commit = "")
16
+ options = {
17
+ :body => (
18
+ convert_cypher(statements)
19
+ ).to_json,
20
+ :headers => json_content_type
21
+ }
22
+ @connection.post(base_path + commit, options)
23
+ end
24
+
25
+ def add(tx, statements = [])
26
+ options = {
27
+ :body => (
28
+ convert_cypher(statements)
29
+ ).to_json,
30
+ :headers => json_content_type
31
+ }
32
+ @connection.post(tx_path(:id => get_id(tx)), options)
33
+ end
34
+
35
+ def commit(tx, statements = [])
36
+ options = {
37
+ :body => (
38
+ convert_cypher(statements)
39
+ ).to_json,
40
+ :headers => json_content_type
41
+ }
42
+ @connection.post(commit_path(:id => get_id(tx)), options)
43
+ end
44
+
45
+ def rollback(tx)
46
+ @connection.delete(tx_path(:id => get_id(tx)))
47
+ end
48
+
49
+ private
50
+
51
+ def get_id(tx)
52
+ return tx if tx.is_a?(Integer)
53
+ return tx.split("/")[-2] if tx.is_a?(String)
54
+ return tx["commit"].split("/")[-2] if tx["commit"]
55
+ raise NeographyError.new("Could not determine transaction id", nil, tx)
56
+ end
57
+
58
+ def convert_cypher(statements)
59
+ array = []
60
+ query = nil
61
+ parameters = nil
62
+ Array(statements).each do |statement|
63
+ case
64
+ when query && parameters && statement.is_a?(Array)
65
+ then
66
+ array << {:statement => query, :parameters => parameters, :resultDataContents => statement }
67
+ query = nil
68
+ parameters = nil
69
+ when query && parameters && statement.is_a?(String)
70
+ then
71
+ array << {:statement => query, :parameters => parameters}
72
+ query = statement
73
+ parameters = nil
74
+ when query && statement.is_a?(Hash)
75
+ then
76
+ parameters = statement
77
+ when query && statement.is_a?(Array)
78
+ then
79
+ array << {:statement => query, :resultDataContents => statement }
80
+ query = nil
81
+ parameters = nil
82
+ else
83
+ query = statement
84
+ end
85
+
86
+ end
87
+
88
+ if query && parameters
89
+ array << {:statement => query, :parameters => parameters}
90
+ query = nil
91
+ end
92
+
93
+ if query
94
+ array << {:statement => query}
95
+ end
96
+
97
+ { :statements => array }
98
+ end
99
+
100
+ end
101
+ end
102
+ end