Class: Typecast::Client
- Inherits:
-
Object
- Object
- Typecast::Client
- Defined in:
- lib/typecast/client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.typecast.ai".freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #clone_voice(audio:, filename:, name:, model:) ⇒ Object
- #delete_voice(voice_id) ⇒ Object
- #get_my_subscription ⇒ Object
- #get_voice_v2(voice_id) ⇒ Object
- #get_voices_v2(filter = nil) ⇒ Object
-
#initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30) ⇒ Client
constructor
A new instance of Client.
- #text_to_speech(request) ⇒ Object
- #text_to_speech_stream(request) {|response.body| ... } ⇒ Object
- #text_to_speech_with_timestamps(request, granularity: nil) ⇒ Object
Constructor Details
#initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 |
# File 'lib/typecast/client.rb', line 16 def initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30) @api_key = api_key.to_s @base_url = normalize_base_url(base_url) @open_timeout = open_timeout @read_timeout = read_timeout end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/typecast/client.rb', line 14 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
14 15 16 |
# File 'lib/typecast/client.rb', line 14 def base_url @base_url end |
Instance Method Details
#clone_voice(audio:, filename:, name:, model:) ⇒ Object
70 71 72 73 74 |
# File 'lib/typecast/client.rb', line 70 def clone_voice(audio:, filename:, name:, model:) validate_clone_inputs(audio, name) response = request_multipart("/v1/voices/clone", audio: audio, filename: filename, fields: { name: name, model: model }) Models::CustomVoice.from_h(JSON.parse(response.body)) end |
#delete_voice(voice_id) ⇒ Object
76 77 78 79 |
# File 'lib/typecast/client.rb', line 76 def delete_voice(voice_id) request_raw(:delete, "/v1/voices/#{path_segment(voice_id)}") nil end |
#get_my_subscription ⇒ Object
50 51 52 |
# File 'lib/typecast/client.rb', line 50 def get_my_subscription Models::SubscriptionResponse.from_h(JSON.parse(request_json(:get, "/v1/users/me/subscription").body)) end |
#get_voice_v2(voice_id) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/typecast/client.rb', line 61 def get_voice_v2(voice_id) voices = JSON.parse(request_json(:get, "/v2/voices/#{path_segment(voice_id)}").body).map do |item| Models::VoiceV2.from_h(item) end raise NotFoundError, "Voice not found: #{voice_id}" if voices.empty? voices.first end |
#get_voices_v2(filter = nil) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/typecast/client.rb', line 54 def get_voices_v2(filter = nil) query = filter.respond_to?(:to_h) ? filter.to_h : filter JSON.parse(request_json(:get, "/v2/voices", nil, query).body).map do |item| Models::VoiceV2.from_h(item) end end |
#text_to_speech(request) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/typecast/client.rb', line 23 def text_to_speech(request) response = request_json(:post, "/v1/text-to-speech", request.to_h) Models::TTSResponse.new( audio_data: response.body, duration: response["X-Audio-Duration"].to_f, format: response["Content-Type"].to_s.include?("mp3") || response["Content-Type"].to_s.include?("mpeg") ? Models::AUDIO_MP3 : Models::AUDIO_WAV ) end |
#text_to_speech_stream(request) {|response.body| ... } ⇒ Object
32 33 34 35 36 37 |
# File 'lib/typecast/client.rb', line 32 def text_to_speech_stream(request) response = request_json(:post, "/v1/text-to-speech/stream", request.to_h) return enum_for(:text_to_speech_stream, request) unless block_given? yield response.body end |
#text_to_speech_with_timestamps(request, granularity: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/typecast/client.rb', line 39 def (request, granularity: nil) unless granularity.nil? || %w[word char].include?(granularity) raise ArgumentError, "granularity must be 'word' or 'char'" end path = "/v1/text-to-speech/with-timestamps" query = granularity.nil? ? nil : { granularity: granularity } response = request_json(:post, path, request.to_h, query) Models::TTSWithTimestampsResponse.from_h(JSON.parse(response.body)) end |