Class: Aigen::Google::ImageResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/aigen/google/image_response.rb

Overview

Wraps an image generation API response with convenient helper methods. Provides easy access to generated images, text descriptions, and status information.

Examples:

Basic usage

response = client.generate_image("A cute puppy")
if response.success?
  response.save("puppy.png")
  puts response.text
end

Checking for failures

response = client.generate_image("problematic prompt")
unless response.success?
  puts "Failed: #{response.failure_reason}"
  puts response.failure_message
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ImageResponse

Creates a new ImageResponse from a Gemini API response.

Parameters:

  • response (Hash)

    the raw API response hash



29
30
31
# File 'lib/aigen/google/image_response.rb', line 29

def initialize(response)
  @raw_response = response
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



24
25
26
# File 'lib/aigen/google/image_response.rb', line 24

def raw_response
  @raw_response
end

Instance Method Details

#failure_messageString?

Returns the failure message for failed generations.

Returns:

  • (String, nil)

    the failure message or nil if successful



95
96
97
98
99
# File 'lib/aigen/google/image_response.rb', line 95

def failure_message
  return nil if success?

  candidate.dig("finishMessage")
end

#failure_reasonString?

Returns the finish reason for failed generations.

Returns:

  • (String, nil)

    the finish reason or nil if successful



86
87
88
89
90
# File 'lib/aigen/google/image_response.rb', line 86

def failure_reason
  return nil if success?

  candidate.dig("finishReason")
end

#has_image?Boolean

Checks if an image is present in the response.

Returns:

  • (Boolean)

    true if image data exists, false otherwise



43
44
45
# File 'lib/aigen/google/image_response.rb', line 43

def has_image?
  !image_part.nil?
end

#image_dataString?

Returns the decoded binary image data.

Returns:

  • (String, nil)

    binary image data or nil if no image present



57
58
59
60
61
# File 'lib/aigen/google/image_response.rb', line 57

def image_data
  return nil unless has_image?

  Base64.decode64(image_part["inlineData"]["data"])
end

#mime_typeString?

Returns the MIME type of the generated image.

Returns:

  • (String, nil)

    MIME type (e.g., “image/png”) or nil if no image



66
67
68
# File 'lib/aigen/google/image_response.rb', line 66

def mime_type
  image_part&.dig("inlineData", "mimeType")
end

#save(path) ⇒ Object

Saves the generated image to the specified file path.

Examples:

response.save("output.png")

Parameters:

  • path (String)

    the file path to save the image

Raises:



77
78
79
80
81
# File 'lib/aigen/google/image_response.rb', line 77

def save(path)
  raise Error, "No image data to save" unless has_image?

  File.write(path, image_data)
end

#success?Boolean

Checks if the image generation was successful.

Returns:

  • (Boolean)

    true if generation succeeded, false otherwise



36
37
38
# File 'lib/aigen/google/image_response.rb', line 36

def success?
  finish_reason == "STOP"
end

#textString?

Returns the text description that accompanied the generated image.

Returns:

  • (String, nil)

    the text description or nil if not present



50
51
52
# File 'lib/aigen/google/image_response.rb', line 50

def text
  text_part&.dig("text")
end