Class: Updox::Models::Model
- Inherits:
-
Hashie::Trash
- Object
- Hashie::Trash
- Updox::Models::Model
show all
- Includes:
- Hashie::Extensions::IgnoreUndeclared, Hashie::Extensions::IndifferentAccess
- Defined in:
- lib/updox/models/model.rb
Constant Summary
collapse
- LIST_TYPE =
'undefined'
- LIST_NAME =
'models'
- ITEM_TYPE =
'model'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.exists?(item_id, account_id:, cached_query: nil) ⇒ Boolean
36
37
38
39
40
41
42
43
44
|
# File 'lib/updox/models/model.rb', line 36
def self.exists?(item_id, account_id: , cached_query: nil)
raise UpdoxException.new('Not implemented on this model.') unless self.respond_to?(:find)
opts = { account_id: account_id }
opts[:cached_query] = cached_query unless cached_query.nil?
response = self.find(item_id, **opts)
false == response.nil? && (false == self.const_defined?(:FIND_ENDPOINT) || response.successful?)
end
|
.from_response(response, klazz = self) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/updox/models/model.rb', line 73
def self.from_response(response, klazz = self)
return response if false == Updox.configuration.parse_responses?
model = Model.new
model.define_singleton_method(:response) { response }
if (response.ok?)
data = response.parsed_response
if data.is_a?(Array)
model.items = data
elsif data&.include?(klazz.const_get(:LIST_TYPE))
model.items = data.dig(klazz.const_get(:LIST_TYPE)).map { |obj| klazz.new(obj) }
model.define_singleton_method(klazz.const_get(:LIST_NAME)) { self.items }
elsif data&.include?(klazz.const_get(:ITEM_TYPE))
model.item = klazz.new(data.dig(klazz.const_get(:ITEM_TYPE)))
model.define_singleton_method(klazz.const_get(:ITEM_TYPE)) { self.item }
else
k = data&.keys&.find {|k| k.to_s.downcase.end_with?('statuses') }
c = 'Updox::Models::' + k[0..-3].capitalize if k
if k.nil? || false == Module.const_defined?(c)
model.items = [data]
else
statuses = data.delete(k)
model.items = statuses.map {|status| Object.const_get(c).new(status) }
model.define_singleton_method(:statuses) { self.items }
end
model.item = data
end
if (data.is_a?(Hash))
model.updox_status = data&.select {|k,v| ['successful', 'responseMessage', 'responseCode'].include?(k)} || {}
if false == model.successful? && false == Updox.configuration.failure_action.nil?
if Updox.configuration.failure_action.respond_to?(:call)
Updox.configuration.failure_action.call(model)
elsif :raise == Updox.configuration.failure_action
raise UpdoxException.from_response(response, msg: 'request')
end
end
end
else
raise UpdoxException.from_response(response)
end
return model
end
|
.request(**kwargs) ⇒ Object
69
70
71
|
# File 'lib/updox/models/model.rb', line 69
def self.request(**kwargs)
from_response(UpdoxClient.connection.request(kwargs))
end
|
.sync(items, account_id:, batch_size: RECOMMENDED_BATCH_SIZE, endpoint: self.const_get(:SYNC_ENDPOINT)) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/updox/models/model.rb', line 46
def self.sync(items, account_id: , batch_size: RECOMMENDED_BATCH_SIZE, endpoint: self.const_get(:SYNC_ENDPOINT))
response = nil
list_type = self.const_get(:SYNC_LIST_TYPE)
if 0 >= batch_size
response = request(endpoint: endpoint, body: { list_type => items }, auth: {accountId: account_id}, required_auths: Updox::Models::Auth::AUTH_ACCT)
else
items.each_slice(batch_size) do |batch|
r = request(endpoint: endpoint, body: { list_type => batch }, auth: {accountId: account_id}, required_auths: Updox::Models::Auth::AUTH_ACCT)
return r unless r.successful?
if response
response.items += r.items
else
response = r
end
end
end
return response
end
|
Instance Method Details
#error_message ⇒ Object
32
33
34
|
# File 'lib/updox/models/model.rb', line 32
def error_message
"#{response_code}: #{response_message}"
end
|
#response_code ⇒ Object
24
25
26
|
# File 'lib/updox/models/model.rb', line 24
def response_code
self[:updox_status].dig('responseCode')
end
|
#response_message ⇒ Object
28
29
30
|
# File 'lib/updox/models/model.rb', line 28
def response_message
self[:updox_status].dig('responseMessage')
end
|
#successful? ⇒ Boolean
20
21
22
|
# File 'lib/updox/models/model.rb', line 20
def successful?
self[:updox_status].dig('successful')
end
|