Class: Klarna::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/klarna/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



24
25
26
27
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/klarna/response.rb', line 3

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/klarna/response.rb', line 3

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/klarna/response.rb', line 3

def headers
  @headers
end

#http_responseObject (readonly)

Returns the value of attribute http_response.



3
4
5
# File 'lib/klarna/response.rb', line 3

def http_response
  @http_response
end

Instance Method Details

#[](index) ⇒ Object



20
21
22
# File 'lib/klarna/response.rb', line 20

def [](index)
  http_response.send("[]", index)
end

#error?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/klarna/response.rb', line 16

def error?
  @code >= 300
end

#error_codeObject



34
35
36
37
# File 'lib/klarna/response.rb', line 34

def error_code
  return @body["error_code"] if has_body_field("error_code")
  @code.to_s
end

#respond_to?(m, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/klarna/response.rb', line 29

def respond_to?(m, *args, &block)
  return true if has_body_field(m)
  super
end

#success?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/klarna/response.rb', line 12

def success?
  @code < 300
end