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.



7
8
9
10
11
12
# File 'lib/klarna/response.rb', line 7

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(method, *args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/klarna/response.rb', line 26

def method_missing(method, *args, &block)
  return @body[method.to_s] if body_field?(method)

  super
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/klarna/response.rb', line 5

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/klarna/response.rb', line 5

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/klarna/response.rb', line 5

def headers
  @headers
end

#http_responseObject (readonly)

Returns the value of attribute http_response.



5
6
7
# File 'lib/klarna/response.rb', line 5

def http_response
  @http_response
end

Instance Method Details

#[](index) ⇒ Object



22
23
24
# File 'lib/klarna/response.rb', line 22

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

#error?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/klarna/response.rb', line 18

def error?
  @code >= 300
end

#error_codeObject



38
39
40
41
42
# File 'lib/klarna/response.rb', line 38

def error_code
  return @body['error_code'] if body_field?('error_code')

  @code.to_s
end

#respond_to_missing?(method, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method, *args, &block)
  return true if body_field?(method)

  super
end

#success?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/klarna/response.rb', line 14

def success?
  @code < 300
end