Class: PetstoreApiClient::Response

Inherits:
Object
  • Object
show all
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.

Examples:

Accessing a successful response

response = client.get("/pet/123")
if response.success?
  pet = response.body
  puts pet["name"]
end

Handling an error response

response = client.get("/pet/invalid")
if response.error?
  puts response.error_message
  puts response.error_code
end

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

faraday_response = connection.get("/pet/123")
response = Response.new(faraday_response)

Parameters:

  • faraday_response (Faraday::Response)

    The Faraday response object

Since:

  • 0.1.0



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

#bodyHash, ... (readonly)

Returns Parsed response body.

Returns:

  • (Hash, Array, String)

    Parsed response body



46
# File 'lib/petstore_api_client/response.rb', line 46

attr_reader :status, :body, :headers, :raw_response

#headersHash (readonly)

Returns HTTP response headers.

Returns:

  • (Hash)

    HTTP response headers



46
# File 'lib/petstore_api_client/response.rb', line 46

attr_reader :status, :body, :headers, :raw_response

#raw_responseObject (readonly)

Since:

  • 0.1.0



46
# File 'lib/petstore_api_client/response.rb', line 46

attr_reader :status, :body, :headers, :raw_response

#statusInteger (readonly)

Returns HTTP status code (e.g., 200, 404, 500).

Returns:

  • (Integer)

    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.

Examples:

response = client.get("/pet/invalid")
if response.error?
  puts response.error_message
end

Returns:

  • (Boolean)

    true if status is not 200-299, false otherwise

Since:

  • 0.1.0



96
97
98
# File 'lib/petstore_api_client/response.rb', line 96

def error?
  !success?
end

#error_codeInteger?

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.

Examples:

# API returns: { "code": 1, "type": "NotFound", "message": "Pet not found" }
response.error_code # => 1

Returns:

  • (Integer, nil)

    Error code if response is an error, nil otherwise

Since:

  • 0.1.0



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_messageString?

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

Examples:

JSON error response

# API returns: { "code": 404, "type": "NotFound", "message": "Pet not found" }
response.error_message # => "Pet not found"

HTML error response

# API returns HTML error page
response.error_message # => "Request failed with status 500"

Returns:

  • (String, nil)

    Error message if response is an error, nil otherwise

Since:

  • 0.1.0



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/petstore_api_client/response.rb', line 119

def error_message
  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_typeString?

Extract error type from response body

Returns the error type/category from the response if available. This is useful for programmatic error handling.

Examples:

# API returns: { "code": 1, "type": "NotFound", "message": "Pet not found" }
response.error_type # => "NotFound"

Returns:

  • (String, nil)

    Error type if available, nil otherwise

Since:

  • 0.1.0



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).

Examples:

response = client.get("/pet/123")
if response.success?
  # Handle successful response
end

Returns:

  • (Boolean)

    true if status is 200-299, false otherwise

Since:

  • 0.1.0



79
80
81
# File 'lib/petstore_api_client/response.rb', line 79

def success?
  (200..299).cover?(status)
end