Class: Updox::Models::Model

Inherits:
Hashie::Trash
  • Object
show all
Includes:
Hashie::Extensions::IgnoreUndeclared, Hashie::Extensions::IndifferentAccess
Defined in:
lib/updox/models/model.rb

Direct Known Subclasses

Application, Appointment, Calendar, Location, Patient, Practice, User

Constant Summary collapse

LIST_TYPE =
'undefined'
LIST_NAME =
'models'

Class Method Summary collapse

Class Method Details

.from_response(response, klazz = self) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/updox/models/model.rb', line 15

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 }
    else
      model.items = [data]
      model.item  = data
    end
  else
    raise UpdoxException.from_response(response)
  end

  return model
end