Class: Klarna::Response
- Inherits:
-
Object
- Object
- Klarna::Response
- Defined in:
- lib/klarna/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #error? ⇒ Boolean
- #error_code ⇒ Object
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to_missing?(method, *args, &block) ⇒ Boolean
- #success? ⇒ Boolean
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/klarna/response.rb', line 5 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/klarna/response.rb', line 5 def code @code end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/klarna/response.rb', line 5 def headers @headers end |
#http_response ⇒ Object (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
18 19 20 |
# File 'lib/klarna/response.rb', line 18 def error? @code >= 300 end |
#error_code ⇒ Object
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
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
14 15 16 |
# File 'lib/klarna/response.rb', line 14 def success? @code < 300 end |