Class: ReveAI::ImageResponse

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

Overview

Response wrapper for image generation API responses.

Provides convenient accessors for image data, version info, content policy status, and credit usage. All accessors work for both JSON responses (values from the parsed body) and binary responses (values from the X-Reve-* headers).

Examples:

Accessing image data

result = client.images.create(prompt: "A sunset")
png_data = Base64.decode64(result.base64)
File.binwrite("image.png", png_data)

Checking content policy

result = client.images.create(prompt: "...")
if result.content_violation?
  puts "Content policy violated"
end

Tracking credit usage

result = client.images.create(prompt: "A cat")
puts "Used #{result.credits_used} credits, #{result.credits_remaining} remaining"

See Also:

Instance Attribute Summary

Attributes inherited from Response

#body, #headers, #status

Instance Method Summary collapse

Methods inherited from Response

#binary?, #initialize, #request_id, #success?

Constructor Details

This class inherits a constructor from ReveAI::Response

Instance Method Details

#base64String?

Note:

Despite the name, binary responses (Accept: image/*) return raw image bytes here, not base64 data.

Alias for #image.

Returns:

  • (String, nil)

    Base64 encoded image data (JSON response) or raw image bytes (binary response)

See Also:



123
124
125
# File 'lib/reve_ai/response.rb', line 123

def base64
  image
end

#content_violation?Boolean

Checks if the generated image violates content policy.

Examples:

if result.content_violation?
  puts "Warning: Content policy violated"
end

Returns:

  • (Boolean)

    true if content policy was violated



156
157
158
159
# File 'lib/reve_ai/response.rb', line 156

def content_violation?
  body_value(:content_violation) == true ||
    headers["x-reve-content-violation"] == "true"
end

#credits_remainingInteger?

Returns the number of credits remaining after this request.

Examples:

if result.credits_remaining < 100
  puts "Warning: Low credit balance"
end

Returns:

  • (Integer, nil)

    Remaining credit balance



179
180
181
# File 'lib/reve_ai/response.rb', line 179

def credits_remaining
  body_value(:credits_remaining) || headers["x-reve-credits-remaining"]&.to_i
end

#credits_usedInteger?

Returns the number of credits used for this request.

Examples:

puts "This request used #{result.credits_used} credits"

Returns:

  • (Integer, nil)

    Credits consumed by this generation



167
168
169
# File 'lib/reve_ai/response.rb', line 167

def credits_used
  body_value(:credits_used) || headers["x-reve-credits-used"]&.to_i
end

#imageString?

Returns the image data.

For JSON responses this is the base64 encoded image; for binary responses (Accept: image/*) it is the raw image bytes in the negotiated format, ready to write to disk without decoding.

Examples:

Save a JSON (base64) response to file

require "base64"
File.binwrite("output.png", Base64.decode64(result.image))

Save a binary response to file (no Base64 decode needed)

File.binwrite("output.webp", result.image)

Returns:

  • (String, nil)

    Base64 encoded image data (JSON response) or raw image bytes (binary response)



111
112
113
# File 'lib/reve_ai/response.rb', line 111

def image
  binary? ? body : body[:image]
end

#layoutHash?

Returns the layout object for this generation.

Present on v2 create/render JSON responses; nil on v1 responses and on binary (Accept: image/*) responses.

Returns:

  • (Hash, nil)

    Layout Hash (e.g., prompt, regions, width, height), or nil when absent



134
135
136
# File 'lib/reve_ai/response.rb', line 134

def layout
  body_value(:layout)
end

#versionString?

Returns the model version used for generation.

Examples:

result.version # => "reve-create@20250915"

Returns:

  • (String, nil)

    Model version (e.g., "reve-create@20250915")



144
145
146
# File 'lib/reve_ai/response.rb', line 144

def version
  body_value(:version) || headers["x-reve-version"]
end