class Datmachine::Error

Custom error class for rescuing from all API response-related Balanced errors

Attributes

response[R]

Public Class Methods

new(response=nil) click to toggle source

@param [Hash] response the decoded json response body

Calls superclass method
# File lib/datmachine/error.rb, line 8
def initialize(response=nil)
  @response = response
  unless response.nil?
    super error_message
  end
end

Public Instance Methods

body() click to toggle source

@return [Hash]

# File lib/datmachine/error.rb, line 16
def body
  @body ||= begin
    return {} unless response[:body]
    Utils.indifferent_read_access(response[:body])
  end
end
error_message() click to toggle source
# File lib/datmachine/error.rb, line 23
def error_message
  set_attrs
  errors = body.fetch('errors', nil)
  unless errors.nil?
    error = body[:errors][0]
    extra = error[:additional] ? " -- #{error[:additional]}" : ""
    "#{self.class.name}(#{response[:status]})::#{error[:status]}:: "\
    "#{response[:method].to_s.upcase} #{response[:url].to_s}: "\
    "#{error[:category_code]}: #{error[:description]} #{extra}"
  end
end

Private Instance Methods

set_attrs() click to toggle source
# File lib/datmachine/error.rb, line 36
def set_attrs
  errors = body.fetch('errors', nil)
  unless errors.nil?
    error = errors[0]
    error.keys.each do |name|
      self.class.instance_eval {
        define_method(name) { error[name] } # Get.
        define_method("#{name}?") { !!error[name] } # Present.
      }
    end
  end
end