dm_api 0.0.4
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 +7 -0
- data/README.md +76 -0
- data/dm_api.gemspec +18 -0
- data/lib/dm_api/api.rb +43 -0
- data/lib/dm_api/api_response.rb +67 -0
- data/lib/dm_api/configuration.rb +20 -0
- data/lib/dm_api/errors/failed_response.rb +16 -0
- data/lib/dm_api/errors/required_parameter_missing.rb +8 -0
- data/lib/dm_api/resource/base.rb +199 -0
- data/lib/dm_api/resource/binder.rb +18 -0
- data/lib/dm_api/resource/cabinet.rb +12 -0
- data/lib/dm_api/resource/document.rb +20 -0
- data/lib/dm_api/resource/file.rb +72 -0
- data/lib/dm_api/resource/information.rb +16 -0
- data/lib/dm_api/resource/information_definition.rb +11 -0
- data/lib/dm_api/resource/shelf.rb +14 -0
- data/lib/dm_api/resource/tray.rb +17 -0
- data/lib/dm_api/resource.rb +39 -0
- data/lib/dm_api/resource_set.rb +41 -0
- data/lib/dm_api.rb +34 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4d367b16598aeaa0c05f8064b881bfd68337fc33ce5858ab23c2421b412ef936
|
4
|
+
data.tar.gz: 2061c3feb8f6e63f2d8abf3f88723c14a75c5b3c4ba6a66ebeda745644ab97cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 126ae454dbf89c1f09651862a87ff97f51838a4bd09cd0732e98c2c0e6e6f929b9dfa92c3e448f1ef567a42c99c129a2a391689c83a28e6a869e12ccd00c9d27
|
7
|
+
data.tar.gz: cd41be68e47ae01563339f25c77fb52adb3588b2ed78a8542cd7c7e5d93c164a266b41a8b88b96f48c8beca10950e519fe8bc2ecedac1a535151db4dac8205bf
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# dm_api client
|
2
|
+
|
3
|
+
## Adding to Gemfile
|
4
|
+
|
5
|
+
### Local - development
|
6
|
+
|
7
|
+
Add `gem "dm_api", path: "/home/user/clones/dm_api"` to your Gemfile and run `bundle install`
|
8
|
+
|
9
|
+
### Git
|
10
|
+
|
11
|
+
```bash
|
12
|
+
bundle add dm_api --git git@gitlab.vmin.cz:vm/dm_api.git
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### Configuration
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
DmApi::Configuration.instance.api_key = File.read(Rails.root.join('config', 'dm_api.key')).chomp
|
21
|
+
DmApi::Configuration.instance.proxy = 'http://localhost:8080'
|
22
|
+
DmApi::Configuration.instance.ssl_verify = false
|
23
|
+
```
|
24
|
+
|
25
|
+
### Create (empty) document
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
container_id = '7b1d9be9-e329-45b2-860b-c54462f14312'
|
29
|
+
dm_doc = DmApi::Resource::Document.create(
|
30
|
+
body: {
|
31
|
+
title: 'document_title',
|
32
|
+
container: { data: {
|
33
|
+
id: container_id,
|
34
|
+
type: 'binder'
|
35
|
+
} }
|
36
|
+
}
|
37
|
+
)
|
38
|
+
```
|
39
|
+
|
40
|
+
### Upload file data
|
41
|
+
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
filename = Rails.root.join('example.pdf').to_s
|
45
|
+
file_uuid, upload_response = DmApi::Resource::File.upload(filename)
|
46
|
+
|
47
|
+
file_uuid = '54b49b1f-084e-4e2d-948c-7222f450821a'
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
### Attach file to document
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
file_data = [ { data: { id: file_uuid, type: 'file' } } ]
|
55
|
+
response = DmApi::Resource::Document.add_file(dm_doc.id, body: file_data)
|
56
|
+
```
|
57
|
+
|
58
|
+
|
59
|
+
### Display list of info fields
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
DmApi::Resource::Document.find(dm_doc.id).information.map { |info| info.label }
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
67
|
+
DmApi::Resource::Document.find(dm_doc.id).information.each do |info|
|
68
|
+
case info.label
|
69
|
+
when 'DUZP'
|
70
|
+
date = Date.today
|
71
|
+
DmApi::Resource::Information.update(info.id, body: { value: { data: date.to_time } })
|
72
|
+
when 'DPH'
|
73
|
+
value = { number: 123.45, unit: 'CZK' }
|
74
|
+
DmApi::Resource::Information.update(info.id, body: { value: { data: value } })
|
75
|
+
end
|
76
|
+
end
|
data/dm_api.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'dm_api'
|
5
|
+
s.version = `git describe --tags | sed -e s/^v//`
|
6
|
+
s.summary = 'DocumentManager API client'
|
7
|
+
s.description = 'DocumentManager API v3 bare bones client'
|
8
|
+
s.authors = ['Lukáš Bauer', 'Matouš Vokál', 'Josef Liška']
|
9
|
+
s.email = 'billing@virtualmaster.com'
|
10
|
+
s.homepage = ''
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.add_dependency('typhoeus', ['>= 1.3.0'])
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
17
|
+
s.require_path = 'lib'
|
18
|
+
end
|
data/lib/dm_api/api.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
class Api
|
5
|
+
attr_reader :config
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@config = Configuration.instance
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(endpoint, method, options = {})
|
12
|
+
request_url = @config.base_url + endpoint
|
13
|
+
if options[:params]
|
14
|
+
request_url +=
|
15
|
+
"?#{options[:params].map { |key, val| "#{key}=#{CGI.escape(val)}" }.join('&')}"
|
16
|
+
end
|
17
|
+
json_body = call_url(request_url, method, options)
|
18
|
+
ApiResponse.new(json_body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def call_url(request_url, method, options = {})
|
22
|
+
request_options = {
|
23
|
+
headers: {
|
24
|
+
'Authorization': "Bearer #{@config.api_key}",
|
25
|
+
'Accept': 'application/json',
|
26
|
+
'Content-Type': 'application/json; charset=utf-8'
|
27
|
+
},
|
28
|
+
method: method,
|
29
|
+
ssl_verifypeer: @config.ssl_verify
|
30
|
+
}
|
31
|
+
|
32
|
+
request_options[:body] = options[:body]&.to_json unless options[:body].nil?
|
33
|
+
request_options[:proxy] = @config.proxy unless @config.proxy.nil?
|
34
|
+
|
35
|
+
response = Typhoeus::Request.new(request_url, request_options).run
|
36
|
+
unless response.success?
|
37
|
+
raise DmApi::Errors::FailedResponse.new('Could not get data from DM API', response)
|
38
|
+
end
|
39
|
+
|
40
|
+
JSON.parse(response.body)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
class ApiResponse
|
5
|
+
delegate_missing_to :data
|
6
|
+
delegate :to_json, to: :data
|
7
|
+
attr_reader :data, :links, :metadata, :raw_body
|
8
|
+
|
9
|
+
def initialize(response)
|
10
|
+
@data = parse_response_data(response)
|
11
|
+
@links = response['links']
|
12
|
+
@metadata = response['meta']
|
13
|
+
@raw_body = response
|
14
|
+
end
|
15
|
+
|
16
|
+
def next
|
17
|
+
return if @links['next'].nil?
|
18
|
+
|
19
|
+
next_url = @links['next']
|
20
|
+
json_body = Api.new.call_url(next_url, :get)
|
21
|
+
ApiResponse.new(json_body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def prev
|
25
|
+
return if @links['prev'].nil?
|
26
|
+
|
27
|
+
prev_url = @links['prev']
|
28
|
+
json_body = Api.new.call_url(prev_url, :get)
|
29
|
+
ApiResponse.new(json_body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def first
|
33
|
+
return if @links['first'].nil?
|
34
|
+
|
35
|
+
first_url = @links['first']
|
36
|
+
json_body = Api.new.call_url(first_url, :get)
|
37
|
+
ApiResponse.new(json_body)
|
38
|
+
end
|
39
|
+
|
40
|
+
def last
|
41
|
+
return if @links['last'].nil?
|
42
|
+
|
43
|
+
last_url = @links['last']
|
44
|
+
json_body = Api.new.call_url(last_url, :get)
|
45
|
+
ApiResponse.new(json_body)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def parse_response_data(response)
|
51
|
+
response_data = response['data']
|
52
|
+
return if response_data.nil?
|
53
|
+
|
54
|
+
if response_data.is_a?(Array)
|
55
|
+
response_data.map do |response_data_item|
|
56
|
+
parse_response_item(response_data_item)
|
57
|
+
end
|
58
|
+
else
|
59
|
+
parse_response_item(response)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_response_item(response_item = {})
|
64
|
+
Resource.init_from_hash(response_item)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
class Configuration
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_accessor :base_url, :api_key, :proxy, :ssl_verify
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@base_url = 'https://documentmanager.cz/api/v3'
|
11
|
+
@api_key = nil
|
12
|
+
@proxy = nil
|
13
|
+
@ssl_verify = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield(self) if block_given?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Errors
|
5
|
+
class FailedResponse < StandardError
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def initialize(msg = nil, response = {})
|
11
|
+
@response = response
|
12
|
+
super(msg)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Base
|
6
|
+
UNLOADED_ATTR = Object.new
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def belongs_to_relations
|
10
|
+
@belongs_to_relations ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_many_relations
|
14
|
+
@has_many_relations ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes
|
18
|
+
@attributes ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def api_method(name, method, endpoint)
|
22
|
+
endpoint_params = endpoint.scan(/:\w+/).map { |param| param[1..] }
|
23
|
+
method_args = endpoint_params.join(', ')
|
24
|
+
method_args += ', ' if method_args.present?
|
25
|
+
method_args += 'opts = HashWithIndifferentAccess.new'
|
26
|
+
formatted_endpoint = endpoint.gsub(/:(\w+)/, '#{\1}')
|
27
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
28
|
+
def self.#{name}(#{method_args})
|
29
|
+
#{endpoint_params.map { |param| "raise DmApi::Errors::RequiredParameterMissing, 'parameter #{param} is required' if #{param}.nil?" }.join("\n")}
|
30
|
+
|
31
|
+
endpoint = "#{formatted_endpoint}"
|
32
|
+
opts[:body] = { data: opts[:body] } if opts[:body].present?
|
33
|
+
Api.new.call(endpoint, :#{method}, opts)
|
34
|
+
end
|
35
|
+
RUBY
|
36
|
+
end
|
37
|
+
|
38
|
+
def attribute(name, options = {})
|
39
|
+
full_options = default_attr_options.merge(options)
|
40
|
+
attributes[name] = full_options
|
41
|
+
if full_options[:lazy_loaded]
|
42
|
+
define_lazy_loaded_attr(name)
|
43
|
+
else
|
44
|
+
attr_reader name
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_writer(name) unless full_options[:read_only]
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_lazy_loaded_attr(attr)
|
51
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
52
|
+
def #{attr}
|
53
|
+
return @#{attr} unless @#{attr} == UNLOADED_ATTR && @lazy_loaded
|
54
|
+
return if @links.nil? || @links['self'].nil?
|
55
|
+
|
56
|
+
request_url = @links['self']['href']
|
57
|
+
obj_hash = Api.new.call_url(request_url, :get)
|
58
|
+
data = obj_hash['data']
|
59
|
+
new_data = data.except('type')
|
60
|
+
new_data['links'] = obj_hash['links']
|
61
|
+
load_from_hash(new_data, lazy_loaded: false)
|
62
|
+
@#{attr}
|
63
|
+
end
|
64
|
+
RUBY
|
65
|
+
end
|
66
|
+
|
67
|
+
def has_many(attribute, options = {})
|
68
|
+
full_options = default_relation_options.merge(options)
|
69
|
+
if full_options[:lazy_loaded]
|
70
|
+
define_lazy_loaded_attr(attribute)
|
71
|
+
else
|
72
|
+
attr_reader attribute
|
73
|
+
end
|
74
|
+
|
75
|
+
has_many_relations[attribute] = options
|
76
|
+
end
|
77
|
+
|
78
|
+
def belongs_to(attribute, options = {})
|
79
|
+
full_options = default_relation_options.merge(options)
|
80
|
+
if full_options[:lazy_loaded]
|
81
|
+
define_lazy_loaded_attr(attribute)
|
82
|
+
else
|
83
|
+
attr_reader attribute
|
84
|
+
end
|
85
|
+
|
86
|
+
belongs_to_relations[attribute] = full_options
|
87
|
+
end
|
88
|
+
|
89
|
+
def default_relation_options
|
90
|
+
{
|
91
|
+
json_column: nil,
|
92
|
+
lazy_loaded: true
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
def default_attr_options
|
97
|
+
{
|
98
|
+
json_column: nil,
|
99
|
+
lazy_loaded: true,
|
100
|
+
read_only: false,
|
101
|
+
type: :default
|
102
|
+
}
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def to_json(*_args)
|
107
|
+
self.class.attributes.map do |attr_name, _opts|
|
108
|
+
attr_val = instance_variable_get("@#{attr_name}")
|
109
|
+
[attr_name, attr_val.to_json] if attr_val.present?
|
110
|
+
end.compact.to_h.merge(self.class.belongs_to_relations.map do |rel_name, _opts|
|
111
|
+
[rel_name, instance_variable_get("@#{rel_name}").to_json]
|
112
|
+
end.to_h.merge(self.class.has_many_relations.map do |rel_name, _opts|
|
113
|
+
[rel_name, instance_variable_get("@#{rel_name}").to_json]
|
114
|
+
end.to_h)).to_json
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def initialize(data = {})
|
120
|
+
load_from_hash(data)
|
121
|
+
end
|
122
|
+
|
123
|
+
def load_from_hash(data, lazy_loaded: true)
|
124
|
+
@lazy_loaded = lazy_loaded
|
125
|
+
@links = data['links']
|
126
|
+
load_attributes(data)
|
127
|
+
load_relations(data)
|
128
|
+
end
|
129
|
+
|
130
|
+
def load_attributes(data)
|
131
|
+
self.class.attributes.each do |attr_name, options|
|
132
|
+
assign_attribute(data, attr_name, options)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def assign_attribute(data, attr_name, options)
|
137
|
+
json_name = (options[:json_column] || attr_name).to_s
|
138
|
+
|
139
|
+
unless data.key?(json_name)
|
140
|
+
instance_variable_set("@#{attr_name}", UNLOADED_ATTR)
|
141
|
+
return
|
142
|
+
end
|
143
|
+
|
144
|
+
json_value = data[json_name]
|
145
|
+
|
146
|
+
variable_value = case options[:type]
|
147
|
+
when :string
|
148
|
+
json_value&.to_s
|
149
|
+
when :integer
|
150
|
+
json_value&.to_i
|
151
|
+
when :float
|
152
|
+
json_value&.to_f
|
153
|
+
when :object
|
154
|
+
parse_inline_object(json_value)
|
155
|
+
else
|
156
|
+
json_value
|
157
|
+
end
|
158
|
+
instance_variable_set("@#{attr_name}", variable_value)
|
159
|
+
end
|
160
|
+
|
161
|
+
def parse_inline_object(obj_hash)
|
162
|
+
return obj_hash unless obj_hash.keys.include?('data')
|
163
|
+
|
164
|
+
data = obj_hash['data']
|
165
|
+
return if data.nil?
|
166
|
+
|
167
|
+
if data.respond_to?(:each_pair)
|
168
|
+
data.transform_keys!(&:underscore)
|
169
|
+
klass = Base
|
170
|
+
data.each_key do |key|
|
171
|
+
klass.attribute key, lazy_loaded: false
|
172
|
+
end
|
173
|
+
klass.new(data)
|
174
|
+
elsif data.respond_to?(:each)
|
175
|
+
data.map do |data_item|
|
176
|
+
parse_inline_object({ 'data' => data_item })
|
177
|
+
end
|
178
|
+
else
|
179
|
+
data
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def load_relations(data)
|
184
|
+
self.class.belongs_to_relations.each do |relation, options|
|
185
|
+
json_name = (options[:json_column] || relation).to_s
|
186
|
+
instance_variable_set(
|
187
|
+
"@#{relation}", data[json_name].nil? ? nil : Resource.init_from_hash(data[json_name])
|
188
|
+
)
|
189
|
+
end
|
190
|
+
self.class.has_many_relations.each do |relation, options|
|
191
|
+
json_name = (options[:json_column] || relation).to_s
|
192
|
+
instance_variable_set(
|
193
|
+
"@#{relation}", data[json_name].nil? ? nil : ResourceSet.new(data[json_name])
|
194
|
+
)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Binder < Base
|
6
|
+
api_method :all, :get, '/binders'
|
7
|
+
api_method :find, :get, '/binders/:id'
|
8
|
+
api_method :list_documents, :get, '/binders/:id/documents'
|
9
|
+
|
10
|
+
attribute :id, lazy_loaded: false
|
11
|
+
attribute :count
|
12
|
+
attribute :color
|
13
|
+
attribute :label
|
14
|
+
attribute :secondary_label, json_column: 'secondaryLabel'
|
15
|
+
belongs_to :shelf
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Document < Base
|
6
|
+
api_method :all, :get, '/documents'
|
7
|
+
api_method :create, :post, '/documents'
|
8
|
+
api_method :find, :get, '/documents/:id'
|
9
|
+
api_method :update, :patch, '/documents/:id'
|
10
|
+
api_method :add_file, :post, '/documents/:id/files'
|
11
|
+
|
12
|
+
attribute :id, lazy_loaded: false
|
13
|
+
attribute :title
|
14
|
+
attribute :note
|
15
|
+
attribute :created_at, json_column: 'createdAt'
|
16
|
+
has_many :information
|
17
|
+
belongs_to :container
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class File < Base
|
6
|
+
api_method :all, :get, '/files'
|
7
|
+
api_method :find, :get, '/files/:id'
|
8
|
+
api_method :request_uuid, :post, '/files'
|
9
|
+
api_method :request_upload, :post, '/files/:id/content'
|
10
|
+
|
11
|
+
attribute :id, lazy_loaded: false
|
12
|
+
attribute :created_at, json_column: 'createdAt'
|
13
|
+
attribute :filename
|
14
|
+
attribute :size, type: :integer
|
15
|
+
attribute :media_type, json_column: 'mediaType'
|
16
|
+
attribute :checksums
|
17
|
+
attribute :status
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def upload(file_name, uuid: nil)
|
21
|
+
if uuid.blank?
|
22
|
+
resp = request_uuid
|
23
|
+
uuid = resp.id
|
24
|
+
end
|
25
|
+
resp = request_upload(uuid)
|
26
|
+
aws_meta = resp.metadata['location']
|
27
|
+
req = Typhoeus::Request.new(aws_meta['url'],
|
28
|
+
{
|
29
|
+
method: aws_meta['method'],
|
30
|
+
headers: aws_headers(aws_meta['headers']),
|
31
|
+
body: aws_body(aws_meta['formFields'], file_name)
|
32
|
+
})
|
33
|
+
response = req.run
|
34
|
+
|
35
|
+
retries = 5
|
36
|
+
retries.times do
|
37
|
+
sleep 1
|
38
|
+
doc = DmApi::Resource::File.find(uuid)
|
39
|
+
puts doc.status
|
40
|
+
|
41
|
+
break if %w[analyzed checked].include?(doc.status)
|
42
|
+
rescue DmApi::Errors::FailedResponse => e
|
43
|
+
raise e unless e.response.code.to_s == '404'
|
44
|
+
end
|
45
|
+
|
46
|
+
[uuid, response]
|
47
|
+
end
|
48
|
+
|
49
|
+
def aws_headers(headers)
|
50
|
+
aws_headers = {}
|
51
|
+
headers.each do |key, value|
|
52
|
+
aws_headers[key] = value if value.present?
|
53
|
+
end
|
54
|
+
aws_headers['User-Agent'] = 'curl/7.68.0'
|
55
|
+
aws_headers
|
56
|
+
end
|
57
|
+
|
58
|
+
def aws_body(body, file_name)
|
59
|
+
aws_body = {}
|
60
|
+
body.each do |key, value|
|
61
|
+
value = ::File.open(file_name, 'r') if key == 'file'
|
62
|
+
next if value.blank?
|
63
|
+
|
64
|
+
aws_body[key] = value
|
65
|
+
end
|
66
|
+
aws_body[:file] = ::File.open(file_name, 'r') if aws_body['file'].nil?
|
67
|
+
aws_body
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Information < Base
|
6
|
+
api_method :all, :get, '/documents/:document_id/information'
|
7
|
+
api_method :find, :get, '/information/:id'
|
8
|
+
api_method :update, :patch, '/information/:id'
|
9
|
+
|
10
|
+
attribute :id, lazy_loaded: false
|
11
|
+
attribute :label
|
12
|
+
attribute :value, type: :object
|
13
|
+
attribute :tags
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Shelf < Base
|
6
|
+
api_method :find, :get, '/shelves/:id'
|
7
|
+
|
8
|
+
attribute :id, lazy_loaded: false
|
9
|
+
attribute :label
|
10
|
+
has_many :information_definitions, json_column: 'informationDefinitions'
|
11
|
+
belongs_to :cabinet
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
class Tray < Base
|
6
|
+
api_method :all, :get, '/trays'
|
7
|
+
api_method :find, :get, '/trays/:id'
|
8
|
+
|
9
|
+
attribute :id, lazy_loaded: false
|
10
|
+
attribute :label
|
11
|
+
attribute :color
|
12
|
+
attribute :count
|
13
|
+
attribute :secondary_label, json_column: 'secondaryLabel'
|
14
|
+
belongs_to :shelf
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
module Resource
|
5
|
+
RESOURCE_CLASS_MAP = {
|
6
|
+
'document' => 'DmApi::Resource::Document',
|
7
|
+
'binder' => 'DmApi::Resource::Binder',
|
8
|
+
'information' => 'DmApi::Resource::Information',
|
9
|
+
'shelf' => 'DmApi::Resource::Shelf',
|
10
|
+
'informationDefinition' => 'DmApi::Resource::InformationDefinition',
|
11
|
+
'cabinet' => 'DmApi::Resource::Cabinet',
|
12
|
+
'file' => 'DmApi::Resource::File',
|
13
|
+
'tray' => 'DmApi::Resource::Tray'
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def init_from_hash(obj_hash = {})
|
18
|
+
data = obj_hash['data']
|
19
|
+
|
20
|
+
if data.is_a? Array
|
21
|
+
ResourceSet.new(obj_hash)
|
22
|
+
else
|
23
|
+
data['links'] = obj_hash['links']
|
24
|
+
return obj_hash if data['type'].nil?
|
25
|
+
|
26
|
+
type = RESOURCE_CLASS_MAP[data['type']]
|
27
|
+
return obj_hash if type.nil?
|
28
|
+
|
29
|
+
init_resource(type, data)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def init_resource(type, data)
|
34
|
+
new_data = data.except('type')
|
35
|
+
type.constantize.new(new_data)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DmApi
|
4
|
+
class ResourceSet
|
5
|
+
delegate_missing_to :resources
|
6
|
+
delegate :to_json, to: :resources
|
7
|
+
|
8
|
+
attr_reader :links
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def initialize(data)
|
13
|
+
load_from_hash(data)
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_from_hash(data)
|
17
|
+
if data.is_a? Array
|
18
|
+
@resources = data&.map do |resource|
|
19
|
+
Resource.init_from_hash({ 'data' => resource })
|
20
|
+
end
|
21
|
+
@links = nil
|
22
|
+
else
|
23
|
+
@resources = data['data']&.map do |resource|
|
24
|
+
Resource.init_from_hash(resource)
|
25
|
+
end
|
26
|
+
@links = data['links']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def resources
|
31
|
+
return @resources unless @resources.nil?
|
32
|
+
|
33
|
+
return if @links.nil? || @links['self'].nil?
|
34
|
+
|
35
|
+
request_url = @links['self']['href']
|
36
|
+
obj_hash = Api.new.call_url(request_url, :get)
|
37
|
+
load_from_hash(obj_hash)
|
38
|
+
@resources
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/dm_api.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'dm_api/configuration'
|
4
|
+
require_relative 'dm_api/resource/base'
|
5
|
+
require_relative 'dm_api/resource/binder'
|
6
|
+
require_relative 'dm_api/resource/file'
|
7
|
+
require_relative 'dm_api/resource/shelf'
|
8
|
+
require_relative 'dm_api/resource/tray'
|
9
|
+
require_relative 'dm_api/resource/document'
|
10
|
+
require_relative 'dm_api/resource/cabinet'
|
11
|
+
require_relative 'dm_api/resource/information_definition'
|
12
|
+
require_relative 'dm_api/resource/information'
|
13
|
+
require_relative 'dm_api/errors/required_parameter_missing'
|
14
|
+
require_relative 'dm_api/errors/failed_response'
|
15
|
+
require_relative 'dm_api/resource'
|
16
|
+
require_relative 'dm_api/resource_set'
|
17
|
+
require_relative 'dm_api/api'
|
18
|
+
require_relative 'dm_api/api_response'
|
19
|
+
|
20
|
+
module DmApi
|
21
|
+
class << self
|
22
|
+
def configure
|
23
|
+
if block_given?
|
24
|
+
yield(Configuration.instance)
|
25
|
+
else
|
26
|
+
Configuration.instance
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def use_relative_model_naming?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukáš Bauer
|
8
|
+
- Matouš Vokál
|
9
|
+
- Josef Liška
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2025-08-13 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: typhoeus
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.3.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 1.3.0
|
29
|
+
description: DocumentManager API v3 bare bones client
|
30
|
+
email: billing@virtualmaster.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- dm_api.gemspec
|
37
|
+
- lib/dm_api.rb
|
38
|
+
- lib/dm_api/api.rb
|
39
|
+
- lib/dm_api/api_response.rb
|
40
|
+
- lib/dm_api/configuration.rb
|
41
|
+
- lib/dm_api/errors/failed_response.rb
|
42
|
+
- lib/dm_api/errors/required_parameter_missing.rb
|
43
|
+
- lib/dm_api/resource.rb
|
44
|
+
- lib/dm_api/resource/base.rb
|
45
|
+
- lib/dm_api/resource/binder.rb
|
46
|
+
- lib/dm_api/resource/cabinet.rb
|
47
|
+
- lib/dm_api/resource/document.rb
|
48
|
+
- lib/dm_api/resource/file.rb
|
49
|
+
- lib/dm_api/resource/information.rb
|
50
|
+
- lib/dm_api/resource/information_definition.rb
|
51
|
+
- lib/dm_api/resource/shelf.rb
|
52
|
+
- lib/dm_api/resource/tray.rb
|
53
|
+
- lib/dm_api/resource_set.rb
|
54
|
+
homepage: ''
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.5.22
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: DocumentManager API client
|
77
|
+
test_files: []
|