Class: FlowSpeech::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flowspeech/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://flowspeech.io"

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV["FLOWSPEECH_API_KEY"], base_url: DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 90, transport: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flowspeech/client.rb', line 51

def initialize(api_key: ENV["FLOWSPEECH_API_KEY"], base_url: DEFAULT_BASE_URL,
               open_timeout: 10, read_timeout: 90, transport: nil)
  @api_key = api_key.to_s.strip
  @base_url = URI(base_url)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @transport = transport

  raise ArgumentError, "api_key is required" if @api_key.empty?
  unless @base_url.is_a?(URI::HTTP) && @base_url.host
    raise ArgumentError, "base_url must be an HTTP or HTTPS URL"
  end
end

Instance Method Details

#quotaObject



65
66
67
# File 'lib/flowspeech/client.rb', line 65

def quota
  request(:get, "/api/ai/text-to-speech/quota").fetch("quota")
end

#synthesize(text:, speakers: [{ voice_name: "Kore" }], original_text: nil) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/flowspeech/client.rb', line 69

def synthesize(text:, speakers: [{ voice_name: "Kore" }], original_text: nil)
  raise ArgumentError, "text is required" if text.to_s.strip.empty?

  normalized_speakers = Array(speakers).map { |speaker| normalize_speaker(speaker) }
  raise ArgumentError, "at least one speaker is required" if normalized_speakers.empty?

  data = request(
    :post,
    "/api/ai/text-to-speech",
    text: text,
    originalText: original_text || text,
    speakers: normalized_speakers
  )
  raise Error, "FlowSpeech response did not include audio" if data["audioBase64"].to_s.empty?

  Audio.new(data)
end