Class: PetstoreApiClient::Response
- Inherits:
-
Object
- Object
- PetstoreApiClient::Response
- Defined in:
- lib/petstore_api_client/response.rb
Overview
HTTP response wrapper for Petstore API responses
Wraps Faraday HTTP responses and provides a clean, consistent interface for accessing response data. This class implements the Single Responsibility Principle by encapsulating all response handling logic in one place.
The Response object provides:
-
Parsed JSON body (automatically converted from JSON)
-
HTTP status code
-
Response headers
-
Success/error detection
-
Error message extraction
-
Access to raw Faraday response
Response bodies are automatically parsed from JSON. If the API returns non-JSON content (like HTML error pages), the body is returned as a string.
Instance Attribute Summary collapse
-
#body ⇒ Hash, ...
readonly
Parsed response body.
-
#headers ⇒ Hash
readonly
HTTP response headers.
- #raw_response ⇒ Object readonly
-
#status ⇒ Integer
readonly
HTTP status code (e.g., 200, 404, 500).
Instance Method Summary collapse
-
#error? ⇒ Boolean
Check if response indicates an error.
-
#error_code ⇒ Integer?
Extract error code from response body.
-
#error_message ⇒ String?
Extract error message from response body.
-
#error_type ⇒ String?
Extract error type from response body.
-
#initialize(faraday_response) ⇒ Response
constructor
Initialize a new Response wrapper.
-
#success? ⇒ Boolean
Check if the response was successful.
Constructor Details
#initialize(faraday_response) ⇒ Response
Initialize a new Response wrapper
Wraps a Faraday response object and extracts relevant data. The response body is automatically parsed from JSON to Ruby objects.
59 60 61 62 63 64 |
# File 'lib/petstore_api_client/response.rb', line 59 def initialize(faraday_response) @raw_response = faraday_response @status = faraday_response.status @body = parse_body(faraday_response.body) @headers = faraday_response.headers end |
Instance Attribute Details
#body ⇒ Hash, ... (readonly)
Returns Parsed response body.
46 |
# File 'lib/petstore_api_client/response.rb', line 46 attr_reader :status, :body, :headers, :raw_response |
#headers ⇒ Hash (readonly)
Returns HTTP response headers.
46 |
# File 'lib/petstore_api_client/response.rb', line 46 attr_reader :status, :body, :headers, :raw_response |
#raw_response ⇒ Object (readonly)
46 |
# File 'lib/petstore_api_client/response.rb', line 46 attr_reader :status, :body, :headers, :raw_response |
#status ⇒ Integer (readonly)
Returns HTTP status code (e.g., 200, 404, 500).
46 47 48 |
# File 'lib/petstore_api_client/response.rb', line 46 def status @status end |
Instance Method Details
#error? ⇒ Boolean
Check if response indicates an error
A response is considered an error if the HTTP status code is outside the 2xx range.
96 97 98 |
# File 'lib/petstore_api_client/response.rb', line 96 def error? !success? end |
#error_code ⇒ Integer?
Extract error code from response body
Returns the error code from the response if available. Falls back to HTTP status code if no error code is present in body.
145 146 147 148 149 |
# File 'lib/petstore_api_client/response.rb', line 145 def error_code return nil unless error? body.is_a?(Hash) ? (body["code"] || body[:code]) : status end |
#error_message ⇒ String?
Extract error message from response body
Attempts to extract a human-readable error message from the response. Handles various response formats:
-
JSON with “message” key
-
Plain text error messages
-
HTML error pages
-
Empty/nil responses
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/petstore_api_client/response.rb', line 119 def return nil unless error? # Extract message from different response formats case body when Hash body["message"] || body[:message] || "Unknown error" when String # Sometimes the API returns HTML instead of JSON (sigh...) body.include?("<html>") ? "Request failed with status #{status}" : body else "Request failed with status #{status}" end end |
#error_type ⇒ String?
Extract error type from response body
Returns the error type/category from the response if available. This is useful for programmatic error handling.
162 163 164 165 166 |
# File 'lib/petstore_api_client/response.rb', line 162 def error_type return nil unless error? body.is_a?(Hash) ? (body["type"] || body[:type]) : nil end |
#success? ⇒ Boolean
Check if the response was successful
A response is considered successful if the HTTP status code is in the 2xx range (200-299).
79 80 81 |
# File 'lib/petstore_api_client/response.rb', line 79 def success? (200..299).cover?(status) end |