arangorb 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
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,53 +1,68 @@
1
- # === TRANSACTION ===
2
-
3
- class ArangoTransaction < ArangoServer
4
- def initialize(database: @@database, action:, write: [], read: [], params: nil, lockTimeout: nil, waitForSync: nil) # 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
- @action = action
13
- @collections = {}
14
- @collections["write"] = write.is_a?(Array) ? write.map{ |x| x.is_a?(String) ? x : x.is_a?(ArangoCollection) ? x.collection : nil } : write.is_a?(String) ? [write] : write.is_a?(ArangoCollection) ? [write.collection] : []
15
- @collections["read"] = read.is_a?(Array) ? read.map{ |x| x.is_a?(String) ? x : x.is_a?(ArangoCollection) ? x.collection : nil } : read.is_a?(String) ? [read] : read.is_a?(ArangoCollection) ? [read.collection] : []
16
- @params = params
17
- @lockTimeout = lockTimeout
18
- @waitForSync = waitForSync
19
- @result = nil
20
- @idCache = "AT_#{@database}_#{Random.rand(0..10^12)}"
21
- end
22
-
23
- attr_reader :action, :params, :lockTimeout, :waitForSync, :idCache
24
-
25
- ### RETRIEVE ###
26
-
27
- def collections
28
- result = {}
29
- result["write"] = @collections["write"].map{|x| ArangoCollection.new(database: @database, collection: x)} unless @collections["write"].nil?
30
- result["read"] = @collections["read"].map{|x| ArangoCollection.new(database: @database, collection: x)} unless @collections["read"].nil?
31
- result
32
- end
33
-
34
- def database
35
- ArangoDatabase.new(database: @database)
36
- end
37
-
38
- def execute # TESTED
39
- body = {
40
- "action" => @action,
41
- "collections" => @collections,
42
- "params" => @params,
43
- "lockTimeout" => @lockTimeout,
44
- "waitForSync" => @waitForSync
45
- }.delete_if{|k,v| v.nil?}.to_json
46
- request = @@request.merge({ :body => body })
47
- result = self.class.post("/_db/#{@database}/_api/transaction", request)
48
- return result.headers["x-arango-async-id"] if @@async == "store"
49
- result = result.parsed_response
50
- @result = result["result"] unless result["error"]
51
- @@verbose ? result : result["error"] ? {"message": result["errorMessage"], "stacktrace": result["stacktrace"]} : result["result"]
52
- end
53
- end
1
+ # === TRANSACTION ===
2
+
3
+ class ArangoTransaction < ArangoServer
4
+ def initialize(database: @@database, action:, write: [], read: [], params: nil, lockTimeout: nil, waitForSync: nil) # 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
+ @action = action
13
+ @collections = {}
14
+ @collections["write"] = write.is_a?(Array) ? write.map{ |x| x.is_a?(String) ? x : x.is_a?(ArangoCollection) ? x.collection : nil } : write.is_a?(String) ? [write] : write.is_a?(ArangoCollection) ? [write.collection] : []
15
+ @collections["read"] = read.is_a?(Array) ? read.map{ |x| x.is_a?(String) ? x : x.is_a?(ArangoCollection) ? x.collection : nil } : read.is_a?(String) ? [read] : read.is_a?(ArangoCollection) ? [read.collection] : []
16
+ @params = params
17
+ @lockTimeout = lockTimeout
18
+ @waitForSync = waitForSync
19
+ @result = nil
20
+ @idCache = "AT_#{@database}_#{Random.rand(0..10^12)}"
21
+ end
22
+
23
+ attr_reader :action, :params, :lockTimeout, :waitForSync, :idCache
24
+
25
+ ### RETRIEVE ###
26
+
27
+ def to_hash
28
+ {
29
+ "database" => @database,
30
+ "action" => @action,
31
+ "collections" => @collections,
32
+ "result" => @result,
33
+ "params" => @params,
34
+ "lockTimeout" => @lockTimeout,
35
+ "waitForSync" => @waitForSync,
36
+ "idCache" => @idCache
37
+ }.delete_if{|k,v| v.nil?}
38
+ end
39
+ alias to_h to_hash
40
+
41
+ def collections
42
+ result = {}
43
+ result["write"] = @collections["write"].map{|x| ArangoCollection.new(database: @database, collection: x)} unless @collections["write"].nil?
44
+ result["read"] = @collections["read"].map{|x| ArangoCollection.new(database: @database, collection: x)} unless @collections["read"].nil?
45
+ result
46
+ end
47
+
48
+ def database
49
+ ArangoDatabase.new(database: @database)
50
+ end
51
+
52
+ def execute # TESTED
53
+ body = {
54
+ "action" => @action,
55
+ "collections" => @collections,
56
+ "params" => @params,
57
+ "lockTimeout" => @lockTimeout,
58
+ "waitForSync" => @waitForSync
59
+ }.delete_if{|k,v| v.nil?}.to_json
60
+ request = @@request.merge({ :body => body })
61
+ result = self.class.post("/_db/#{@database}/_api/transaction", request)
62
+ return result.headers["x-arango-async-id"] if @@async == "store"
63
+ return true if @@async
64
+ result = result.parsed_response
65
+ @result = result["result"] unless result["error"]
66
+ @@verbose ? result : result["error"] ? {"message": result["errorMessage"], "stacktrace": result["stacktrace"]} : result["result"]
67
+ end
68
+ end
@@ -1,136 +1,149 @@
1
- # === USER ===
2
-
3
- class ArangoUser < ArangoServer
4
- def initialize(user: @@user, password: nil, active: nil, extra: nil) # TESTED
5
- @password = password
6
- @user = user
7
- @active = active
8
- @extra = extra
9
- @idCache = "USER_#{@user}"
10
- end
11
-
12
- attr_reader :user, :active, :extra, :idCache
13
- alias name user
14
-
15
- def [](database)
16
- if self.databases[database] == "rw"
17
- ArangoDatabase.new database: database
18
- else
19
- "This User does not have access to Database #{database}."
20
- end
21
- end
22
- alias database []
23
-
24
- def create # TESTED
25
- body = {
26
- "user" => @user,
27
- "passwd" => @password,
28
- "active" => @active,
29
- "extra" => @extra
30
- }.delete_if{|k,v| v.nil?}.to_json
31
- request = @@request.merge({ :body => body })
32
- result = self.class.post("/_api/user", request)
33
- return_result result: result
34
- end
35
-
36
- def retrieve # TESTED
37
- result = self.class.get("/_api/user/#{@user}", @@request)
38
- return_result result: result
39
- end
40
-
41
- def grant(database: @@database) # TESTED
42
- database = database.database if database.is_a?(ArangoDatabase)
43
- body = { "grant" => "rw" }.to_json
44
- request = @@request.merge({ :body => body })
45
- result = self.class.put("/_api/user/#{@user}/database/#{database}", request)
46
- return_result result: result, caseTrue: true
47
- end
48
-
49
- def revoke(database: @@database) # TESTED
50
- database = database.database if database.is_a?(ArangoDatabase)
51
- body = { "grant" => "none" }.to_json
52
- request = @@request.merge({ :body => body })
53
- result = self.class.put("/_api/user/#{@user}/database/#{database}", request)
54
- return_result result: result, caseTrue: true
55
- end
56
-
57
- def databases # TESTED
58
- result = self.class.get("/_api/user/#{@user}/database/", @@request)
59
- return_result result: result, key: "result"
60
- end
61
-
62
- def replace(password:, active: nil, extra: nil) # TESTED
63
- body = {
64
- "passwd" => password,
65
- "active" => active,
66
- "extra" => extra
67
- }.delete_if{|k,v| v.nil?}.to_json
68
- request = @@request.merge({ :body => body })
69
- result = self.class.put("/_api/user/#{@user}", request)
70
- return result.headers["x-arango-async-id"] if @@async == "store"
71
- result = result.parsed_response
72
- if @@verbose
73
- unless result["error"]
74
- @password = password
75
- @active = active.nil? || active
76
- @extra = extra
77
- end
78
- result
79
- else
80
- return result["errorMessage"] if result["error"]
81
- @password = password
82
- @active = active.nil? || active
83
- @extra = extra
84
- self
85
- end
86
- end
87
-
88
- def update(password: , active: nil, extra: nil) # TESTED
89
- body = {
90
- "passwd" => password,
91
- "active" => active,
92
- "extra" => extra
93
- }.delete_if{|k,v| v.nil?}.to_json
94
- request = @@request.merge({ :body => body })
95
- result = self.class.patch("/_api/user/#{@user}", request)
96
- return result.headers["x-arango-async-id"] if @@async == "store"
97
- result = result.parsed_response
98
- if @@verbose
99
- unless result["error"]
100
- @password = password
101
- @active = active.nil? || active
102
- @extra = extra
103
- end
104
- result
105
- else
106
- return result["errorMessage"] if result["error"]
107
- @password = password
108
- @active = active.nil? || active
109
- @extra = extra
110
- self
111
- end
112
- end
113
-
114
- def destroy # TESTED
115
- result = self.class.delete("/_api/user/#{@user}", @@request)
116
- return_result result: result, caseTrue: true
117
- end
118
-
119
- def return_result(result:, caseTrue: false, key: nil)
120
- return result.headers["x-arango-async-id"] if @@async == "store"
121
- result = result.parsed_response
122
- if @@verbose || !result.is_a?(Hash)
123
- unless result["error"]
124
- @active = result["active"]
125
- @extra = result["extra"]
126
- end
127
- result
128
- else
129
- return result["errorMessage"] if result["error"]
130
- @active = result["active"]
131
- @extra = result["extra"]
132
- return true if caseTrue
133
- key.nil? ? self : result[key]
134
- end
135
- end
136
- end
1
+ # === USER ===
2
+
3
+ class ArangoUser < ArangoServer
4
+ def initialize(user: @@user, password: nil, active: nil, extra: nil) # TESTED
5
+ @password = password
6
+ @user = user
7
+ @active = active
8
+ @extra = extra
9
+ @idCache = "USER_#{@user}"
10
+ end
11
+
12
+ attr_reader :user, :active, :extra, :idCache
13
+ alias name user
14
+
15
+ def to_hash
16
+ {
17
+ "user" => @user,
18
+ "active" => @active,
19
+ "extra" => @extra,
20
+ "idCache" => @idCache
21
+ }.delete_if{|k,v| v.nil?}
22
+ end
23
+ alias to_h to_hash
24
+
25
+ def [](database)
26
+ if self.databases[database] == "rw"
27
+ ArangoDatabase.new database: database
28
+ else
29
+ "This User does not have access to Database #{database}."
30
+ end
31
+ end
32
+ alias database []
33
+
34
+ def create # TESTED
35
+ body = {
36
+ "user" => @user,
37
+ "passwd" => @password,
38
+ "active" => @active,
39
+ "extra" => @extra
40
+ }.delete_if{|k,v| v.nil?}.to_json
41
+ request = @@request.merge({ :body => body })
42
+ result = self.class.post("/_api/user", request)
43
+ return_result result: result
44
+ end
45
+
46
+ def retrieve # TESTED
47
+ result = self.class.get("/_api/user/#{@user}", @@request)
48
+ return_result result: result
49
+ end
50
+
51
+ def grant(database: @@database) # TESTED
52
+ database = database.database if database.is_a?(ArangoDatabase)
53
+ body = { "grant" => "rw" }.to_json
54
+ request = @@request.merge({ :body => body })
55
+ result = self.class.put("/_api/user/#{@user}/database/#{database}", request)
56
+ return_result result: result, caseTrue: true
57
+ end
58
+
59
+ def revoke(database: @@database) # TESTED
60
+ database = database.database if database.is_a?(ArangoDatabase)
61
+ body = { "grant" => "none" }.to_json
62
+ request = @@request.merge({ :body => body })
63
+ result = self.class.put("/_api/user/#{@user}/database/#{database}", request)
64
+ return_result result: result, caseTrue: true
65
+ end
66
+
67
+ def databases # TESTED
68
+ result = self.class.get("/_api/user/#{@user}/database/", @@request)
69
+ return_result result: result, key: "result"
70
+ end
71
+
72
+ def replace(password:, active: nil, extra: nil) # TESTED
73
+ body = {
74
+ "passwd" => password,
75
+ "active" => active,
76
+ "extra" => extra
77
+ }.delete_if{|k,v| v.nil?}.to_json
78
+ request = @@request.merge({ :body => body })
79
+ result = self.class.put("/_api/user/#{@user}", request)
80
+ return result.headers["x-arango-async-id"] if @@async == "store"
81
+ return true if @@async
82
+ result = result.parsed_response
83
+ if @@verbose
84
+ unless result["error"]
85
+ @password = password
86
+ @active = active.nil? || active
87
+ @extra = extra
88
+ end
89
+ result
90
+ else
91
+ return result["errorMessage"] if result["error"]
92
+ @password = password
93
+ @active = active.nil? || active
94
+ @extra = extra
95
+ self
96
+ end
97
+ end
98
+
99
+ def update(password: , active: nil, extra: nil) # TESTED
100
+ body = {
101
+ "passwd" => password,
102
+ "active" => active,
103
+ "extra" => extra
104
+ }.delete_if{|k,v| v.nil?}.to_json
105
+ request = @@request.merge({ :body => body })
106
+ result = self.class.patch("/_api/user/#{@user}", request)
107
+ return result.headers["x-arango-async-id"] if @@async == "store"
108
+ return true if @@async
109
+ result = result.parsed_response
110
+ if @@verbose
111
+ unless result["error"]
112
+ @password = password
113
+ @active = active.nil? || active
114
+ @extra = extra
115
+ end
116
+ result
117
+ else
118
+ return result["errorMessage"] if result["error"]
119
+ @password = password
120
+ @active = active.nil? || active
121
+ @extra = extra
122
+ self
123
+ end
124
+ end
125
+
126
+ def destroy # TESTED
127
+ result = self.class.delete("/_api/user/#{@user}", @@request)
128
+ return_result result: result, caseTrue: true
129
+ end
130
+
131
+ def return_result(result:, caseTrue: false, key: nil)
132
+ return result.headers["x-arango-async-id"] if @@async == "store"
133
+ return true if @@async
134
+ result = result.parsed_response
135
+ if @@verbose || !result.is_a?(Hash)
136
+ unless result["error"]
137
+ @active = result["active"]
138
+ @extra = result["extra"]
139
+ end
140
+ result
141
+ else
142
+ return result["errorMessage"] if result["error"]
143
+ @active = result["active"]
144
+ @extra = result["extra"]
145
+ return true if caseTrue
146
+ key.nil? ? self : result[key]
147
+ end
148
+ end
149
+ end
@@ -1,147 +1,162 @@
1
- # === GRAPH VERTEX ===
2
-
3
- # ==== DOCUMENT ====
4
-
5
- class ArangoVertex < ArangoDocument
6
- def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}) # TESTED
7
- if collection.is_a?(String)
8
- @collection = collection
9
- elsif collection.is_a?(ArangoCollection)
10
- @collection = collection.collection
11
- else
12
- raise "collection should be a String or an ArangoCollection instance, not a #{collection.class}"
13
- end
14
-
15
- if graph.is_a?(String)
16
- @graph = graph
17
- elsif graph.is_a?(ArangoGraph)
18
- @graph = graph.graph
19
- else
20
- raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
21
- end
22
-
23
- if database.is_a?(String)
24
- @database = database
25
- else
26
- raise "database should be a String, 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
- @idCache = "VER_#{@id}"
48
- end
49
-
50
- attr_reader :key, :id, :body, :idCache
51
-
52
- # === RETRIEVE ===
53
-
54
- def graph
55
- ArangoGraph.new(graph: @graph, database: @database)
56
- end
57
-
58
- # === GET ===
59
-
60
- def retrieve # TESTED
61
- result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", @@request)
62
- return result.headers["x-arango-async-id"] if @@async == "store"
63
- result = result.parsed_response
64
- if @@verbose
65
- @body = result["vertex"] unless result["error"]
66
- result
67
- else
68
- return result["errorMessage"] if result["error"]
69
- @body = result["vertex"]
70
- self
71
- end
72
- end
73
-
74
- # === POST ====
75
-
76
- def create(body: @body, waitForSync: nil) # TESTED
77
- query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
78
- body["_key"] = @key if body["_key"].nil? && !@key.nil?
79
- request = @@request.merge({ :body => body.to_json, :query => query })
80
- result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@collection}", request)
81
- return_result result: result, body: body
82
- end
83
- alias create_vertex create
84
-
85
- # === MODIFY ===
86
-
87
- def replace(body: {}, waitForSync: nil) # TESTED
88
- query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
89
- request = @@request.merge({ :body => body.to_json, :query => query })
90
- result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
91
- return_result result: result, body: body
92
- end
93
-
94
- def update(body: {}, waitForSync: nil, keepNull: nil) # TESTED
95
- query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
96
- request = @@request.merge({ :body => body.to_json, :query => query })
97
- result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
98
- return result.headers["x-arango-async-id"] if @@async == "store"
99
- result = result.parsed_response
100
- if @@verbose
101
- unless result["error"]
102
- @key = result["_key"]
103
- @id = "#{@collection}/#{@key}"
104
- @body = result["vertex"].body
105
- end
106
- result
107
- else
108
- return result["errorMessage"] if result["error"]
109
- @key = result["vertex"]["_key"]
110
- @id = "#{@collection}/#{@key}"
111
- @body = @body.merge(body)
112
- @body = @body.merge(result["vertex"])
113
- self
114
- end
115
- end
116
-
117
- # === DELETE ===
118
-
119
- def destroy(waitForSync: nil) # TESTED
120
- query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
121
- request = @@request.merge({ :query => query })
122
- result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
123
- return_result result: result, caseTrue: true
124
- end
125
-
126
- # === UTILITY ===
127
-
128
- def return_result(result:, body: {}, caseTrue: false)
129
- return result.headers["x-arango-async-id"] if @@async == "store"
130
- result = result.parsed_response
131
- if @@verbose
132
- unless result["error"]
133
- @key = result["vertex"]["_key"]
134
- @id = "#{@collection}/#{@key}"
135
- @body = result["vertex"].merge(body)
136
- end
137
- result
138
- else
139
- return result["errorMessage"] if result["error"]
140
- return true if caseTrue
141
- @key = result["vertex"]["_key"]
142
- @id = "#{@collection}/#{@key}"
143
- @body = result["vertex"].merge(body)
144
- self
145
- end
146
- end
147
- end
1
+ # === GRAPH VERTEX ===
2
+
3
+ # ==== DOCUMENT ====
4
+
5
+ class ArangoVertex < ArangoDocument
6
+ def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}) # TESTED
7
+ if collection.is_a?(String)
8
+ @collection = collection
9
+ elsif collection.is_a?(ArangoCollection)
10
+ @collection = collection.collection
11
+ else
12
+ raise "collection should be a String or an ArangoCollection instance, not a #{collection.class}"
13
+ end
14
+
15
+ if graph.is_a?(String)
16
+ @graph = graph
17
+ elsif graph.is_a?(ArangoGraph)
18
+ @graph = graph.graph
19
+ else
20
+ raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
21
+ end
22
+
23
+ if database.is_a?(String)
24
+ @database = database
25
+ else
26
+ raise "database should be a String, 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
+ @idCache = "VER_#{@id}"
48
+ end
49
+
50
+ attr_reader :key, :id, :body, :idCache
51
+
52
+ # === RETRIEVE ===
53
+
54
+ def to_hash
55
+ {
56
+ "key" => @key,
57
+ "id" => @id,
58
+ "collection" => @collection,
59
+ "database" => @database,
60
+ "body" => @body,
61
+ "idCache" => @idCache
62
+ }.delete_if{|k,v| v.nil?}
63
+ end
64
+ alias to_h to_hash
65
+
66
+ def graph
67
+ ArangoGraph.new(graph: @graph, database: @database)
68
+ end
69
+
70
+ # === GET ===
71
+
72
+ def retrieve # TESTED
73
+ result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", @@request)
74
+ return result.headers["x-arango-async-id"] if @@async == "store"
75
+ return true if @@async
76
+ result = result.parsed_response
77
+ if @@verbose
78
+ @body = result["vertex"] unless result["error"]
79
+ result
80
+ else
81
+ return result["errorMessage"] if result["error"]
82
+ @body = result["vertex"]
83
+ self
84
+ end
85
+ end
86
+
87
+ # === POST ====
88
+
89
+ def create(body: @body, waitForSync: nil) # TESTED
90
+ query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
91
+ body["_key"] = @key if body["_key"].nil? && !@key.nil?
92
+ request = @@request.merge({ :body => body.to_json, :query => query })
93
+ result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@collection}", request)
94
+ return_result result: result, body: body
95
+ end
96
+ alias create_vertex create
97
+
98
+ # === MODIFY ===
99
+
100
+ def replace(body: {}, waitForSync: nil) # TESTED
101
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
102
+ request = @@request.merge({ :body => body.to_json, :query => query })
103
+ result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
104
+ return_result result: result, body: body
105
+ end
106
+
107
+ def update(body: {}, waitForSync: nil, keepNull: nil) # TESTED
108
+ query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
109
+ request = @@request.merge({ :body => body.to_json, :query => query })
110
+ result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
111
+ return result.headers["x-arango-async-id"] if @@async == "store"
112
+ return true if @@async
113
+ result = result.parsed_response
114
+ if @@verbose
115
+ unless result["error"]
116
+ @key = result["_key"]
117
+ @id = "#{@collection}/#{@key}"
118
+ @body = result["vertex"].body
119
+ end
120
+ result
121
+ else
122
+ return result["errorMessage"] if result["error"]
123
+ @key = result["vertex"]["_key"]
124
+ @id = "#{@collection}/#{@key}"
125
+ @body = @body.merge(body)
126
+ @body = @body.merge(result["vertex"])
127
+ self
128
+ end
129
+ end
130
+
131
+ # === DELETE ===
132
+
133
+ def destroy(waitForSync: nil) # TESTED
134
+ query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
135
+ request = @@request.merge({ :query => query })
136
+ result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
137
+ return_result result: result, caseTrue: true
138
+ end
139
+
140
+ # === UTILITY ===
141
+
142
+ def return_result(result:, body: {}, caseTrue: false)
143
+ return result.headers["x-arango-async-id"] if @@async == "store"
144
+ return true if @@async
145
+ result = result.parsed_response
146
+ if @@verbose
147
+ unless result["error"]
148
+ @key = result["vertex"]["_key"]
149
+ @id = "#{@collection}/#{@key}"
150
+ @body = result["vertex"].merge(body)
151
+ end
152
+ result
153
+ else
154
+ return result["errorMessage"] if result["error"]
155
+ return true if caseTrue
156
+ @key = result["vertex"]["_key"]
157
+ @id = "#{@collection}/#{@key}"
158
+ @body = result["vertex"].merge(body)
159
+ self
160
+ end
161
+ end
162
+ end