Class: Aigen::Google::ImageResponse
- Inherits:
-
Object
- Object
- Aigen::Google::ImageResponse
- 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.
Instance Attribute Summary collapse
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
Instance Method Summary collapse
-
#failure_message ⇒ String?
Returns the failure message for failed generations.
-
#failure_reason ⇒ String?
Returns the finish reason for failed generations.
-
#has_image? ⇒ Boolean
Checks if an image is present in the response.
-
#image_data ⇒ String?
Returns the decoded binary image data.
-
#initialize(response) ⇒ ImageResponse
constructor
Creates a new ImageResponse from a Gemini API response.
-
#mime_type ⇒ String?
Returns the MIME type of the generated image.
-
#save(path) ⇒ Object
Saves the generated image to the specified file path.
-
#success? ⇒ Boolean
Checks if the image generation was successful.
-
#text ⇒ String?
Returns the text description that accompanied the generated image.
Constructor Details
#initialize(response) ⇒ ImageResponse
Creates a new ImageResponse from a Gemini API response.
29 30 31 |
# File 'lib/aigen/google/image_response.rb', line 29 def initialize(response) @raw_response = response end |
Instance Attribute Details
#raw_response ⇒ Object (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_message ⇒ String?
Returns the failure message for failed generations.
95 96 97 98 99 |
# File 'lib/aigen/google/image_response.rb', line 95 def return nil if success? candidate.dig("finishMessage") end |
#failure_reason ⇒ String?
Returns the finish reason for failed generations.
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.
43 44 45 |
# File 'lib/aigen/google/image_response.rb', line 43 def has_image? !image_part.nil? end |
#image_data ⇒ String?
Returns the decoded binary image data.
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_type ⇒ String?
Returns the MIME type of the generated 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.
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.
36 37 38 |
# File 'lib/aigen/google/image_response.rb', line 36 def success? finish_reason == "STOP" end |
#text ⇒ String?
Returns the text description that accompanied the generated image.
50 51 52 |
# File 'lib/aigen/google/image_response.rb', line 50 def text text_part&.dig("text") end |