class Mambu::Response

Attributes

body[R]
error_code[R]
error_status[R]
faraday_response[R]
http_status[R]

Public Class Methods

new(faraday_response) click to toggle source
# File lib/mambu/response.rb, line 5
def initialize(faraday_response)
  @faraday_response = faraday_response
  @http_status = faraday_response.status
  @body = parse_body(faraday_response.body)
  return self if success?
  @error_code = @body[:return_code]
  @error_status = @body[:return_status]
end

Public Instance Methods

error() click to toggle source
# File lib/mambu/response.rb, line 26
def error
  return nil if success?
  Mambu::Error.new(error_message, error_code, error_status)
end
error?() click to toggle source
# File lib/mambu/response.rb, line 18
def error?
  !success?
end
error_message() click to toggle source
# File lib/mambu/response.rb, line 22
def error_message
  error_status.humanize
end
success?() click to toggle source
# File lib/mambu/response.rb, line 14
def success?
  @faraday_response.success?
end

Private Instance Methods

convert_hash_keys(value) click to toggle source
# File lib/mambu/response.rb, line 41
def convert_hash_keys(value)
  case value
  when Array
    value.map(&method(:convert_hash_keys))
  when Hash
    Hash[value.map { |k, v| [k.to_s.underscore.to_sym, convert_hash_keys(v)] }]
  else
    value
  end
end
parse_body(json_body) click to toggle source
# File lib/mambu/response.rb, line 33
def parse_body(json_body)
  begin
    convert_hash_keys(JSON.parse(json_body))
  rescue JSON::ParserError
    { return_code: -1, return_status: 'INTERNAL_SERVER_ERROR' }
  end
end