Class: VoiceTextAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/voice_text_api.rb,
lib/voice_text_api/version.rb

Defined Under Namespace

Classes: BadRequest, Unauthoeized

Constant Summary collapse

ENDPOINT =
URI('https://api.voicetext.jp/v1/tts')
SPEAKERS =
%w(show haruka hikari takeru)
EMOTIONS =
%w(happiness anger sadness)
HIGHT =

emotion levels

2
NORMAL =
1
VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ VoiceTextAPI

Returns a new instance of VoiceTextAPI.



17
18
19
# File 'lib/voice_text_api.rb', line 17

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#tts(text, speaker = :show, emotion: nil, emotion_level: NORMAL, pitch: 100, speed: 100, volume: 100) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/voice_text_api.rb', line 21

def tts(text, speaker = :show, emotion: nil, emotion_level: NORMAL, pitch: 100, speed: 100, volume: 100)
  validate_parameters(speaker, emotion, emotion_level, pitch, speed, volume)

  res = nil
  https = Net::HTTP.new(ENDPOINT.host, 443)
  https.use_ssl = true
  https.start do |https|
    req = create_request(https, text, speaker, emotion, emotion_level, pitch, speed, volume)
    res = https.request(req)
  end

  case res
  when Net::HTTPOK
    res.body
  when Net::HTTPBadRequest
    raise BadRequest.new(res.body)
  when Net::HTTPUnauthorized
    raise Unauthoeized.new(res.body)
  else
    raise StandardError.new(res.body)
  end
end