arangorb 1.2.0 → 1.3.0

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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/ArangoRB.gemspec +18 -18
  3. data/Gemfile +8 -7
  4. data/LICENSE +21 -21
  5. data/README.md +906 -867
  6. data/lib/ArangoRB_AQL.rb +181 -160
  7. data/lib/ArangoRB_Cache.rb +174 -174
  8. data/lib/ArangoRB_Col.rb +526 -499
  9. data/lib/ArangoRB_DB.rb +363 -339
  10. data/lib/ArangoRB_Doc.rb +319 -298
  11. data/lib/ArangoRB_Edg.rb +184 -169
  12. data/lib/ArangoRB_Gra.rb +201 -180
  13. data/lib/ArangoRB_Index.rb +135 -115
  14. data/lib/ArangoRB_Replication.rb +261 -0
  15. data/lib/ArangoRB_Ser.rb +446 -441
  16. data/lib/ArangoRB_Task.rb +129 -113
  17. data/lib/ArangoRB_Tra.rb +169 -142
  18. data/lib/ArangoRB_Tran.rb +68 -53
  19. data/lib/ArangoRB_User.rb +149 -136
  20. data/lib/ArangoRB_Ver.rb +162 -147
  21. data/lib/arangorb.rb +16 -15
  22. data/spec/arangoRB_helper.rb +4 -4
  23. data/spec/arangoRestart_helper.rb +14 -14
  24. data/spec/lib/0.1.0/arangoAQL_helper.rb +64 -64
  25. data/spec/lib/0.1.0/arangoC_helper.rb +170 -170
  26. data/spec/lib/0.1.0/arangoDB_helper.rb +119 -119
  27. data/spec/lib/0.1.0/arangoDoc_helper.rb +79 -79
  28. data/spec/lib/0.1.0/arangoE_helper.rb +50 -50
  29. data/spec/lib/0.1.0/arangoG_helper.rb +78 -78
  30. data/spec/lib/0.1.0/arangoS_helper.rb +37 -37
  31. data/spec/lib/0.1.0/arangoT_helper.rb +48 -48
  32. data/spec/lib/0.1.0/arangoV_helper.rb +65 -65
  33. data/spec/lib/1.0.0/arangoC_helper.rb +73 -73
  34. data/spec/lib/1.0.0/arangoDB_helper.rb +48 -48
  35. data/spec/lib/1.0.0/arangoI_helper.rb +43 -43
  36. data/spec/lib/1.0.0/arangoS_helper.rb +192 -192
  37. data/spec/lib/1.0.0/arangoTa_helper.rb +49 -49
  38. data/spec/lib/1.0.0/arangoTr_helper.rb +15 -15
  39. data/spec/lib/1.0.0/arangoU_helper.rb +72 -72
  40. data/spec/lib/1.1.0/arangoRB_helper.rb +144 -144
  41. data/spec/lib/1.1.0/arangoRB_walks_helper.rb +19 -19
  42. data/spec/lib/1.2.0/arangoCache_helper.rb +66 -66
  43. data/spec/lib/1.3.0/arangoHash_helper.rb +30 -0
  44. data/spec/lib/arangoRB_0.1.0_helper.rb +9 -9
  45. data/spec/lib/arangoRB_1.0.0_helper.rb +6 -6
  46. data/spec/lib/arangoRB_1.1.0_helper.rb +2 -2
  47. data/spec/lib/arangoRB_1.2.0_helper.rb +2 -1
  48. data/spec/spec_helper.rb +41 -41
  49. metadata +6 -5
@@ -1,169 +1,184 @@
1
- # === GRAPH EDGE ===
2
-
3
- class ArangoEdge < ArangoDocument
4
- def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}, from: nil, to: nil) # TESTED
5
- if collection.is_a?(String)
6
- @collection = collection
7
- elsif collection.is_a?(ArangoCollection)
8
- @collection = collection.collection
9
- else
10
- raise "collection should be a String or an ArangoCollection instance, not a #{collection.class}"
11
- end
12
-
13
- if graph.is_a?(String)
14
- @graph = graph
15
- elsif graph.is_a?(ArangoGraph)
16
- @graph = graph.graph
17
- else
18
- raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
19
- end
20
-
21
- if database.is_a?(String)
22
- @database = database
23
- elsif database.is_a?(ArangoDatabase)
24
- @database = database.database
25
- else
26
- raise "database should be a String or an ArangoDatabase instance, not a #{database.class}"
27
- end
28
-
29
- if key.is_a?(String) || key.nil?
30
- @key = key
31
- unless key.nil?
32
- body["_key"] = @key
33
- @id = "#{@collection}/#{@key}"
34
- end
35
- elsif key.is_a?(ArangoDocument)
36
- @key = key.key
37
- @id = key.id
38
- else
39
- raise "key should be a String, not a #{key.class}"
40
- end
41
-
42
- if body.is_a?(Hash)
43
- @body = body
44
- else
45
- raise "body should be a Hash, not a #{body.class}"
46
- end
47
-
48
- if from.is_a?(String)
49
- @body["_from"] = from
50
- elsif from.is_a?(ArangoDocument)
51
- @body["_from"] = from.id
52
- elsif from.nil?
53
- else
54
- raise "from should be a String or an ArangoDocument instance, not a #{from.class}"
55
- end
56
-
57
- if to.is_a?(String)
58
- @body["_to"] = to
59
- elsif to.is_a?(ArangoDocument)
60
- @body["_to"] = to.id
61
- elsif to.nil?
62
- else
63
- raise "to should be a String or an ArangoDocument instance, not a #{to.class}"
64
- end
65
-
66
- @idCache = "EDG_#{@id}"
67
- end
68
-
69
- attr_reader :key, :id, :body, :idCache
70
-
71
- # === RETRIEVE ===
72
-
73
- def graph
74
- ArangoGraph.new(graph: @graph, database: @database)
75
- end
76
-
77
- # === GET ===
78
-
79
- def retrieve # TESTED
80
- result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", @@request)
81
- return result.headers["x-arango-async-id"] if @@async == "store"
82
- result = result.parsed_response
83
- if @@verbose
84
- @body = result["edge"] unless result["error"]
85
- result
86
- else
87
- return result["errorMessage"] if result["error"]
88
- @body = result["edge"]
89
- self
90
- end
91
- end
92
-
93
- # === POST ====
94
-
95
- def create(body: {}, from: @body["_from"], to: @body["_to"], waitForSync: nil) # TESTED
96
- query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
97
- body["_key"] = @key if body["_key"].nil? && !@key.nil?
98
- body["_from"] = from.is_a?(String) ? from : from.id
99
- body["_to"] = to.is_a?(String) ? to : to.id
100
- request = @@request.merge({ :body => body.to_json, :query => query })
101
- result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@collection}", request)
102
- return_result result: result, body: body
103
- end
104
- alias create_document create
105
- alias create_vertex create
106
-
107
- # === MODIFY ===
108
-
109
- def replace(body: {}, waitForSync: nil) # TESTED
110
- query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
111
- request = @@request.merge({ :body => body.to_json, :query => query })
112
- result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
113
- return_result result: result, body: body
114
- end
115
-
116
- def update(body: {}, waitForSync: nil, keepNull: nil) # TESTED
117
- query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
118
- request = @@request.merge({ :body => body.to_json, :query => query })
119
- result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
120
- return result.headers["x-arango-async-id"] if @@async == "store"
121
- result = result.parsed_response
122
- if @@verbose
123
- unless result["error"]
124
- @key = result["_key"]
125
- @id = "#{@collection}/#{@key}"
126
- @body = body
127
- end
128
- result
129
- else
130
- return result["errorMessage"] if result["error"]
131
- @key = result["edge"]["_key"]
132
- @id = "#{@collection}/#{@key}"
133
- @body = @body.merge(body)
134
- @body = @body.merge(result["edge"])
135
- self
136
- end
137
- end
138
-
139
- # === DELETE ===
140
-
141
- def destroy(body: nil, waitForSync: nil) # TESTED
142
- query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
143
- request = @@request.merge({ :query => query })
144
- result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
145
- return_result result: result, caseTrue: true
146
- end
147
-
148
- # === UTILITY ===
149
-
150
- def return_result(result:, body: {}, caseTrue: false)
151
- return result.headers["x-arango-async-id"] if @@async == "store"
152
- result = result.parsed_response
153
- if @@verbose
154
- unless result["error"]
155
- @key = result["edge"]["_key"]
156
- @id = "#{@collection}/#{@key}"
157
- @body = body
158
- end
159
- result
160
- else
161
- return result["errorMessage"] if result["error"]
162
- return true if caseTrue
163
- @key = result["edge"]["_key"]
164
- @id = "#{@collection}/#{@key}"
165
- @body = body
166
- self
167
- end
168
- end
169
- end
1
+ # === GRAPH EDGE ===
2
+
3
+ class ArangoEdge < ArangoDocument
4
+ def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}, from: nil, to: nil) # TESTED
5
+ if collection.is_a?(String)
6
+ @collection = collection
7
+ elsif collection.is_a?(ArangoCollection)
8
+ @collection = collection.collection
9
+ else
10
+ raise "collection should be a String or an ArangoCollection instance, not a #{collection.class}"
11
+ end
12
+
13
+ if graph.is_a?(String)
14
+ @graph = graph
15
+ elsif graph.is_a?(ArangoGraph)
16
+ @graph = graph.graph
17
+ else
18
+ raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
19
+ end
20
+
21
+ if database.is_a?(String)
22
+ @database = database
23
+ elsif database.is_a?(ArangoDatabase)
24
+ @database = database.database
25
+ else
26
+ raise "database should be a String or an ArangoDatabase instance, not a #{database.class}"
27
+ end
28
+
29
+ if key.is_a?(String) || key.nil?
30
+ @key = key
31
+ unless key.nil?
32
+ body["_key"] = @key
33
+ @id = "#{@collection}/#{@key}"
34
+ end
35
+ elsif key.is_a?(ArangoDocument)
36
+ @key = key.key
37
+ @id = key.id
38
+ else
39
+ raise "key should be a String, not a #{key.class}"
40
+ end
41
+
42
+ if body.is_a?(Hash)
43
+ @body = body
44
+ else
45
+ raise "body should be a Hash, not a #{body.class}"
46
+ end
47
+
48
+ if from.is_a?(String)
49
+ @body["_from"] = from
50
+ elsif from.is_a?(ArangoDocument)
51
+ @body["_from"] = from.id
52
+ elsif from.nil?
53
+ else
54
+ raise "from should be a String or an ArangoDocument instance, not a #{from.class}"
55
+ end
56
+
57
+ if to.is_a?(String)
58
+ @body["_to"] = to
59
+ elsif to.is_a?(ArangoDocument)
60
+ @body["_to"] = to.id
61
+ elsif to.nil?
62
+ else
63
+ raise "to should be a String or an ArangoDocument instance, not a #{to.class}"
64
+ end
65
+
66
+ @idCache = "EDG_#{@id}"
67
+ end
68
+
69
+ attr_reader :key, :id, :body, :idCache
70
+
71
+ # === RETRIEVE ===
72
+
73
+ def to_hash
74
+ {
75
+ "key" => @key,
76
+ "id" => @id,
77
+ "collection" => @collection,
78
+ "database" => @database,
79
+ "body" => @body,
80
+ "idCache" => @idCache
81
+ }.delete_if{|k,v| v.nil?}
82
+ end
83
+ alias to_h to_hash
84
+
85
+ def graph
86
+ ArangoGraph.new(graph: @graph, database: @database)
87
+ end
88
+
89
+ # === GET ===
90
+
91
+ def retrieve # TESTED
92
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", @@request)
93
+ return result.headers["x-arango-async-id"] if @@async == "store"
94
+ return true if @@async
95
+ result = result.parsed_response
96
+ if @@verbose
97
+ @body = result["edge"] unless result["error"]
98
+ result
99
+ else
100
+ return result["errorMessage"] if result["error"]
101
+ @body = result["edge"]
102
+ self
103
+ end
104
+ end
105
+
106
+ # === POST ====
107
+
108
+ def create(body: {}, from: @body["_from"], to: @body["_to"], waitForSync: nil) # TESTED
109
+ query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
110
+ body["_key"] = @key if body["_key"].nil? && !@key.nil?
111
+ body["_from"] = from.is_a?(String) ? from : from.id
112
+ body["_to"] = to.is_a?(String) ? to : to.id
113
+ request = @@request.merge({ :body => body.to_json, :query => query })
114
+ result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@collection}", request)
115
+ return_result result: result, body: body
116
+ end
117
+ alias create_document create
118
+ alias create_vertex create
119
+
120
+ # === MODIFY ===
121
+
122
+ def replace(body: {}, waitForSync: nil) # TESTED
123
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
124
+ request = @@request.merge({ :body => body.to_json, :query => query })
125
+ result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
126
+ return_result result: result, body: body
127
+ end
128
+
129
+ def update(body: {}, waitForSync: nil, keepNull: nil) # TESTED
130
+ query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
131
+ request = @@request.merge({ :body => body.to_json, :query => query })
132
+ result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
133
+ return result.headers["x-arango-async-id"] if @@async == "store"
134
+ return true if @@async
135
+ result = result.parsed_response
136
+ if @@verbose
137
+ unless result["error"]
138
+ @key = result["_key"]
139
+ @id = "#{@collection}/#{@key}"
140
+ @body = body
141
+ end
142
+ result
143
+ else
144
+ return result["errorMessage"] if result["error"]
145
+ @key = result["edge"]["_key"]
146
+ @id = "#{@collection}/#{@key}"
147
+ @body = @body.merge(body)
148
+ @body = @body.merge(result["edge"])
149
+ self
150
+ end
151
+ end
152
+
153
+ # === DELETE ===
154
+
155
+ def destroy(body: nil, waitForSync: nil) # TESTED
156
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
157
+ request = @@request.merge({ :query => query })
158
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{@id}", request)
159
+ return_result result: result, caseTrue: true
160
+ end
161
+
162
+ # === UTILITY ===
163
+
164
+ def return_result(result:, body: {}, caseTrue: false)
165
+ return result.headers["x-arango-async-id"] if @@async == "store"
166
+ return true if @@async
167
+ result = result.parsed_response
168
+ if @@verbose
169
+ unless result["error"]
170
+ @key = result["edge"]["_key"]
171
+ @id = "#{@collection}/#{@key}"
172
+ @body = body
173
+ end
174
+ result
175
+ else
176
+ return result["errorMessage"] if result["error"]
177
+ return true if caseTrue
178
+ @key = result["edge"]["_key"]
179
+ @id = "#{@collection}/#{@key}"
180
+ @body = body
181
+ self
182
+ end
183
+ end
184
+ end
@@ -1,180 +1,201 @@
1
- # ==== GRAPH ====
2
-
3
- class ArangoGraph < ArangoServer
4
- def initialize(graph: @@graph, database: @@database, edgeDefinitions: [], orphanCollections: []) # TESTED
5
- if database.is_a?(String)
6
- @database = database
7
- elsif database.is_a?(ArangoDatabase)
8
- @database = database.database
9
- else
10
- raise "database should be a String or an ArangoDatabase instance, not a #{database.class}"
11
- end
12
-
13
- if graph.is_a?(String)
14
- @graph = graph
15
- elsif database.is_a?(ArangoGraph)
16
- @graph = graph.graph
17
- else
18
- raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
19
- end
20
-
21
- if edgeDefinitions.is_a?(Array)
22
- @edgeDefinitions = edgeDefinitions
23
- else
24
- raise "edgeDefinitions should be an Array, not a #{edgeDefinitions.class}"
25
- end
26
-
27
- if orphanCollections.is_a?(Array)
28
- @orphanCollections = orphanCollections
29
- else
30
- raise "orphanCollections should be an Array, not a #{orphanCollections.class}"
31
- end
32
-
33
- @idCache = "GRA_#{@graph}"
34
- end
35
-
36
- attr_reader :graph, :edgeDefinitions, :orphanCollections, :database, :idCache
37
- alias name graph
38
-
39
- # === RETRIEVE ===
40
-
41
- def database
42
- ArangoDatabase.new(database: @database)
43
- end
44
-
45
- # === GET ===
46
-
47
- def retrieve # TESTED
48
- result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}", @@request)
49
- return result.headers["x-arango-async-id"] if @@async == "store"
50
- result = result.parsed_response
51
- return result if @@verbose
52
- return result["errorMessage"] if result["error"]
53
- @edgeDefinitions = result["graph"]["edgeDefinitions"]
54
- @orphanCollections = result["graph"]["orphanCollections"]
55
- self
56
- end
57
-
58
- # === POST ===
59
-
60
- def create # TESTED
61
- body = { "name" => @graph, "edgeDefinitions" => @edgeDefinitions, "orphanCollections" => @orphanCollections }
62
- request = @@request.merge({ :body => body.to_json })
63
- result = self.class.post("/_db/#{@database}/_api/gharial", request)
64
- return result.headers["x-arango-async-id"] if @@async == "store"
65
- result = result.parsed_response
66
- @@verbose ? result : result["error"] ? result["errorMessage"] : self
67
- end
68
-
69
- # === DELETE ===
70
-
71
- def destroy # TESTED
72
- result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}", @@request)
73
- return result.headers["x-arango-async-id"] if @@async == "store"
74
- result = result.parsed_response
75
- @@verbose ? result : result["error"] ? result["errorMessage"] : true
76
- end
77
-
78
- # === VERTEX COLLECTION ===
79
-
80
- def vertexCollections # TESTED
81
- result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex", @@request)
82
- return result.headers["x-arango-async-id"] if @@async == "store"
83
- result = result.parsed_response
84
- @@verbose ? result : result["error"] ? result["errorMessage"] : result["collections"].map{|x| ArangoCollection.new(collection: x)}
85
- end
86
-
87
- def addVertexCollection(collection:) # TESTED
88
- collection = collection.is_a?(String) ? collection : collection.collection
89
- body = { "collection" => collection }.to_json
90
- request = @@request.merge({ :body => body })
91
- result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex", request)
92
- return result.headers["x-arango-async-id"] if @@async == "store"
93
- result = result.parsed_response
94
- if @@verbose
95
- @orphanCollections << collection unless result["error"]
96
- result
97
- else
98
- return result["errorMessage"] if result["error"]
99
- @orphanCollections << collection
100
- self
101
- end
102
- end
103
-
104
- def removeVertexCollection(collection:) # TESTED
105
- collection = collection.is_a?(String) ? collection : collection.collection
106
- result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{collection}", @@request)
107
- return result.headers["x-arango-async-id"] if @@async == "store"
108
- result = result.parsed_response
109
- if @@verbose
110
- @orphanCollections -= [collection] unless result["error"]
111
- result
112
- else
113
- return result["errorMessage"] if result["error"]
114
- @orphanCollections -= [collection]
115
- self
116
- end
117
- end
118
-
119
- # === EDGE COLLECTION ===
120
-
121
- def edgeCollections # TESTED
122
- result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/edge", @@request)
123
- return result.headers["x-arango-async-id"] if @@async == "store"
124
- result = result.parsed_response
125
- @@verbose ? result : result["error"] ? result["errorMessage"] : result["collections"].map{|x| ArangoCollection.new(collection: x)}
126
- end
127
-
128
- def addEdgeCollection(collection:, from:, to:, replace: false) # TESTED
129
- from = from.is_a?(String) ? [from] : from.is_a?(ArangoCollection) ? [from.collection] : from
130
- to = to.is_a?(String) ? [to] : to.is_a?(ArangoCollection) ? [to.collection] : to
131
- body = {}
132
- collection = collection.is_a?(String) ? collection : collection.collection
133
- body["collection"] = collection
134
- body["from"] = from.map{|f| f.is_a?(String) ? f : f.id }
135
- body["to"] = to.map{|t| t.is_a?(String) ? t : t.id }
136
- request = @@request.merge({ :body => body.to_json })
137
- if replace
138
- result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{collection}", request)
139
- else
140
- result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/edge", request)
141
- end
142
- return result.headers["x-arango-async-id"] if @@async == "store"
143
- result = result.parsed_response
144
- if @@verbose
145
- unless result["error"]
146
- @edgeDefinitions = result["graph"]["edgeDefinitions"]
147
- @orphanCollections = result["graph"]["orphanCollections"]
148
- end
149
- result
150
- else
151
- return result["errorMessage"] if result["error"]
152
- @edgeDefinitions = result["graph"]["edgeDefinitions"]
153
- @orphanCollections = result["graph"]["orphanCollections"]
154
- self
155
- end
156
- end
157
-
158
- def replaceEdgeCollection(collection:, from:, to:) # TESTED
159
- self.addEdgeCollection(collection: collection, from: from, to: to, replace: true)
160
- end
161
-
162
- def removeEdgeCollection(collection:) # TESTED
163
- collection = collection.is_a?(String) ? collection : collection.collection
164
- result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{collection}", @@request)
165
- return result.headers["x-arango-async-id"] if @@async == "store"
166
- result = result.parsed_response
167
- if @@verbose
168
- unless result["error"]
169
- @edgeDefinitions = result["graph"]["edgeDefinitions"]
170
- @orphanCollections = result["graph"]["orphanCollections"]
171
- end
172
- result
173
- else
174
- return result["errorMessage"] if result["error"]
175
- @edgeDefinitions = result["graph"]["edgeDefinitions"]
176
- @orphanCollections = result["graph"]["orphanCollections"]
177
- self
178
- end
179
- end
180
- end
1
+ # ==== GRAPH ====
2
+
3
+ class ArangoGraph < ArangoServer
4
+ def initialize(graph: @@graph, database: @@database, edgeDefinitions: [], orphanCollections: []) # TESTED
5
+ if database.is_a?(String)
6
+ @database = database
7
+ elsif database.is_a?(ArangoDatabase)
8
+ @database = database.database
9
+ else
10
+ raise "database should be a String or an ArangoDatabase instance, not a #{database.class}"
11
+ end
12
+
13
+ if graph.is_a?(String)
14
+ @graph = graph
15
+ elsif database.is_a?(ArangoGraph)
16
+ @graph = graph.graph
17
+ else
18
+ raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
19
+ end
20
+
21
+ if edgeDefinitions.is_a?(Array)
22
+ @edgeDefinitions = edgeDefinitions
23
+ else
24
+ raise "edgeDefinitions should be an Array, not a #{edgeDefinitions.class}"
25
+ end
26
+
27
+ if orphanCollections.is_a?(Array)
28
+ @orphanCollections = orphanCollections
29
+ else
30
+ raise "orphanCollections should be an Array, not a #{orphanCollections.class}"
31
+ end
32
+
33
+ @idCache = "GRA_#{@graph}"
34
+ end
35
+
36
+ attr_reader :graph, :edgeDefinitions, :orphanCollections, :database, :idCache
37
+ alias name graph
38
+
39
+ # === RETRIEVE ===
40
+
41
+ def to_hash
42
+ {
43
+ "graph" => @graph,
44
+ "collection" => @collection,
45
+ "database" => @database,
46
+ "edgeDefinitions" => @edgeDefinitions,
47
+ "orphanCollections" => @orphanCollections,
48
+ "idCache" => @idCache
49
+ }.delete_if{|k,v| v.nil?}
50
+ end
51
+ alias to_h to_hash
52
+
53
+ def database
54
+ ArangoDatabase.new(database: @database)
55
+ end
56
+
57
+ # === GET ===
58
+
59
+ def retrieve # TESTED
60
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}", @@request)
61
+ return result.headers["x-arango-async-id"] if @@async == "store"
62
+ return true if @@async
63
+ result = result.parsed_response
64
+ return result if @@verbose
65
+ return result["errorMessage"] if result["error"]
66
+ @edgeDefinitions = result["graph"]["edgeDefinitions"]
67
+ @orphanCollections = result["graph"]["orphanCollections"]
68
+ self
69
+ end
70
+
71
+ # === POST ===
72
+
73
+ def create # TESTED
74
+ body = { "name" => @graph, "edgeDefinitions" => @edgeDefinitions, "orphanCollections" => @orphanCollections }
75
+ request = @@request.merge({ :body => body.to_json })
76
+ result = self.class.post("/_db/#{@database}/_api/gharial", request)
77
+ return result.headers["x-arango-async-id"] if @@async == "store"
78
+ return true if @@async
79
+ result = result.parsed_response
80
+ @@verbose ? result : result["error"] ? result["errorMessage"] : self
81
+ end
82
+
83
+ # === DELETE ===
84
+
85
+ def destroy # TESTED
86
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}", @@request)
87
+ return result.headers["x-arango-async-id"] if @@async == "store"
88
+ return true if @@async
89
+ result = result.parsed_response
90
+ @@verbose ? result : result["error"] ? result["errorMessage"] : true
91
+ end
92
+
93
+ # === VERTEX COLLECTION ===
94
+
95
+ def vertexCollections # TESTED
96
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex", @@request)
97
+ return result.headers["x-arango-async-id"] if @@async == "store"
98
+ return true if @@async
99
+ result = result.parsed_response
100
+ @@verbose ? result : result["error"] ? result["errorMessage"] : result["collections"].map{|x| ArangoCollection.new(collection: x)}
101
+ end
102
+
103
+ def addVertexCollection(collection:) # TESTED
104
+ collection = collection.is_a?(String) ? collection : collection.collection
105
+ body = { "collection" => collection }.to_json
106
+ request = @@request.merge({ :body => body })
107
+ result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex", request)
108
+ return result.headers["x-arango-async-id"] if @@async == "store"
109
+ return true if @@async
110
+ result = result.parsed_response
111
+ if @@verbose
112
+ @orphanCollections << collection unless result["error"]
113
+ result
114
+ else
115
+ return result["errorMessage"] if result["error"]
116
+ @orphanCollections << collection
117
+ self
118
+ end
119
+ end
120
+
121
+ def removeVertexCollection(collection:) # TESTED
122
+ collection = collection.is_a?(String) ? collection : collection.collection
123
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{collection}", @@request)
124
+ return result.headers["x-arango-async-id"] if @@async == "store"
125
+ return true if @@async
126
+ result = result.parsed_response
127
+ if @@verbose
128
+ @orphanCollections -= [collection] unless result["error"]
129
+ result
130
+ else
131
+ return result["errorMessage"] if result["error"]
132
+ @orphanCollections -= [collection]
133
+ self
134
+ end
135
+ end
136
+
137
+ # === EDGE COLLECTION ===
138
+
139
+ def edgeCollections # TESTED
140
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/edge", @@request)
141
+ return result.headers["x-arango-async-id"] if @@async == "store"
142
+ return true if @@async
143
+ result = result.parsed_response
144
+ @@verbose ? result : result["error"] ? result["errorMessage"] : result["collections"].map{|x| ArangoCollection.new(collection: x)}
145
+ end
146
+
147
+ def addEdgeCollection(collection:, from:, to:, replace: false) # TESTED
148
+ from = from.is_a?(String) ? [from] : from.is_a?(ArangoCollection) ? [from.collection] : from
149
+ to = to.is_a?(String) ? [to] : to.is_a?(ArangoCollection) ? [to.collection] : to
150
+ body = {}
151
+ collection = collection.is_a?(String) ? collection : collection.collection
152
+ body["collection"] = collection
153
+ body["from"] = from.map{|f| f.is_a?(String) ? f : f.id }
154
+ body["to"] = to.map{|t| t.is_a?(String) ? t : t.id }
155
+ request = @@request.merge({ :body => body.to_json })
156
+ if replace
157
+ result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{collection}", request)
158
+ else
159
+ result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/edge", request)
160
+ end
161
+ return result.headers["x-arango-async-id"] if @@async == "store"
162
+ return true if @@async
163
+ result = result.parsed_response
164
+ if @@verbose
165
+ unless result["error"]
166
+ @edgeDefinitions = result["graph"]["edgeDefinitions"]
167
+ @orphanCollections = result["graph"]["orphanCollections"]
168
+ end
169
+ result
170
+ else
171
+ return result["errorMessage"] if result["error"]
172
+ @edgeDefinitions = result["graph"]["edgeDefinitions"]
173
+ @orphanCollections = result["graph"]["orphanCollections"]
174
+ self
175
+ end
176
+ end
177
+
178
+ def replaceEdgeCollection(collection:, from:, to:) # TESTED
179
+ self.addEdgeCollection(collection: collection, from: from, to: to, replace: true)
180
+ end
181
+
182
+ def removeEdgeCollection(collection:) # TESTED
183
+ collection = collection.is_a?(String) ? collection : collection.collection
184
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/edge/#{collection}", @@request)
185
+ return result.headers["x-arango-async-id"] if @@async == "store"
186
+ return true if @@async
187
+ result = result.parsed_response
188
+ if @@verbose
189
+ unless result["error"]
190
+ @edgeDefinitions = result["graph"]["edgeDefinitions"]
191
+ @orphanCollections = result["graph"]["orphanCollections"]
192
+ end
193
+ result
194
+ else
195
+ return result["errorMessage"] if result["error"]
196
+ @edgeDefinitions = result["graph"]["edgeDefinitions"]
197
+ @orphanCollections = result["graph"]["orphanCollections"]
198
+ self
199
+ end
200
+ end
201
+ end