Class: HeartlandRetail::Client::Response
- Inherits:
-
Object
- Object
- HeartlandRetail::Client::Response
- Defined in:
- lib/heartland/client/response.rb
Overview
An API response including body, headers, and status information.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the corresponding key from “body”.
-
#body ⇒ Body
Returns the parsed response body as a Body object.
-
#initialize(response, client) ⇒ Response
constructor
A new instance of Response.
-
#method_missing(method, *args, &block) ⇒ Object
Delegates missing methods to the underlying Faraday::Response.
-
#raw_body ⇒ String
Returns the raw response body as a String.
-
#resource ⇒ Resource
If the response included a ‘Location’ header, returns a new Resource with a URI set to its value, else nil.
-
#success? ⇒ Boolean
Returns true if the request was successful, else false.
Constructor Details
#initialize(response, client) ⇒ Response
Returns a new instance of Response.
9 10 11 12 |
# File 'lib/heartland/client/response.rb', line 9 def initialize(response, client) @response = response @client = client end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegates missing methods to the underlying Faraday::Response.
50 51 52 |
# File 'lib/heartland/client/response.rb', line 50 def method_missing(method, *args, &block) @response.respond_to?(method) ? @response.__send__(method, *args, &block) : super end |
Instance Method Details
#[](key) ⇒ Object
Returns the corresponding key from “body”.
16 17 18 |
# File 'lib/heartland/client/response.rb', line 16 def [](key) body[key] end |
#body ⇒ Body
Returns the parsed response body as a Body object.
34 35 36 |
# File 'lib/heartland/client/response.rb', line 34 def body @data ||= parse_body end |
#raw_body ⇒ String
Returns the raw response body as a String.
24 25 26 |
# File 'lib/heartland/client/response.rb', line 24 def raw_body @response.body end |
#resource ⇒ Resource
If the response included a ‘Location’ header, returns a new Resource with a URI set to its value, else nil.
59 60 61 62 63 64 65 |
# File 'lib/heartland/client/response.rb', line 59 def resource if location = headers['Location'] @client[headers['Location']] else nil end end |
#success? ⇒ Boolean
Returns true if the request was successful, else false.
42 43 44 |
# File 'lib/heartland/client/response.rb', line 42 def success? status < 400 end |