class Updox::Models::Model

Constants

ITEM_TYPE
LIST_NAME
LIST_TYPE

Attributes

updox_status[W]

Public Class Methods

from_response(response, klazz = self) click to toggle source
# 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) click to toggle source
# File lib/updox/models/model.rb, line 43
def self.request(**kwargs)
  from_response(UpdoxClient.connection.request(kwargs))
end

Public Instance Methods

as_json(options = nil) click to toggle source
# File lib/updox/models/model.rb, line 39
def as_json(options = nil)
  self.to_h
end
error_message() click to toggle source
# File lib/updox/models/model.rb, line 35
def error_message
  "#{response_code}: #{response_message}"
end
response_code() click to toggle source
# File lib/updox/models/model.rb, line 23
def response_code
  @updox_status['responseCode']
end
response_message() click to toggle source
# File lib/updox/models/model.rb, line 27
def response_message
  @updox_status['responseMessage']
end
successful?() click to toggle source
# File lib/updox/models/model.rb, line 19
def successful?
  @updox_status['successful']
end
updox_status() click to toggle source
# File lib/updox/models/model.rb, line 31
def updox_status
  @updox_status ||= {}
end