Class: FriendlyShipping::Response
- Inherits:
-
Object
- Object
- FriendlyShipping::Response
- Defined in:
- lib/friendly_shipping/response.rb
Overview
Represents an HTTP response received from a carrier API.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The HTTP response body.
-
#headers ⇒ Hash
readonly
The HTTP response headers.
-
#status ⇒ Integer
(also: #code)
readonly
The HTTP response status code.
Class Method Summary collapse
-
.new_from_rest_client_response(response) ⇒ Response
Constructs a new Response from a
RestClient::Response
object.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true if the given object shares the same class and attributes with this response.
-
#attributes ⇒ Array
protected
Returns the status, body, and headers from this response.
-
#hash ⇒ Hash
Returns this response's attributes as a hash.
-
#initialize(status:, body:, headers:) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(status:, body:, headers:) ⇒ Response
Returns a new instance of Response.
18 19 20 21 22 |
# File 'lib/friendly_shipping/response.rb', line 18 def initialize(status:, body:, headers:) @status = status @body = body @headers = headers || {} end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns the HTTP response body.
10 11 12 |
# File 'lib/friendly_shipping/response.rb', line 10 def body @body end |
#headers ⇒ Hash (readonly)
Returns the HTTP response headers.
13 14 15 |
# File 'lib/friendly_shipping/response.rb', line 13 def headers @headers end |
#status ⇒ Integer (readonly) Also known as: code
Returns the HTTP response status code.
7 8 9 |
# File 'lib/friendly_shipping/response.rb', line 7 def status @status end |
Class Method Details
.new_from_rest_client_response(response) ⇒ Response
Constructs a new FriendlyShipping::Response from a RestClient::Response
object.
29 30 31 |
# File 'lib/friendly_shipping/response.rb', line 29 def self.new_from_rest_client_response(response) new(status: response&.code, body: response&.body, headers: response&.headers) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true if the given object shares the same class and attributes with this response.
36 37 38 39 |
# File 'lib/friendly_shipping/response.rb', line 36 def ==(other) other.class == self.class && other.attributes == attributes end |
#attributes ⇒ Array (protected)
Returns the status, body, and headers from this response.
53 54 55 |
# File 'lib/friendly_shipping/response.rb', line 53 def attributes [status, body, headers] end |
#hash ⇒ Hash
Returns this response's attributes as a hash.
45 46 47 |
# File 'lib/friendly_shipping/response.rb', line 45 def hash attributes.hash end |