class Klarna::Response

Attributes

body[R]
code[R]
headers[R]
http_response[R]

Public Class Methods

new(http_response) click to toggle source
# File lib/klarna/response.rb, line 5
def initialize(http_response)
  @http_response = http_response
  @code = http_response.code.to_i
  @body = parse_body
  @headers = http_response.to_hash
end

Public Instance Methods

[](index) click to toggle source
# File lib/klarna/response.rb, line 20
def [](index)
  http_response.send("[]", index)
end
error?() click to toggle source
# File lib/klarna/response.rb, line 16
def error?
  @code >= 300
end
error_code() click to toggle source
# File lib/klarna/response.rb, line 34
def error_code
  return @body["error_code"] if has_body_field("error_code")
  @code.to_s
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/klarna/response.rb, line 24
def method_missing(m, *args, &block)
  return @body[m.to_s] if has_body_field(m)
  super
end
respond_to?(m, *args, &block) click to toggle source
Calls superclass method
# File lib/klarna/response.rb, line 29
def respond_to?(m, *args, &block)
  return true if has_body_field(m)
  super
end
success?() click to toggle source
# File lib/klarna/response.rb, line 12
def success?
  @code < 300
end

Private Instance Methods

has_body_field(field) click to toggle source
# File lib/klarna/response.rb, line 54
def has_body_field(field)
  @body.is_a?(Hash) && @body.has_key?(field.to_s)
end
parse_body() click to toggle source
# File lib/klarna/response.rb, line 41
def parse_body
  return nil if @http_response.body.nil? || @http_response.body.empty?

  case @http_response.content_type
    when "application/json"
      JSON.parse(@http_response.body)
    when "text/html"
      http_response.body
    else
      http_response.body
  end
end