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
Direct Known Subclasses
Application, Appointment, AppointmentStatus, Calendar, Location, Patient, PatientMessage, Practice, Reminder, Status, User
Constant Summary
collapse
- LIST_TYPE =
'undefined'
- LIST_NAME =
'models'
- ITEM_TYPE =
'model'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.from_response(response, klazz = self) ⇒ Object
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
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/updox/models/model.rb', line 38
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
status_key = data&.keys&.find {|k| k.to_s.downcase.end_with?('statuses') }
status_class = 'Updox::Models::' + status_key[0..-3].capitalize if status_key
if status_key.nil? || false == Module.const_defined?(status_class)
model.items = [data]
else
statuses = data.delete(status_key)
model.items = statuses.map {|status| Object.const_get(status_class).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
34
35
36
|
# File 'lib/updox/models/model.rb', line 34
def self.request(**kwargs)
from_response(UpdoxClient.connection.request(kwargs))
end
|
Instance Method Details
#error_message ⇒ Object
30
31
32
|
# File 'lib/updox/models/model.rb', line 30
def error_message
"#{response_code}: #{response_message}"
end
|
#response_code ⇒ Object
22
23
24
|
# File 'lib/updox/models/model.rb', line 22
def response_code
self[:updox_status].dig('responseCode')
end
|
#response_message ⇒ Object
26
27
28
|
# File 'lib/updox/models/model.rb', line 26
def response_message
self[:updox_status].dig('responseMessage')
end
|
#successful? ⇒ Boolean
18
19
20
|
# File 'lib/updox/models/model.rb', line 18
def successful?
self[:updox_status].dig('successful')
end
|