chino_ruby 1.3 → 3.0.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.
- checksums.yaml +5 -5
- data/.gitignore +30 -28
- data/.rubocop.yml +149 -0
- data/CHANGELOG.md +29 -0
- data/CONTRIBUTING.md +17 -0
- data/Gemfile +4 -4
- data/Gemfile.lock +14 -111
- data/INSTRUCTIONS.md +61 -0
- data/LICENSE.txt +21 -21
- data/README.md +107 -73
- data/Rakefile +2 -2
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/chino-ruby.iml +19 -0
- data/chino_ruby.gemspec +38 -42
- data/config-chino-base.yml +9 -0
- data/lib/chino_ruby.rb +73 -1581
- data/lib/chino_ruby/classes.rb +1592 -0
- data/lib/chino_ruby/version.rb +3 -3
- metadata +21 -75
@@ -0,0 +1,1592 @@
|
|
1
|
+
module ChinoRuby
|
2
|
+
QUERY_DEFAULT_LIMIT = 100
|
3
|
+
|
4
|
+
|
5
|
+
class CheckValues
|
6
|
+
|
7
|
+
# This function is used to check if a parameter passed to a function is a string, otherwise it raises an error
|
8
|
+
def check_string(value)
|
9
|
+
if not value.is_a?(String)
|
10
|
+
raise ArgumentError, "{#value} must be a String, got #{value.inspect}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# This function is used to check if a parameter passed to a function is an integer, otherwise it raises an error
|
15
|
+
def check_int(value)
|
16
|
+
if not value.is_a?(Integer)
|
17
|
+
raise ArgumentError, "{#value} must be a Int, got #{value.inspect}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# This function is used to check if a parameter passed to a function is a boolean, otherwise it raises an error
|
22
|
+
def check_boolean(value)
|
23
|
+
if not !!value == value
|
24
|
+
raise ArgumentError, "{#value} must be a Boolean, got #{value.inspect}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This function is used to check if a parameter passed to a function can be converted to json, otherwise it raises an error
|
29
|
+
def check_json(value)
|
30
|
+
if not value.respond_to?(:to_json)
|
31
|
+
raise ArgumentError, "{#value} cannot be converted to json!"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Class which defines the fields for the creation of a Schema or a UserSchema
|
37
|
+
class Field < CheckValues
|
38
|
+
attr_accessor :type, :name, :indexed
|
39
|
+
|
40
|
+
# * type: type of the field in the Schema/UserSchema. Ex: 'string'
|
41
|
+
# * name: name of the field in the Schema/UserSchema
|
42
|
+
# * indexed: if true, the field will be indexed on the server. That means it can be used to make a search request
|
43
|
+
def initialize(type, name, indexed)
|
44
|
+
check_string(type)
|
45
|
+
check_string(name)
|
46
|
+
check_boolean(indexed)
|
47
|
+
self.type = type
|
48
|
+
self.name = name
|
49
|
+
self.indexed = indexed
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the values as a json
|
53
|
+
def to_json
|
54
|
+
return {"type": type, "name": name, "indexed": indexed}.to_json
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Base class of every resource class. It contains the functions for the GET, POST, PUT, PATCH and DELETE requests
|
59
|
+
class ChinoBaseAPI < CheckValues
|
60
|
+
|
61
|
+
# Used to inizialize a customer or a user. If you want to authenticate a user, simply pass nil as the customer_id
|
62
|
+
def initialize(customer_id, customer_key, host_url)
|
63
|
+
@customer_id = customer_id.blank? ? "Bearer " : customer_id
|
64
|
+
@customer_key = customer_key
|
65
|
+
@host_url = host_url
|
66
|
+
end
|
67
|
+
|
68
|
+
#returns the uri with the proper params if specified
|
69
|
+
def return_uri(path, limit=nil, offset=nil, full_document=nil)
|
70
|
+
uri = URI(@host_url+path)
|
71
|
+
if limit!=nil && offset!=nil
|
72
|
+
if full_document!=nil
|
73
|
+
params = { :"full_document" => true, :"limit" => limit, :"offset" => offset}
|
74
|
+
uri.query = URI.encode_www_form(params)
|
75
|
+
else
|
76
|
+
params = { "limit" => limit, :"offset" => offset}
|
77
|
+
uri.query = URI.encode_www_form(params)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
uri
|
81
|
+
end
|
82
|
+
|
83
|
+
#base function to GET a resource with the proper params if specified
|
84
|
+
def get_resource(path, limit=nil, offset=nil, full_document=nil)
|
85
|
+
check_string(path)
|
86
|
+
if (limit==nil) && (offset==nil)
|
87
|
+
uri = return_uri(path)
|
88
|
+
elsif full_document==nil
|
89
|
+
uri = return_uri(path, limit, offset)
|
90
|
+
else
|
91
|
+
uri = return_uri(path, limit, offset, full_document)
|
92
|
+
end
|
93
|
+
req = Net::HTTP::Get.new(uri)
|
94
|
+
if @customer_id == "Bearer "
|
95
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
96
|
+
else
|
97
|
+
req.basic_auth @customer_id, @customer_key
|
98
|
+
end
|
99
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
100
|
+
http.request(req)
|
101
|
+
}
|
102
|
+
parse_response(res)['data']
|
103
|
+
end
|
104
|
+
|
105
|
+
#base function to POST a resource with the proper params if specified
|
106
|
+
def post_resource(path, data=nil, limit=nil, offset=nil, full_document=nil)
|
107
|
+
check_string(path)
|
108
|
+
if (limit==nil) && (offset==nil)
|
109
|
+
uri = return_uri(path)
|
110
|
+
elsif full_document==nil
|
111
|
+
uri = return_uri(path, limit, offset)
|
112
|
+
else
|
113
|
+
uri = return_uri(path, limit, offset, full_document)
|
114
|
+
end
|
115
|
+
req = Net::HTTP::Post.new(uri)
|
116
|
+
if @customer_id == "Bearer "
|
117
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
118
|
+
else
|
119
|
+
req.basic_auth @customer_id, @customer_key
|
120
|
+
end
|
121
|
+
if data!=nil
|
122
|
+
req.body = data
|
123
|
+
end
|
124
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
125
|
+
http.request(req)
|
126
|
+
}
|
127
|
+
if data!=nil
|
128
|
+
parse_response(res)['data']
|
129
|
+
else
|
130
|
+
JSON.parse(parse_response(res).to_json)['result']
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
#base function to POST a resource with string result
|
135
|
+
def post_resource_with_string_result(path, data)
|
136
|
+
check_string(path)
|
137
|
+
uri = return_uri(path)
|
138
|
+
req = Net::HTTP::Post.new(uri.path)
|
139
|
+
if @customer_id == "Bearer "
|
140
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
141
|
+
else
|
142
|
+
req.basic_auth @customer_id, @customer_key
|
143
|
+
end
|
144
|
+
req.body = data
|
145
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
146
|
+
http.request(req)
|
147
|
+
}
|
148
|
+
JSON.parse(parse_response(res).to_json)['result']
|
149
|
+
end
|
150
|
+
|
151
|
+
#base function to PUT a resource
|
152
|
+
def put_resource(path, data)
|
153
|
+
check_string(path)
|
154
|
+
uri = return_uri(path)
|
155
|
+
req = Net::HTTP::Put.new(uri.path)
|
156
|
+
if @customer_id == "Bearer "
|
157
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
158
|
+
else
|
159
|
+
req.basic_auth @customer_id, @customer_key
|
160
|
+
end
|
161
|
+
req.body = data
|
162
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
163
|
+
http.request(req)
|
164
|
+
}
|
165
|
+
parse_response(res)['data']
|
166
|
+
end
|
167
|
+
|
168
|
+
#base function to PATCH a resource
|
169
|
+
def patch_resource(path, data)
|
170
|
+
check_string(path)
|
171
|
+
uri = return_uri(path)
|
172
|
+
req = Net::HTTP::Patch.new(uri.path)
|
173
|
+
if @customer_id == "Bearer "
|
174
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
175
|
+
else
|
176
|
+
req.basic_auth @customer_id, @customer_key
|
177
|
+
end
|
178
|
+
req.body = data
|
179
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
180
|
+
http.request(req)
|
181
|
+
}
|
182
|
+
parse_response(res)['data']
|
183
|
+
end
|
184
|
+
|
185
|
+
#base function to DELETE a resource
|
186
|
+
def delete_resource(path, force)
|
187
|
+
check_string(path)
|
188
|
+
check_boolean(force)
|
189
|
+
if force
|
190
|
+
uri = return_uri(path+"?force=true")
|
191
|
+
else
|
192
|
+
uri = return_uri(path)
|
193
|
+
end
|
194
|
+
req = Net::HTTP::Delete.new(uri)
|
195
|
+
if @customer_id == "Bearer "
|
196
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
197
|
+
else
|
198
|
+
req.basic_auth @customer_id, @customer_key
|
199
|
+
end
|
200
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
201
|
+
http.request(req)
|
202
|
+
}
|
203
|
+
JSON.parse(parse_response(res).to_json)['result']
|
204
|
+
end
|
205
|
+
|
206
|
+
#base function to parse the response and raise "chino" errors if problems occurred
|
207
|
+
def parse_response(response, raw=false)
|
208
|
+
if response.is_a?(Net::HTTPServerError)
|
209
|
+
raise ChinoError.new("Chino Server Error: #{response} - #{response.body}", response)
|
210
|
+
elsif response.is_a?(Net::HTTPUnauthorized)
|
211
|
+
d = JSON.parse(response.body)
|
212
|
+
raise ChinoAuthError.new("Chino authentication error: #{d['message']}", response)
|
213
|
+
elsif !response.is_a?(Net::HTTPSuccess)
|
214
|
+
begin
|
215
|
+
d = JSON.parse(response.body)
|
216
|
+
rescue
|
217
|
+
raise ChinoError.new("Chino Server Error: body=#{response.body}", response)
|
218
|
+
end
|
219
|
+
if d['user_error'] and d['error']
|
220
|
+
raise ChinoError.new(d['error'], response, d['user_error']) #user_error is translated
|
221
|
+
elsif d['message'] == "Invalid credentials given."
|
222
|
+
raise ChinoAuthError.new(response.body, response)
|
223
|
+
elsif d['error']
|
224
|
+
raise ChinoError.new(d['error'], response)
|
225
|
+
else
|
226
|
+
raise ChinoError.new(response.body, response)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
return response.body if raw
|
231
|
+
|
232
|
+
begin
|
233
|
+
return JSON.parse(response.body)
|
234
|
+
rescue JSON::ParserError
|
235
|
+
raise ChinoError.new("Unable to parse JSON response: #{response.body}", response)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
#------------------------------APPLICATIONS-----------------------------------#
|
241
|
+
|
242
|
+
class Application
|
243
|
+
include ActiveModel::Serializers::JSON
|
244
|
+
|
245
|
+
attr_accessor :app_name, :app_id, :app_secret, :grant_type, :redirect_url, :client_type
|
246
|
+
|
247
|
+
def attributes=(hash)
|
248
|
+
hash.each do |key, value|
|
249
|
+
send("#{key}=", value)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def attributes
|
254
|
+
instance_values
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
class GetApplicationsResponse
|
259
|
+
include ActiveModel::Serializers::JSON
|
260
|
+
|
261
|
+
attr_accessor :count, :total_count, :limit, :offset, :applications
|
262
|
+
|
263
|
+
def attributes=(hash)
|
264
|
+
hash.each do |key, value|
|
265
|
+
send("#{key}=", value)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def attributes
|
270
|
+
instance_values
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
class Applications < ChinoBaseAPI
|
275
|
+
|
276
|
+
def get_application(app_id)
|
277
|
+
check_string(app_id)
|
278
|
+
app = Application.new
|
279
|
+
app.from_json(get_resource("/auth/applications/#{app_id}").to_json, true)
|
280
|
+
app
|
281
|
+
end
|
282
|
+
|
283
|
+
def list_applications(limit=nil, offset=nil)
|
284
|
+
apps = GetApplicationsResponse.new
|
285
|
+
if limit==nil && offset==nil
|
286
|
+
apps.from_json(get_resource("/auth/applications", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
287
|
+
else
|
288
|
+
apps.from_json(get_resource("/auth/applications", limit, offset).to_json)
|
289
|
+
end
|
290
|
+
as = apps.applications
|
291
|
+
apps.applications = []
|
292
|
+
as.each do |a|
|
293
|
+
app = Application.new
|
294
|
+
app.app_id = a['app_id']
|
295
|
+
app.app_name = a['app_name']
|
296
|
+
apps.applications.push(app)
|
297
|
+
end
|
298
|
+
apps
|
299
|
+
end
|
300
|
+
|
301
|
+
def create_application(name, grant_type, redirect_url)
|
302
|
+
check_string(name)
|
303
|
+
check_string(grant_type)
|
304
|
+
check_string(redirect_url)
|
305
|
+
data = {"name": name, "grant_type": grant_type, "redirect_url": redirect_url}.to_json
|
306
|
+
app = Application.new
|
307
|
+
app.from_json(post_resource("/auth/applications", data).to_json, true)
|
308
|
+
app
|
309
|
+
end
|
310
|
+
|
311
|
+
def update_application(app_id, name, grant_type, redirect_url)
|
312
|
+
check_string(name)
|
313
|
+
check_string(grant_type)
|
314
|
+
check_string(redirect_url)
|
315
|
+
check_string(app_id)
|
316
|
+
data = {"name": name, "grant_type": grant_type, "redirect_url": redirect_url}.to_json
|
317
|
+
app = Application.new
|
318
|
+
app.from_json(put_resource("/auth/applications/#{app_id}", data).to_json, true)
|
319
|
+
app
|
320
|
+
end
|
321
|
+
|
322
|
+
def delete_application(app_id, force)
|
323
|
+
check_string(app_id)
|
324
|
+
check_boolean(force)
|
325
|
+
delete_resource("/auth/applications/#{app_id}", force)
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
#------------------------------AUTH-----------------------------------#
|
330
|
+
|
331
|
+
class LoggedUser
|
332
|
+
include ActiveModel::Serializers::JSON
|
333
|
+
|
334
|
+
attr_accessor :access_token, :token_type, :expires_in, :refresh_token, :scope
|
335
|
+
|
336
|
+
def attributes=(hash)
|
337
|
+
hash.each do |key, value|
|
338
|
+
send("#{key}=", value)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
def attributes
|
343
|
+
instance_values
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
class Auth < ChinoBaseAPI
|
348
|
+
|
349
|
+
def login_password(username, password, application_id, application_secret)
|
350
|
+
check_string(username)
|
351
|
+
check_string(password)
|
352
|
+
check_string(application_id)
|
353
|
+
check_string(application_secret)
|
354
|
+
uri = return_uri("/auth/token/")
|
355
|
+
req = Net::HTTP::Post.new(uri.path)
|
356
|
+
req.basic_auth application_id, application_secret
|
357
|
+
req.set_form_data([["username", username], ["password", password], ["grant_type", "password"]])
|
358
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
359
|
+
http.request(req)
|
360
|
+
}
|
361
|
+
usr = LoggedUser.new
|
362
|
+
usr.from_json((parse_response(res)['data']).to_json)
|
363
|
+
usr
|
364
|
+
end
|
365
|
+
|
366
|
+
def login_authentication_code(code, redirect_url, application_id, application_secret)
|
367
|
+
check_string(code)
|
368
|
+
check_string(redirect_url)
|
369
|
+
check_string(application_id)
|
370
|
+
check_string(application_secret)
|
371
|
+
uri = return_uri("/auth/token/")
|
372
|
+
req = Net::HTTP::Post.new(uri.path)
|
373
|
+
req.basic_auth application_id, application_secret
|
374
|
+
req.set_form_data([["code", code], ["redirect_uri", redirect_url], ["grant_type", "authorization_code"], ["scope", "read write"], ["client_id", application_id], ["client_secret", application_secret]])
|
375
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
376
|
+
http.request(req)
|
377
|
+
}
|
378
|
+
usr = LoggedUser.new
|
379
|
+
usr.from_json((parse_response(res)['data']).to_json)
|
380
|
+
usr
|
381
|
+
end
|
382
|
+
|
383
|
+
def refresh_token(refresh_token, application_id, application_secret)
|
384
|
+
check_string(refresh_token)
|
385
|
+
check_string(application_id)
|
386
|
+
check_string(application_secret)
|
387
|
+
uri = return_uri("/auth/token/")
|
388
|
+
req = Net::HTTP::Post.new(uri.path)
|
389
|
+
req.basic_auth application_id, application_secret
|
390
|
+
req.set_form_data([["refresh_token", refresh_token], ["client_id", application_id], ["client_secret", application_secret], ["grant_type", "refresh_token"]])
|
391
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
392
|
+
http.request(req)
|
393
|
+
}
|
394
|
+
usr = LoggedUser.new
|
395
|
+
usr.from_json((parse_response(res)['data']).to_json)
|
396
|
+
usr
|
397
|
+
end
|
398
|
+
|
399
|
+
def logout(token, application_id, application_secret)
|
400
|
+
check_string(token)
|
401
|
+
check_string(application_id)
|
402
|
+
check_string(application_secret)
|
403
|
+
uri = return_uri("/auth/revoke_token/")
|
404
|
+
req = Net::HTTP::Post.new(uri.path)
|
405
|
+
req.basic_auth application_id, application_secret
|
406
|
+
req.set_form_data([["token", token], ["client_id", application_id], ["client_secret", application_secret]])
|
407
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
408
|
+
http.request(req)
|
409
|
+
}
|
410
|
+
parse_response(res)['result']
|
411
|
+
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
#------------------------------REPOSITORIES-----------------------------------#
|
416
|
+
|
417
|
+
class Repository
|
418
|
+
include ActiveModel::Serializers::JSON
|
419
|
+
|
420
|
+
attr_accessor :repository_id, :description, :is_active, :last_update, :insert_date
|
421
|
+
|
422
|
+
def attributes=(hash)
|
423
|
+
hash.each do |key, value|
|
424
|
+
send("#{key}=", value)
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
def attributes
|
429
|
+
instance_values
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
class GetRepositoriesResponse
|
434
|
+
include ActiveModel::Serializers::JSON
|
435
|
+
|
436
|
+
attr_accessor :count, :total_count, :limit, :offset, :repositories
|
437
|
+
|
438
|
+
def attributes=(hash)
|
439
|
+
hash.each do |key, value|
|
440
|
+
send("#{key}=", value)
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
def attributes
|
445
|
+
instance_values
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
class Repositories < ChinoBaseAPI
|
450
|
+
|
451
|
+
def get_repository(repo_id)
|
452
|
+
check_string(repo_id)
|
453
|
+
repo = Repository.new
|
454
|
+
repo.from_json(get_resource("/repositories/#{repo_id}").to_json, true)
|
455
|
+
repo
|
456
|
+
end
|
457
|
+
|
458
|
+
def list_repositories(limit=nil, offset=nil)
|
459
|
+
repos = GetRepositoriesResponse.new
|
460
|
+
if limit==nil && offset==nil
|
461
|
+
repos.from_json(get_resource("/repositories", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
462
|
+
else
|
463
|
+
repos.from_json(get_resource("/repositories", limit, offset).to_json)
|
464
|
+
end
|
465
|
+
rs = repos.repositories
|
466
|
+
repos.repositories = []
|
467
|
+
rs.each do |r|
|
468
|
+
repo = Repository.new
|
469
|
+
repo.from_json(r.to_json)
|
470
|
+
repos.repositories.push(repo)
|
471
|
+
end
|
472
|
+
repos
|
473
|
+
end
|
474
|
+
|
475
|
+
def create_repository(description)
|
476
|
+
check_string(description)
|
477
|
+
data = {"description": description}.to_json
|
478
|
+
repo = Repository.new
|
479
|
+
repo.from_json(post_resource("/repositories", data).to_json, true)
|
480
|
+
repo
|
481
|
+
end
|
482
|
+
|
483
|
+
def update_repository(repository_id, description, is_active=nil)
|
484
|
+
check_string(repository_id)
|
485
|
+
check_string(description)
|
486
|
+
if is_active.nil?
|
487
|
+
data = {"description": description}.to_json
|
488
|
+
else
|
489
|
+
data = {"description": description, "is_active": is_active}.to_json
|
490
|
+
end
|
491
|
+
repo = Repository.new
|
492
|
+
repo.from_json(put_resource("/repositories/#{repository_id}", data).to_json, true)
|
493
|
+
repo
|
494
|
+
end
|
495
|
+
|
496
|
+
def delete_repository(repository_id, force)
|
497
|
+
check_string(repository_id)
|
498
|
+
check_boolean(force)
|
499
|
+
delete_resource("/repositories/#{repository_id}", force)
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
#------------------------------USER SCHEMAS-----------------------------------#
|
504
|
+
|
505
|
+
class UserSchema
|
506
|
+
include ActiveModel::Serializers::JSON
|
507
|
+
|
508
|
+
attr_accessor :user_schema_id, :description, :is_active, :last_update, :structure, :insert_date, :groups
|
509
|
+
|
510
|
+
def attributes=(hash)
|
511
|
+
hash.each do |key, value|
|
512
|
+
send("#{key}=", value)
|
513
|
+
end
|
514
|
+
end
|
515
|
+
|
516
|
+
def attributes
|
517
|
+
instance_values
|
518
|
+
end
|
519
|
+
|
520
|
+
def getFields()
|
521
|
+
structure['fields']
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
class GetUserSchemasResponse
|
526
|
+
include ActiveModel::Serializers::JSON
|
527
|
+
|
528
|
+
attr_accessor :count, :total_count, :limit, :offset, :user_schemas
|
529
|
+
|
530
|
+
def attributes=(hash)
|
531
|
+
hash.each do |key, value|
|
532
|
+
send("#{key}=", value)
|
533
|
+
end
|
534
|
+
end
|
535
|
+
|
536
|
+
def attributes
|
537
|
+
instance_values
|
538
|
+
end
|
539
|
+
end
|
540
|
+
|
541
|
+
class UserSchemas < ChinoBaseAPI
|
542
|
+
|
543
|
+
def get_user_schema(user_schema_id)
|
544
|
+
check_string(user_schema_id)
|
545
|
+
u = UserSchema.new
|
546
|
+
u.from_json(get_resource("/user_schemas/#{user_schema_id}").to_json, true)
|
547
|
+
u
|
548
|
+
end
|
549
|
+
|
550
|
+
def list_user_schemas(limit=nil, offset=nil)
|
551
|
+
schemas = GetUserSchemasResponse.new
|
552
|
+
if limit==nil && offset==nil
|
553
|
+
schemas.from_json(get_resource("/user_schemas", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
554
|
+
else
|
555
|
+
schemas.from_json(get_resource("/user_schemas", limit, offset).to_json)
|
556
|
+
end
|
557
|
+
us = schemas.user_schemas
|
558
|
+
schemas.user_schemas = []
|
559
|
+
us.each do |u|
|
560
|
+
schema = UserSchema.new
|
561
|
+
schema.from_json(u.to_json)
|
562
|
+
schemas.user_schemas.push(schema)
|
563
|
+
end
|
564
|
+
schemas
|
565
|
+
end
|
566
|
+
|
567
|
+
def create_user_schema(description, fields)
|
568
|
+
check_string(description)
|
569
|
+
check_json(fields)
|
570
|
+
data = {"description": description, "structure": { "fields": fields}}.to_json
|
571
|
+
schema = UserSchema.new
|
572
|
+
schema.from_json(post_resource("/user_schemas", data).to_json, true)
|
573
|
+
schema
|
574
|
+
end
|
575
|
+
|
576
|
+
def update_user_schema(user_schema_id, description, fields)
|
577
|
+
check_string(user_schema_id)
|
578
|
+
check_string(description)
|
579
|
+
check_json(fields)
|
580
|
+
data = {"description": description, "structure": { "fields": fields}}.to_json
|
581
|
+
schema = UserSchema.new
|
582
|
+
schema.from_json(put_resource("/user_schemas/#{user_schema_id}", data).to_json, true)
|
583
|
+
schema
|
584
|
+
end
|
585
|
+
|
586
|
+
def delete_user_schema(user_schema_id, force)
|
587
|
+
check_string(user_schema_id)
|
588
|
+
check_boolean(force)
|
589
|
+
delete_resource("/user_schemas/#{user_schema_id}", force)
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
#------------------------------USERS-----------------------------------#
|
594
|
+
|
595
|
+
class User
|
596
|
+
include ActiveModel::Serializers::JSON
|
597
|
+
|
598
|
+
attr_accessor :username, :user_id, :schema_id, :is_active, :last_update, :user_attributes, :insert_date, :groups
|
599
|
+
|
600
|
+
def attributes=(hash)
|
601
|
+
hash.each do |key, value|
|
602
|
+
if key=="attributes"
|
603
|
+
@user_attributes = value
|
604
|
+
else
|
605
|
+
send("#{key}=", value)
|
606
|
+
end
|
607
|
+
end
|
608
|
+
end
|
609
|
+
|
610
|
+
def attributes
|
611
|
+
instance_values
|
612
|
+
end
|
613
|
+
end
|
614
|
+
|
615
|
+
class GetUsersResponse
|
616
|
+
include ActiveModel::Serializers::JSON
|
617
|
+
|
618
|
+
attr_accessor :count, :total_count, :limit, :offset, :users, :exists
|
619
|
+
|
620
|
+
def attributes=(hash)
|
621
|
+
hash.each do |key, value|
|
622
|
+
send("#{key}=", value)
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
def attributes
|
627
|
+
instance_values
|
628
|
+
end
|
629
|
+
end
|
630
|
+
|
631
|
+
class Users < ChinoBaseAPI
|
632
|
+
|
633
|
+
def me
|
634
|
+
u = User.new
|
635
|
+
u.from_json(get_resource("/users/me").to_json, true)
|
636
|
+
u
|
637
|
+
end
|
638
|
+
|
639
|
+
def get_user(user_id)
|
640
|
+
check_string(user_id)
|
641
|
+
u = User.new
|
642
|
+
u.from_json(get_resource("/users/#{user_id}").to_json, true)
|
643
|
+
u
|
644
|
+
end
|
645
|
+
|
646
|
+
def list_users(user_schema_id, limit=nil, offset=nil)
|
647
|
+
check_string(user_schema_id)
|
648
|
+
users = GetUsersResponse.new
|
649
|
+
if limit==nil && offset==nil
|
650
|
+
users.from_json(get_resource("/user_schemas/#{user_schema_id}/users", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
651
|
+
else
|
652
|
+
users.from_json(get_resource("/user_schemas/#{user_schema_id}/users", limit, offset).to_json)
|
653
|
+
end
|
654
|
+
us = users.users
|
655
|
+
users.users = []
|
656
|
+
us.each do |u|
|
657
|
+
user = User.new
|
658
|
+
user.from_json(u.to_json)
|
659
|
+
users.users.push(user)
|
660
|
+
end
|
661
|
+
users
|
662
|
+
end
|
663
|
+
|
664
|
+
def create_user(user_schema_id, username, password, attributes)
|
665
|
+
check_string(user_schema_id)
|
666
|
+
check_string(username)
|
667
|
+
check_string(password)
|
668
|
+
check_json(attributes)
|
669
|
+
data = {"username": username, "password": password, "attributes": attributes}.to_json
|
670
|
+
user = User.new
|
671
|
+
user.from_json(post_resource("/user_schemas/#{user_schema_id}/users", data).to_json, true)
|
672
|
+
user
|
673
|
+
end
|
674
|
+
|
675
|
+
def update_user(user_id, username, password, attributes)
|
676
|
+
check_string(user_id)
|
677
|
+
check_string(username)
|
678
|
+
check_string(password)
|
679
|
+
check_json(attributes)
|
680
|
+
data = {"username": username, "password": password, "attributes": attributes}.to_json
|
681
|
+
user = User.new
|
682
|
+
user.from_json(put_resource("/users/#{user_id}", data).to_json, true)
|
683
|
+
user
|
684
|
+
end
|
685
|
+
|
686
|
+
def update_user_partial(user_id, attributes=nil, username=nil, password=nil)
|
687
|
+
check_string(user_id)
|
688
|
+
data = Hash.new
|
689
|
+
if attributes
|
690
|
+
check_json(attributes)
|
691
|
+
data['attributes'] = attributes
|
692
|
+
end
|
693
|
+
if username
|
694
|
+
check_string(username)
|
695
|
+
data['username'] = username
|
696
|
+
end
|
697
|
+
if password
|
698
|
+
check_string(password)
|
699
|
+
data['password'] = password
|
700
|
+
end
|
701
|
+
data = data.to_json
|
702
|
+
|
703
|
+
user = User.new
|
704
|
+
user.from_json(patch_resource("/users/#{user_id}", data).to_json, true)
|
705
|
+
user
|
706
|
+
end
|
707
|
+
|
708
|
+
def delete_user(user_id, force)
|
709
|
+
check_string(user_id)
|
710
|
+
check_boolean(force)
|
711
|
+
delete_resource("/users/#{user_id}", force)
|
712
|
+
end
|
713
|
+
end
|
714
|
+
|
715
|
+
#------------------------------GROUPS-----------------------------------#
|
716
|
+
|
717
|
+
class Group
|
718
|
+
include ActiveModel::Serializers::JSON
|
719
|
+
|
720
|
+
attr_accessor :group_name, :group_id, :is_active, :last_update, :group_attributes, :insert_date
|
721
|
+
|
722
|
+
def attributes=(hash)
|
723
|
+
hash.each do |key, value|
|
724
|
+
if key=="attributes"
|
725
|
+
@group_attributes = value
|
726
|
+
else
|
727
|
+
send("#{key}=", value)
|
728
|
+
end
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
def attributes
|
733
|
+
instance_values
|
734
|
+
end
|
735
|
+
end
|
736
|
+
|
737
|
+
class GetGroupsResponse
|
738
|
+
include ActiveModel::Serializers::JSON
|
739
|
+
|
740
|
+
attr_accessor :count, :total_count, :limit, :offset, :groups
|
741
|
+
|
742
|
+
def attributes=(hash)
|
743
|
+
hash.each do |key, value|
|
744
|
+
send("#{key}=", value)
|
745
|
+
end
|
746
|
+
end
|
747
|
+
|
748
|
+
def attributes
|
749
|
+
instance_values
|
750
|
+
end
|
751
|
+
end
|
752
|
+
|
753
|
+
class Groups < ChinoBaseAPI
|
754
|
+
|
755
|
+
def get_group(group_id)
|
756
|
+
check_string(group_id)
|
757
|
+
g = Group.new
|
758
|
+
g.from_json(get_resource("/groups/#{group_id}").to_json, true)
|
759
|
+
g
|
760
|
+
end
|
761
|
+
|
762
|
+
def list_groups(limit=nil, offset=nil)
|
763
|
+
groups = GetGroupsResponse.new
|
764
|
+
if limit==nil && offset==nil
|
765
|
+
groups.from_json(get_resource("/groups", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
766
|
+
else
|
767
|
+
groups.from_json(get_resource("/groups", limit, offset).to_json)
|
768
|
+
end
|
769
|
+
gs = groups.groups
|
770
|
+
groups.groups = []
|
771
|
+
gs.each do |g|
|
772
|
+
group = Group.new
|
773
|
+
group.from_json(g.to_json)
|
774
|
+
groups.groups.push(group)
|
775
|
+
end
|
776
|
+
groups
|
777
|
+
end
|
778
|
+
|
779
|
+
def create_group(group_name, attributes)
|
780
|
+
check_string(group_name)
|
781
|
+
check_json(attributes)
|
782
|
+
data = {"group_name": group_name, "attributes": attributes}.to_json
|
783
|
+
group = Group.new
|
784
|
+
group.from_json(post_resource("/groups", data).to_json, true)
|
785
|
+
group
|
786
|
+
end
|
787
|
+
|
788
|
+
def update_group(group_id, group_name, attributes)
|
789
|
+
check_string(group_id)
|
790
|
+
check_string(group_name)
|
791
|
+
check_json(attributes)
|
792
|
+
data = {"group_name": group_name, "attributes": attributes}.to_json
|
793
|
+
group = Group.new
|
794
|
+
group.from_json(put_resource("/groups/#{group_id}", data).to_json, true)
|
795
|
+
group
|
796
|
+
end
|
797
|
+
|
798
|
+
def delete_group(group_id, force)
|
799
|
+
check_string(group_id)
|
800
|
+
check_boolean(force)
|
801
|
+
delete_resource("/groups/#{group_id}", force)
|
802
|
+
end
|
803
|
+
|
804
|
+
def add_user_to_group(user_id, group_id)
|
805
|
+
check_string(group_id)
|
806
|
+
check_string(user_id)
|
807
|
+
post_resource("/groups/#{group_id}/users/#{user_id}")
|
808
|
+
end
|
809
|
+
|
810
|
+
def add_user_schema_to_group(user_schema_id, group_id)
|
811
|
+
check_string(group_id)
|
812
|
+
check_string(user_schema_id)
|
813
|
+
post_resource("/groups/#{group_id}/user_schemas/#{user_schema_id}")
|
814
|
+
end
|
815
|
+
|
816
|
+
def remove_user_from_group(user_id, group_id)
|
817
|
+
check_string(group_id)
|
818
|
+
check_string(user_id)
|
819
|
+
delete_resource("/groups/#{group_id}/users/#{user_id}", false)
|
820
|
+
end
|
821
|
+
|
822
|
+
def remove_user_schema_from_group(user_schema_id, group_id)
|
823
|
+
check_string(group_id)
|
824
|
+
check_string(user_schema_id)
|
825
|
+
delete_resource("/groups/#{group_id}/user_schemas/#{user_schema_id}", false)
|
826
|
+
end
|
827
|
+
end
|
828
|
+
|
829
|
+
#------------------------------SCHEMAS-----------------------------------#
|
830
|
+
|
831
|
+
class Schema
|
832
|
+
include ActiveModel::Serializers::JSON
|
833
|
+
|
834
|
+
attr_accessor :repository_id, :schema_id, :description, :is_active, :last_update, :structure, :insert_date
|
835
|
+
|
836
|
+
def attributes=(hash)
|
837
|
+
hash.each do |key, value|
|
838
|
+
send("#{key}=", value)
|
839
|
+
end
|
840
|
+
end
|
841
|
+
|
842
|
+
def attributes
|
843
|
+
instance_values
|
844
|
+
end
|
845
|
+
|
846
|
+
def getFields()
|
847
|
+
structure['fields']
|
848
|
+
end
|
849
|
+
end
|
850
|
+
|
851
|
+
class GetSchemasResponse
|
852
|
+
include ActiveModel::Serializers::JSON
|
853
|
+
|
854
|
+
attr_accessor :count, :total_count, :limit, :offset, :schemas
|
855
|
+
|
856
|
+
def attributes=(hash)
|
857
|
+
hash.each do |key, value|
|
858
|
+
send("#{key}=", value)
|
859
|
+
end
|
860
|
+
end
|
861
|
+
|
862
|
+
def attributes
|
863
|
+
instance_values
|
864
|
+
end
|
865
|
+
end
|
866
|
+
|
867
|
+
class Schemas < ChinoBaseAPI
|
868
|
+
|
869
|
+
def get_schema(schema_id)
|
870
|
+
check_string(schema_id)
|
871
|
+
s = Schema.new
|
872
|
+
s.from_json(get_resource("/schemas/#{schema_id}").to_json, true)
|
873
|
+
s
|
874
|
+
end
|
875
|
+
|
876
|
+
def list_schemas(repository_id, limit=nil, offset=nil)
|
877
|
+
check_string(repository_id)
|
878
|
+
schemas = GetSchemasResponse.new
|
879
|
+
if limit==nil && offset==nil
|
880
|
+
schemas.from_json(get_resource("/repositories/#{repository_id}/schemas", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
881
|
+
else
|
882
|
+
schemas.from_json(get_resource("/repositories/#{repository_id}/schemas", limit, offset).to_json)
|
883
|
+
end
|
884
|
+
ss = schemas.schemas
|
885
|
+
schemas.schemas = []
|
886
|
+
ss.each do |s|
|
887
|
+
schema = Schema.new
|
888
|
+
schema.from_json(s.to_json)
|
889
|
+
schemas.schemas.push(schema)
|
890
|
+
end
|
891
|
+
schemas
|
892
|
+
end
|
893
|
+
|
894
|
+
def create_schema(repository_id, description, fields)
|
895
|
+
check_string(repository_id)
|
896
|
+
check_string(description)
|
897
|
+
check_json(fields)
|
898
|
+
data = {"description": description, "structure": { "fields": fields}}.to_json
|
899
|
+
schema = Schema.new
|
900
|
+
schema.from_json(post_resource("/repositories/#{repository_id}/schemas", data).to_json, true)
|
901
|
+
schema
|
902
|
+
end
|
903
|
+
|
904
|
+
def update_schema(schema_id, description, fields, is_active=nil)
|
905
|
+
check_string(schema_id)
|
906
|
+
check_string(description)
|
907
|
+
check_json(fields)
|
908
|
+
if is_active.nil?
|
909
|
+
data = {"description": description, "structure": { "fields": fields}}.to_json
|
910
|
+
else
|
911
|
+
data = {"description": description, "structure": { "fields": fields}, "is_active": is_active}.to_json
|
912
|
+
end
|
913
|
+
schema = Schema.new
|
914
|
+
schema.from_json(put_resource("/schemas/#{schema_id}", data).to_json, true)
|
915
|
+
schema
|
916
|
+
end
|
917
|
+
|
918
|
+
def delete_schema(schema_id, force)
|
919
|
+
check_string(schema_id)
|
920
|
+
check_boolean(force)
|
921
|
+
delete_resource("/schemas/#{schema_id}", force)
|
922
|
+
end
|
923
|
+
end
|
924
|
+
|
925
|
+
#------------------------------DOCUMENTS-----------------------------------#
|
926
|
+
|
927
|
+
class Document
|
928
|
+
include ActiveModel::Serializers::JSON
|
929
|
+
|
930
|
+
attr_accessor :repository_id, :schema_id, :document_id, :is_active, :last_update, :content, :insert_date
|
931
|
+
|
932
|
+
def attributes=(hash)
|
933
|
+
hash.each do |key, value|
|
934
|
+
if key=="content"
|
935
|
+
@content = value
|
936
|
+
else
|
937
|
+
send("#{key}=", value)
|
938
|
+
end
|
939
|
+
end
|
940
|
+
end
|
941
|
+
|
942
|
+
def attributes
|
943
|
+
instance_values
|
944
|
+
end
|
945
|
+
end
|
946
|
+
|
947
|
+
class GetDocumentsResponse
|
948
|
+
include ActiveModel::Serializers::JSON
|
949
|
+
|
950
|
+
attr_accessor :count, :total_count, :limit, :offset, :documents, :IDs
|
951
|
+
|
952
|
+
def attributes=(hash)
|
953
|
+
hash.each do |key, value|
|
954
|
+
send("#{key}=", value)
|
955
|
+
end
|
956
|
+
end
|
957
|
+
|
958
|
+
def attributes
|
959
|
+
instance_values
|
960
|
+
end
|
961
|
+
end
|
962
|
+
|
963
|
+
class Documents < ChinoBaseAPI
|
964
|
+
|
965
|
+
def get_document(document_id)
|
966
|
+
check_string(document_id)
|
967
|
+
d = Document.new
|
968
|
+
d.from_json(get_resource("/documents/#{document_id}").to_json, true)
|
969
|
+
d
|
970
|
+
end
|
971
|
+
|
972
|
+
def list_documents(schema_id, full_document, limit=nil, offset=nil)
|
973
|
+
check_string(schema_id)
|
974
|
+
check_boolean(full_document)
|
975
|
+
docs = GetDocumentsResponse.new
|
976
|
+
if limit==nil && offset==nil
|
977
|
+
if full_document
|
978
|
+
docs.from_json(get_resource("/schemas/#{schema_id}/documents", ChinoRuby::QUERY_DEFAULT_LIMIT, 0, true).to_json)
|
979
|
+
else
|
980
|
+
docs.from_json(get_resource("/schemas/#{schema_id}/documents", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
981
|
+
end
|
982
|
+
else
|
983
|
+
if full_document
|
984
|
+
docs.from_json(get_resource("/schemas/#{schema_id}/documents", limit, offset, true).to_json)
|
985
|
+
else
|
986
|
+
docs.from_json(get_resource("/schemas/#{schema_id}/documents", limit, offset).to_json)
|
987
|
+
end
|
988
|
+
end
|
989
|
+
ds = docs.documents
|
990
|
+
docs.documents = []
|
991
|
+
ds.each do |d|
|
992
|
+
doc = Document.new
|
993
|
+
doc.from_json(d.to_json)
|
994
|
+
docs.documents.push(doc)
|
995
|
+
end
|
996
|
+
docs
|
997
|
+
end
|
998
|
+
|
999
|
+
def create_document(schema_id, content)
|
1000
|
+
check_string(schema_id)
|
1001
|
+
check_json(content)
|
1002
|
+
data = {"content": content}.to_json
|
1003
|
+
document = Document.new
|
1004
|
+
document.from_json(post_resource("/schemas/#{schema_id}/documents", data).to_json, true)
|
1005
|
+
document
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
def update_document(document_id, content, is_active=nil)
|
1009
|
+
check_string(document_id)
|
1010
|
+
check_json(content)
|
1011
|
+
if is_active.nil?
|
1012
|
+
data = {"content": content}.to_json
|
1013
|
+
else
|
1014
|
+
data = {"content": content, "is_active": is_active}.to_json
|
1015
|
+
end
|
1016
|
+
document = Document.new
|
1017
|
+
document.from_json(put_resource("/documents/#{document_id}", data).to_json, true)
|
1018
|
+
document
|
1019
|
+
end
|
1020
|
+
|
1021
|
+
def delete_document(document_id, force)
|
1022
|
+
check_string(document_id)
|
1023
|
+
check_boolean(force)
|
1024
|
+
delete_resource("/documents/#{document_id}", force)
|
1025
|
+
end
|
1026
|
+
end
|
1027
|
+
|
1028
|
+
#------------------------------COLLECTIONS-----------------------------------#
|
1029
|
+
|
1030
|
+
class Collection
|
1031
|
+
include ActiveModel::Serializers::JSON
|
1032
|
+
|
1033
|
+
attr_accessor :collection_id, :name, :is_active, :last_update, :insert_date
|
1034
|
+
|
1035
|
+
def attributes=(hash)
|
1036
|
+
hash.each do |key, value|
|
1037
|
+
send("#{key}=", value)
|
1038
|
+
end
|
1039
|
+
end
|
1040
|
+
|
1041
|
+
def attributes
|
1042
|
+
instance_values
|
1043
|
+
end
|
1044
|
+
end
|
1045
|
+
|
1046
|
+
class GetCollectionsResponse
|
1047
|
+
include ActiveModel::Serializers::JSON
|
1048
|
+
|
1049
|
+
attr_accessor :count, :total_count, :limit, :offset, :collections
|
1050
|
+
|
1051
|
+
def attributes=(hash)
|
1052
|
+
hash.each do |key, value|
|
1053
|
+
send("#{key}=", value)
|
1054
|
+
end
|
1055
|
+
end
|
1056
|
+
|
1057
|
+
def attributes
|
1058
|
+
instance_values
|
1059
|
+
end
|
1060
|
+
end
|
1061
|
+
|
1062
|
+
class Collections < ChinoBaseAPI
|
1063
|
+
|
1064
|
+
def get_collection(collection_id)
|
1065
|
+
check_string(collection_id)
|
1066
|
+
col = Collection.new
|
1067
|
+
col.from_json(get_resource("/collections/#{collection_id}").to_json, true)
|
1068
|
+
col
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
def list_collections(limit=nil, offset=nil)
|
1072
|
+
cols = GetCollectionsResponse.new
|
1073
|
+
if limit==nil && offset==nil
|
1074
|
+
cols.from_json(get_resource("/collections", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1075
|
+
else
|
1076
|
+
cols.from_json(get_resource("/collections", limit, offset).to_json)
|
1077
|
+
end
|
1078
|
+
cs = cols.collections
|
1079
|
+
cols.collections = []
|
1080
|
+
cs.each do |c|
|
1081
|
+
col = Collection.new
|
1082
|
+
col.from_json(c.to_json)
|
1083
|
+
cols.collections.push(col)
|
1084
|
+
end
|
1085
|
+
cols
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
def create_collection(name)
|
1089
|
+
check_string(name)
|
1090
|
+
data = {"name": name}.to_json
|
1091
|
+
col = Collection.new
|
1092
|
+
col.from_json(post_resource("/collections", data).to_json, true)
|
1093
|
+
col
|
1094
|
+
end
|
1095
|
+
|
1096
|
+
def update_collection(collection_id, name)
|
1097
|
+
check_string(collection_id)
|
1098
|
+
check_string(name)
|
1099
|
+
data = {"name": name}.to_json
|
1100
|
+
col = Collection.new
|
1101
|
+
col.from_json(put_resource("/collections/#{collection_id}", data).to_json, true)
|
1102
|
+
col
|
1103
|
+
end
|
1104
|
+
|
1105
|
+
def delete_collection(collection_id, force)
|
1106
|
+
check_string(collection_id)
|
1107
|
+
check_boolean(force)
|
1108
|
+
delete_resource("/collections/#{collection_id}", force)
|
1109
|
+
end
|
1110
|
+
|
1111
|
+
def add_document(document_id, collection_id)
|
1112
|
+
check_string(document_id)
|
1113
|
+
check_string(collection_id)
|
1114
|
+
post_resource("/collections/#{collection_id}/documents/#{document_id}")
|
1115
|
+
end
|
1116
|
+
|
1117
|
+
def remove_document(document_id, collection_id)
|
1118
|
+
check_string(document_id)
|
1119
|
+
check_string(collection_id)
|
1120
|
+
delete_resource("/collections/#{collection_id}/documents/#{document_id}", false)
|
1121
|
+
end
|
1122
|
+
|
1123
|
+
def list_documents(collection_id, limit=nil, offset=nil)
|
1124
|
+
check_string(collection_id)
|
1125
|
+
docs = GetDocumentsResponse.new
|
1126
|
+
if limit==nil && offset==nil
|
1127
|
+
docs.from_json(get_resource("/collections/#{collection_id}/documents", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1128
|
+
else
|
1129
|
+
docs.from_json(get_resource("/collections/#{collection_id}/documents", limit, offset).to_json)
|
1130
|
+
end
|
1131
|
+
ds = docs.documents
|
1132
|
+
docs.documents = []
|
1133
|
+
ds.each do |d|
|
1134
|
+
doc = Document.new
|
1135
|
+
doc.from_json(d.to_json)
|
1136
|
+
docs.documents.push(doc)
|
1137
|
+
end
|
1138
|
+
docs
|
1139
|
+
end
|
1140
|
+
end
|
1141
|
+
|
1142
|
+
#------------------------------PERMISSIONS-----------------------------------#
|
1143
|
+
|
1144
|
+
class Permission
|
1145
|
+
include ActiveModel::Serializers::JSON
|
1146
|
+
|
1147
|
+
attr_accessor :access, :parent_id, :resource_id, :resource_type, :permission
|
1148
|
+
|
1149
|
+
def attributes=(hash)
|
1150
|
+
hash.each do |key, value|
|
1151
|
+
send("#{key}=", value)
|
1152
|
+
end
|
1153
|
+
end
|
1154
|
+
|
1155
|
+
def attributes
|
1156
|
+
instance_values
|
1157
|
+
end
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
class GetPermissionsResponse
|
1161
|
+
include ActiveModel::Serializers::JSON
|
1162
|
+
|
1163
|
+
attr_accessor :count, :total_count, :limit, :offset, :permissions
|
1164
|
+
|
1165
|
+
def attributes=(hash)
|
1166
|
+
hash.each do |key, value|
|
1167
|
+
send("#{key}=", value)
|
1168
|
+
end
|
1169
|
+
end
|
1170
|
+
|
1171
|
+
def attributes
|
1172
|
+
instance_values
|
1173
|
+
end
|
1174
|
+
end
|
1175
|
+
|
1176
|
+
class Permissions < ChinoBaseAPI
|
1177
|
+
def list_permissions(limit=nil, offset=nil)
|
1178
|
+
perms = GetPermissionsResponse.new
|
1179
|
+
if limit==nil && offset==nil
|
1180
|
+
perms.from_json(get_resource("/perms", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1181
|
+
else
|
1182
|
+
perms.from_json(get_resource("/perms", limit, offset).to_json)
|
1183
|
+
end
|
1184
|
+
ps = perms.permissions
|
1185
|
+
perms.permissions = []
|
1186
|
+
ps.each do |p|
|
1187
|
+
perm = Permission.new
|
1188
|
+
perm.from_json(p.to_json)
|
1189
|
+
perms.permissions.push(perm)
|
1190
|
+
end
|
1191
|
+
perms
|
1192
|
+
end
|
1193
|
+
|
1194
|
+
def read_permissions_on_a_document(document_id, limit=nil, offset=nil)
|
1195
|
+
check_string(document_id)
|
1196
|
+
perms = GetPermissionsResponse.new
|
1197
|
+
if limit==nil && offset==nil
|
1198
|
+
perms.from_json(get_resource("/perms/documents/#{document_id}", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1199
|
+
else
|
1200
|
+
perms.from_json(get_resource("/perms/documents/#{document_id}", limit, offset).to_json)
|
1201
|
+
end
|
1202
|
+
ps = perms.permissions
|
1203
|
+
perms.permissions = []
|
1204
|
+
ps.each do |p|
|
1205
|
+
perm = Permission.new
|
1206
|
+
perm.from_json(p.to_json)
|
1207
|
+
perms.permissions.push(perm)
|
1208
|
+
end
|
1209
|
+
perms
|
1210
|
+
end
|
1211
|
+
|
1212
|
+
def read_permissions_of_a_user(user_id, limit=nil, offset=nil)
|
1213
|
+
check_string(user_id)
|
1214
|
+
perms = GetPermissionsResponse.new
|
1215
|
+
if limit==nil && offset==nil
|
1216
|
+
perms.from_json(get_resource("/perms/users/#{user_id}", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1217
|
+
else
|
1218
|
+
perms.from_json(get_resource("/perms/users/#{user_id}", limit, offset).to_json)
|
1219
|
+
end
|
1220
|
+
ps = perms.permissions
|
1221
|
+
perms.permissions = []
|
1222
|
+
ps.each do |p|
|
1223
|
+
perm = Permission.new
|
1224
|
+
perm.from_json(p.to_json)
|
1225
|
+
perms.permissions.push(perm)
|
1226
|
+
end
|
1227
|
+
perms
|
1228
|
+
end
|
1229
|
+
|
1230
|
+
def read_permissions_of_a_group(group_id, limit=nil, offset=nil)
|
1231
|
+
check_string(group_id)
|
1232
|
+
perms = GetPermissionsResponse.new
|
1233
|
+
if limit==nil && offset==nil
|
1234
|
+
perms.from_json(get_resource("/perms/groups/#{group_id}", ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1235
|
+
else
|
1236
|
+
perms.from_json(get_resource("/perms/groups/#{group_id}", limit, offset).to_json)
|
1237
|
+
end
|
1238
|
+
ps = perms.permissions
|
1239
|
+
perms.permissions = []
|
1240
|
+
ps.each do |p|
|
1241
|
+
perm = Permission.new
|
1242
|
+
perm.from_json(p.to_json)
|
1243
|
+
perms.permissions.push(perm)
|
1244
|
+
end
|
1245
|
+
perms
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
def permissions_on_resources(action, resource_type, subject_type, subject_id, manage, authorize)
|
1249
|
+
check_string(action)
|
1250
|
+
check_string(resource_type)
|
1251
|
+
check_string(subject_type)
|
1252
|
+
check_string(subject_id)
|
1253
|
+
check_json(manage)
|
1254
|
+
check_json(authorize)
|
1255
|
+
data = {"manage": manage, "authorize": authorize}.to_json
|
1256
|
+
post_resource_with_string_result("/perms/#{action}/#{resource_type}/#{subject_type}/#{subject_id}", data)
|
1257
|
+
end
|
1258
|
+
|
1259
|
+
def permissions_on_a_resource(action, resource_type, resource_id, subject_type, subject_id, manage, authorize)
|
1260
|
+
check_string(action)
|
1261
|
+
check_string(resource_type)
|
1262
|
+
check_string(resource_id)
|
1263
|
+
check_string(subject_type)
|
1264
|
+
check_string(subject_id)
|
1265
|
+
check_json(manage)
|
1266
|
+
check_json(authorize)
|
1267
|
+
data = {"manage": manage, "authorize": authorize}.to_json
|
1268
|
+
post_resource_with_string_result("/perms/#{action}/#{resource_type}/#{resource_id}/#{subject_type}/#{subject_id}", data)
|
1269
|
+
end
|
1270
|
+
|
1271
|
+
def permissions_on_a_resource_children(action, resource_type, resource_id, resource_children, subject_type, subject_id, manage, authorize)
|
1272
|
+
check_string(action)
|
1273
|
+
check_string(resource_type)
|
1274
|
+
check_string(resource_id)
|
1275
|
+
check_string(resource_children)
|
1276
|
+
check_string(subject_type)
|
1277
|
+
check_string(subject_id)
|
1278
|
+
check_json(manage)
|
1279
|
+
check_json(authorize)
|
1280
|
+
data = {"manage": manage, "authorize": authorize}.to_json
|
1281
|
+
post_resource_with_string_result("/perms/#{action}/#{resource_type}/#{resource_id}/#{resource_children}/#{subject_type}/#{subject_id}", data)
|
1282
|
+
end
|
1283
|
+
|
1284
|
+
def permissions_on_a_resource_children_created_document(action, resource_type, resource_id, resource_children, subject_type, subject_id, manage, authorize, manage_created_document, authorize_created_document)
|
1285
|
+
check_string(action)
|
1286
|
+
check_string(resource_type)
|
1287
|
+
check_string(resource_id)
|
1288
|
+
check_string(resource_children)
|
1289
|
+
check_string(subject_type)
|
1290
|
+
check_string(subject_id)
|
1291
|
+
check_json(manage)
|
1292
|
+
check_json(authorize)
|
1293
|
+
data = {"manage": manage, "authorize": authorize, "created_document": { "manage": manage_created_document, "authorize": authorize_created_document}}.to_json
|
1294
|
+
post_resource_with_string_result("/perms/#{action}/#{resource_type}/#{resource_id}/#{resource_children}/#{subject_type}/#{subject_id}", data)
|
1295
|
+
end
|
1296
|
+
|
1297
|
+
end
|
1298
|
+
|
1299
|
+
#------------------------------SEARCH-----------------------------------#
|
1300
|
+
|
1301
|
+
class FilterOption < CheckValues
|
1302
|
+
attr_accessor :field, :type, :value
|
1303
|
+
|
1304
|
+
def initialize(field, type, value)
|
1305
|
+
check_string(field)
|
1306
|
+
check_string(type)
|
1307
|
+
check_json(value)
|
1308
|
+
self.field = field
|
1309
|
+
self.type = type
|
1310
|
+
self.value = value
|
1311
|
+
end
|
1312
|
+
|
1313
|
+
def to_json
|
1314
|
+
{"field": field, "type": type, "value": value}.to_json
|
1315
|
+
end
|
1316
|
+
end
|
1317
|
+
|
1318
|
+
class SortOption < CheckValues
|
1319
|
+
attr_accessor :field, :order
|
1320
|
+
|
1321
|
+
def initialize(field, order)
|
1322
|
+
check_string(field)
|
1323
|
+
check_string(order)
|
1324
|
+
self.field = field
|
1325
|
+
self.order = order
|
1326
|
+
end
|
1327
|
+
|
1328
|
+
def to_json
|
1329
|
+
{"field": field, "order": order}.to_json
|
1330
|
+
end
|
1331
|
+
end
|
1332
|
+
|
1333
|
+
class Search < ChinoBaseAPI
|
1334
|
+
def search_documents(schema_id, result_type, filter_type, sort, filter, limit=nil, offset=nil)
|
1335
|
+
check_string(schema_id)
|
1336
|
+
check_string(result_type)
|
1337
|
+
check_string(filter_type)
|
1338
|
+
check_json(sort)
|
1339
|
+
check_json(filter)
|
1340
|
+
data = {"result_type": result_type, "filter_type": filter_type, "filter": filter, "sort": sort}.to_json
|
1341
|
+
docs = GetDocumentsResponse.new
|
1342
|
+
if limit==nil && offset==nil
|
1343
|
+
docs.from_json(post_resource("/search/documents/#{schema_id}", data, ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1344
|
+
else
|
1345
|
+
docs.from_json(post_resource("/search/documents/#{schema_id}", data, limit, offset).to_json)
|
1346
|
+
end
|
1347
|
+
ds = docs.documents
|
1348
|
+
docs.documents = []
|
1349
|
+
ds.each do |d|
|
1350
|
+
doc = Document.new
|
1351
|
+
doc.from_json(d.to_json)
|
1352
|
+
docs.documents.push(doc)
|
1353
|
+
end
|
1354
|
+
docs
|
1355
|
+
end
|
1356
|
+
|
1357
|
+
##
|
1358
|
+
# The new Search API accept a query hash, documented here:
|
1359
|
+
# https://console.test.chino.io/docs/v1#header-search-query
|
1360
|
+
# query hash need to be converted to json
|
1361
|
+
# e.g. to build this JSON query
|
1362
|
+
# {
|
1363
|
+
# "and": [
|
1364
|
+
# {
|
1365
|
+
# "field": "user_id",
|
1366
|
+
# "type": "eq",
|
1367
|
+
# "value": "aaaaaaa"
|
1368
|
+
# },
|
1369
|
+
# {
|
1370
|
+
# "or": [
|
1371
|
+
# {
|
1372
|
+
# "field": "first_name",
|
1373
|
+
# "type": "like",
|
1374
|
+
# "value": "*Mario*"
|
1375
|
+
# },
|
1376
|
+
# {
|
1377
|
+
# "field": "last_name",
|
1378
|
+
# "type": "like",
|
1379
|
+
# "value": "*Mario*"
|
1380
|
+
# }
|
1381
|
+
# ]
|
1382
|
+
# }
|
1383
|
+
# ]
|
1384
|
+
# }
|
1385
|
+
#
|
1386
|
+
# You can use code like this:
|
1387
|
+
#
|
1388
|
+
# query = { and: [] }
|
1389
|
+
# query[:and] << ChinoFilterOption.new('user_id', 'eq', "aaaaaaa")
|
1390
|
+
#
|
1391
|
+
# or_conditions = { or: [] }
|
1392
|
+
# or_conditions[:or] << ChinoFilterOption.new('first_name', 'like', "*Mario*")
|
1393
|
+
# or_conditions[:or] << ChinoFilterOption.new('last_name', 'like', "*Mario*")
|
1394
|
+
# query[:and] << or_conditions
|
1395
|
+
def complex_search_documents(schema_id, result_type, sort, query, limit=nil, offset=nil)
|
1396
|
+
check_string(schema_id)
|
1397
|
+
check_string(result_type)
|
1398
|
+
check_json(sort)
|
1399
|
+
check_json(query)
|
1400
|
+
data = { result_type: result_type, query: query, sort: sort}.to_json
|
1401
|
+
|
1402
|
+
docs = GetDocumentsResponse.new
|
1403
|
+
docs.from_json(post_resource("/search/documents/#{schema_id}", data, limit || ChinoRuby::QUERY_DEFAULT_LIMIT, offset || 0).to_json)
|
1404
|
+
|
1405
|
+
case result_type
|
1406
|
+
when 'ONLY_ID'
|
1407
|
+
docs.IDs = docs.IDs.map { |id| id }
|
1408
|
+
|
1409
|
+
when 'FULL_CONTENT', 'NO_CONTENT'
|
1410
|
+
docs.documents = docs.documents.map do |d|
|
1411
|
+
doc = Document.new
|
1412
|
+
doc.from_json(d.to_json)
|
1413
|
+
end
|
1414
|
+
end
|
1415
|
+
|
1416
|
+
docs
|
1417
|
+
end
|
1418
|
+
|
1419
|
+
def search_users(user_schema_id, result_type, filter_type, sort, filter, limit=nil, offset=nil)
|
1420
|
+
check_string(user_schema_id)
|
1421
|
+
check_string(result_type)
|
1422
|
+
check_string(filter_type)
|
1423
|
+
check_json(sort)
|
1424
|
+
check_json(filter)
|
1425
|
+
data = {"result_type": result_type, "filter_type": filter_type, "filter": filter, "sort": sort}.to_json
|
1426
|
+
users = GetUsersResponse.new
|
1427
|
+
if limit==nil && offset==nil
|
1428
|
+
users.from_json(post_resource("/search/users/#{user_schema_id}", data, ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
|
1429
|
+
else
|
1430
|
+
users.from_json(post_resource("/search/users/#{user_schema_id}", data, limit, offset).to_json)
|
1431
|
+
end
|
1432
|
+
us = users.users
|
1433
|
+
users.users = []
|
1434
|
+
us.each do |u|
|
1435
|
+
user = User.new
|
1436
|
+
user.from_json(u.to_json)
|
1437
|
+
users.users.push(user)
|
1438
|
+
end
|
1439
|
+
users
|
1440
|
+
end
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
#------------------------------BLOBS-----------------------------------#
|
1444
|
+
|
1445
|
+
class InitBlobResponse < CheckValues
|
1446
|
+
include ActiveModel::Serializers::JSON
|
1447
|
+
|
1448
|
+
attr_accessor :upload_id, :expire_date, :offset
|
1449
|
+
|
1450
|
+
def attributes=(hash)
|
1451
|
+
hash.each do |key, value|
|
1452
|
+
send("#{key}=", value)
|
1453
|
+
end
|
1454
|
+
end
|
1455
|
+
|
1456
|
+
def attributes
|
1457
|
+
instance_values
|
1458
|
+
end
|
1459
|
+
end
|
1460
|
+
|
1461
|
+
class Blob < CheckValues
|
1462
|
+
include ActiveModel::Serializers::JSON
|
1463
|
+
|
1464
|
+
attr_accessor :bytes, :blob_id, :sha1, :document_id, :md5
|
1465
|
+
|
1466
|
+
def attributes=(hash)
|
1467
|
+
hash.each do |key, value|
|
1468
|
+
send("#{key}=", value)
|
1469
|
+
end
|
1470
|
+
end
|
1471
|
+
|
1472
|
+
def attributes
|
1473
|
+
instance_values
|
1474
|
+
end
|
1475
|
+
end
|
1476
|
+
|
1477
|
+
class GetBlobResponse < CheckValues
|
1478
|
+
include ActiveModel::Serializers::JSON
|
1479
|
+
|
1480
|
+
attr_accessor :blob_id, :path, :filename, :size, :sha1, :md5
|
1481
|
+
|
1482
|
+
def attributes=(hash)
|
1483
|
+
hash.each do |key, value|
|
1484
|
+
send("#{key}=", value)
|
1485
|
+
end
|
1486
|
+
end
|
1487
|
+
|
1488
|
+
def attributes
|
1489
|
+
instance_values
|
1490
|
+
end
|
1491
|
+
end
|
1492
|
+
|
1493
|
+
class Blobs < ChinoBaseAPI
|
1494
|
+
|
1495
|
+
def upload_blob(path, filename, document_id, field, chunk_size = 1024*32)
|
1496
|
+
check_string(path)
|
1497
|
+
check_string(document_id)
|
1498
|
+
check_string(field)
|
1499
|
+
check_string(filename)
|
1500
|
+
blob = InitBlobResponse.new
|
1501
|
+
blob = init_upload(filename, document_id, field)
|
1502
|
+
bytes = []
|
1503
|
+
offset = 0
|
1504
|
+
#FIXME: this is relative to the LIBRARY directory, not running app
|
1505
|
+
# file_path = File.join File.expand_path("../..", File.dirname(__FILE__)), path, filename
|
1506
|
+
file_path = File.join path, filename
|
1507
|
+
File.open(file_path, 'rb') { |file|
|
1508
|
+
while (buffer = file.read(chunk_size)) do
|
1509
|
+
upload_chunk(blob.upload_id, buffer, offset)
|
1510
|
+
offset = offset+buffer.length
|
1511
|
+
end
|
1512
|
+
commit_upload(blob.upload_id)
|
1513
|
+
}
|
1514
|
+
end
|
1515
|
+
|
1516
|
+
def init_upload(filename, document_id, field)
|
1517
|
+
check_string(filename)
|
1518
|
+
check_string(document_id)
|
1519
|
+
check_string(field)
|
1520
|
+
data = { file_name: filename, document_id: document_id, field: field }.to_json
|
1521
|
+
blob = InitBlobResponse.new
|
1522
|
+
blob.from_json(ActiveSupport::JSON.decode(post_resource("/blobs", data).to_json)['blob'].to_json)
|
1523
|
+
blob
|
1524
|
+
end
|
1525
|
+
|
1526
|
+
def upload_chunk(upload_id, bytes, offset)
|
1527
|
+
uri = return_uri("/blobs/#{upload_id}")
|
1528
|
+
req = Net::HTTP::Put.new(uri)
|
1529
|
+
req.body = bytes
|
1530
|
+
req.add_field("length", bytes.length)
|
1531
|
+
req.add_field("offset", offset)
|
1532
|
+
req.add_field("Content-Type", "application/octet-stream")
|
1533
|
+
if @customer_id == "Bearer "
|
1534
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
1535
|
+
else
|
1536
|
+
req.basic_auth @customer_id, @customer_key
|
1537
|
+
end
|
1538
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
1539
|
+
http.request(req)
|
1540
|
+
}
|
1541
|
+
blob = InitBlobResponse.new
|
1542
|
+
blob.from_json(parse_response(res)['data'].to_json, true)
|
1543
|
+
blob
|
1544
|
+
end
|
1545
|
+
|
1546
|
+
def commit_upload(upload_id)
|
1547
|
+
check_string(upload_id)
|
1548
|
+
data = { upload_id: upload_id }.to_json
|
1549
|
+
blob = Blob.new
|
1550
|
+
blob.from_json(post_resource("/blobs/commit", data).to_json, true)
|
1551
|
+
blob
|
1552
|
+
end
|
1553
|
+
|
1554
|
+
def get(blob_id, destination)
|
1555
|
+
check_string(blob_id)
|
1556
|
+
check_string(destination)
|
1557
|
+
uri = return_uri("/blobs/#{blob_id}")
|
1558
|
+
req = Net::HTTP::Get.new(uri.path)
|
1559
|
+
if @customer_id == "Bearer "
|
1560
|
+
req.add_field("Authorization", @customer_id+@customer_key)
|
1561
|
+
else
|
1562
|
+
req.basic_auth @customer_id, @customer_key
|
1563
|
+
end
|
1564
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
|
1565
|
+
http.request(req)
|
1566
|
+
}
|
1567
|
+
blob = GetBlobResponse.new
|
1568
|
+
blob.blob_id = blob_id
|
1569
|
+
filename = res.header['Content-Disposition'].partition('=').last
|
1570
|
+
blob.filename = filename
|
1571
|
+
blob.path = destination
|
1572
|
+
# FIXME: this is relative to the LIBRARY directory, not running app
|
1573
|
+
# file_path = File.join File.expand_path("../..", File.dirname(__FILE__)), destination
|
1574
|
+
file_path = File.join Dir.pwd, destination
|
1575
|
+
FileUtils.mkdir_p(file_path) unless File.exist?(file_path)
|
1576
|
+
File.open(File.join(file_path+filename), 'wb') { |file|
|
1577
|
+
file << res.body
|
1578
|
+
blob.md5 = (Digest::MD5.file file).hexdigest
|
1579
|
+
blob.sha1 = (Digest::SHA1.file file).hexdigest
|
1580
|
+
blob.size = file.size
|
1581
|
+
}
|
1582
|
+
blob
|
1583
|
+
end
|
1584
|
+
|
1585
|
+
def delete_blob(blob_id, force)
|
1586
|
+
check_string(blob_id)
|
1587
|
+
check_boolean(force)
|
1588
|
+
delete_resource("/blobs/#{blob_id}", force)
|
1589
|
+
end
|
1590
|
+
end
|
1591
|
+
|
1592
|
+
end
|