Class: ReveAI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/reve_ai/response.rb

Overview

Base response wrapper for API responses.

Provides access to HTTP status, headers, and response body. The body is a parsed Hash for JSON responses, or the raw image String (bytes) when the API answers with a binary body (Accept: image/); in that case all metadata is carried by the X-Reve- response headers.

Direct Known Subclasses

ImageResponse, LayoutResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers:, body:) ⇒ Response

Creates a new response wrapper.

Parameters:

  • status (Integer)

    HTTP status code

  • headers (Hash)

    Response headers

  • body (Hash, String)

    Parsed response body, or raw bytes String



29
30
31
32
33
# File 'lib/reve_ai/response.rb', line 29

def initialize(status:, headers:, body:)
  @status = status
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyHash, String (readonly)

Returns Parsed response body, or raw bytes String for binary (image/*) responses.

Returns:

  • (Hash, String)

    Parsed response body, or raw bytes String for binary (image/*) responses



22
23
24
# File 'lib/reve_ai/response.rb', line 22

def body
  @body
end

#headersHash (readonly)

Returns Response headers.

Returns:

  • (Hash)

    Response headers



18
19
20
# File 'lib/reve_ai/response.rb', line 18

def headers
  @headers
end

#statusInteger (readonly)

Returns HTTP status code.

Returns:

  • (Integer)

    HTTP status code



15
16
17
# File 'lib/reve_ai/response.rb', line 15

def status
  @status
end

Instance Method Details

#binary?Boolean

Checks if the response body is raw binary data (e.g., image bytes).

Returns:

  • (Boolean)

    true if body is not a parsed JSON Hash



45
46
47
# File 'lib/reve_ai/response.rb', line 45

def binary?
  !body.is_a?(Hash)
end

#request_idString?

Returns the request ID for this response.

Useful for debugging and support requests.

Returns:

  • (String, nil)

    Request ID from body or headers



54
55
56
# File 'lib/reve_ai/response.rb', line 54

def request_id
  body_value(:request_id) || headers["x-reve-request-id"]
end

#success?Boolean

Checks if the response indicates success (2xx status).

Returns:

  • (Boolean)

    true if status is between 200 and 299



38
39
40
# File 'lib/reve_ai/response.rb', line 38

def success?
  status >= 200 && status < 300
end