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