Class: Updox::Models::Model

Inherits:
Hashie::Trash
  • Object
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'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#updox_statusObject



31
32
33
# File 'lib/updox/models/model.rb', line 31

def updox_status
  @updox_status ||= {}
end

Class Method Details

.from_response(response, klazz = self) ⇒ Object



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
88
89
90
91
92
93
94
95
96
# File 'lib/updox/models/model.rb', line 47

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



43
44
45
# File 'lib/updox/models/model.rb', line 43

def self.request(**kwargs)
  from_response(UpdoxClient.connection.request(kwargs))
end

Instance Method Details

#as_json(options = nil) ⇒ Object



39
40
41
# File 'lib/updox/models/model.rb', line 39

def as_json(options = nil)
  self.to_h
end

#error_messageObject



35
36
37
# File 'lib/updox/models/model.rb', line 35

def error_message
  "#{response_code}: #{response_message}"
end

#response_codeObject



23
24
25
# File 'lib/updox/models/model.rb', line 23

def response_code
  @updox_status['responseCode']
end

#response_messageObject



27
28
29
# File 'lib/updox/models/model.rb', line 27

def response_message
  @updox_status['responseMessage']
end

#successful?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/updox/models/model.rb', line 19

def successful?
  @updox_status['successful']
end