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