Class: Vocalware::Client

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

Overview

Vocalware client, which uses Vocalware’s REST API to generate speech.

Examples:

# Look up voice
voice = Vocalware::Voice.find(:lang => :en, :name => 'Kate')

# Initialize client
client =
  Vocalware::Client.new(
    :secret_phrase => SECRET_PHRASE,
    :api_id        => API_ID,
    :account_id    => ACCOUNT_ID,
    :voice         => voice
  )

# Generate mp3
client.gen("I love ruby!")  # => mp3 audio as binary string

Constant Summary collapse

DEFAULT_ATTRS =

Default attributes

{
  :protocol => 'http',
  :host     => 'www.vocalware.com',
  :path     => '/tts/gen.php',
  :ext      => 'mp3'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • attrs (Hash<Symbol, Object>) (defaults to: {})

    client attributes



60
61
62
63
64
65
66
67
# File 'lib/vocalware/client.rb', line 60

def initialize(attrs = {})
  DEFAULT_ATTRS.merge(attrs).each do |attr_name, value|
    send("#{attr_name}=", value)
  end

  @http_client ||= Faraday.new
  validate!
end

Instance Attribute Details

#account_idObject

account id (ACC)



21
22
23
# File 'lib/vocalware/client.rb', line 21

def 
  @account_id
end

#api_idObject

API id (API)



24
25
26
# File 'lib/vocalware/client.rb', line 24

def api_id
  @api_id
end

#extObject

whether “mp3” or “swf”



33
34
35
# File 'lib/vocalware/client.rb', line 33

def ext
  @ext
end

#hostObject

host to which request will be set



36
37
38
# File 'lib/vocalware/client.rb', line 36

def host
  @host
end

#http_clientObject



48
49
50
# File 'lib/vocalware/client.rb', line 48

def http_client
  @http_client
end

#pathObject

path as part of URL to send request



39
40
41
# File 'lib/vocalware/client.rb', line 39

def path
  @path
end

#portObject



45
46
47
# File 'lib/vocalware/client.rb', line 45

def port
  @port
end

#protocolObject

whether “http” or “https”



42
43
44
# File 'lib/vocalware/client.rb', line 42

def protocol
  @protocol
end

#secret_phraseObject

secret phrase



27
28
29
# File 'lib/vocalware/client.rb', line 27

def secret_phrase
  @secret_phrase
end

#voiceObject

voice



30
31
32
# File 'lib/vocalware/client.rb', line 30

def voice
  @voice
end

Instance Method Details

#build_url(text, opts = {}) ⇒ String

Build URL where request will be sent.

Parameters:

  • text (String)

    text to generate speech

  • opts (Hash<Symbol, Object>) (defaults to: {})

    options which override client attributes

Returns:

  • (String)

    url



88
89
90
91
92
# File 'lib/vocalware/client.rb', line 88

def build_url(text, opts = {})
  attrs = to_hash.merge(opts)
  attrs[:text] = text.strip
  Request.new(attrs).to_url
end

#gen(text, opts = {}) ⇒ String

Generate speech from passed text.

Parameters:

  • text (String)

    text to generate speech

  • opts (Hash<Symbol, Object>) (defaults to: {})

    options which override client attributes for one particular request.

Returns:

  • (String)

    audio data in format defined by :ext attribute



76
77
78
79
# File 'lib/vocalware/client.rb', line 76

def gen(text, opts = {})
  url = build_url(text, opts)
  send_request(url)
end