Class: TranslationAPI::Provider::Gemini::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_api/provider/gemini/response.rb

Constant Summary collapse

REQUEST_FAILED_MESSAGE =
"Request failed with status"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



13
14
15
# File 'lib/translation_api/provider/gemini/response.rb', line 13

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/translation_api/provider/gemini/response.rb', line 11

def response
  @response
end

Instance Method Details

#dig_used_tokens(type:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/translation_api/provider/gemini/response.rb', line 24

def dig_used_tokens(type:)
  case type
  when :input
    body_json.dig("usageMetadata", "promptTokenCount")
  when :output
    body_json.dig("usageMetadata", "candidatesTokenCount")
  else
    raise ArgumentError, "Invalid token type: #{type}"
  end
end

#translated_textObject

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/translation_api/provider/gemini/response.rb', line 17

def translated_text
  failed_message = "#{REQUEST_FAILED_MESSAGE} #{@response.status}"
  raise ArgumentError, failed_message unless @response.status == 200

  body_json.dig("candidates", 0, "content", "parts", 0, "text")
end