Class: ATTSpeech

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/att_speech/version.rb,
lib/att_speech/att_speech.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Object #initialize(api_key, secret_key, base_url = 'https://api.att.com') ⇒ Object

Creates an ATTSpeech object

Overloads:

  • #initialize(args) ⇒ Object

    Parameters:

    • args (Hash)

      the options to intantiate with

    Options Hash (args):

    • :api_key (String)

      the AT&T Speech API Key

    • :secret_key (String)

      the AT&T Speech API Secret Key

    • :base_url (String)

      the url for the AT&T Speech API, default is ‘api.att.com

    • :ssl_verify (Boolean)

      determines if the peer Cert is verified for SSL, default is true

  • #initialize(api_key, secret_key, base_url = 'https://api.att.com') ⇒ Object

    Parameters:

    • api_key (String)

      the AT&T Speech API Key

    • secret_key (String)

      the AT&T Speech API Secret Key

    • base_url (String) (defaults to: 'https://api.att.com')

      the url for the AT&T Speech API, default is ‘api.att.com

    • ssl_verify (Boolean)

      determines if the peer Cert is verified for SSL, default is true

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/att_speech/att_speech.rb', line 27

def initialize(*args)
  raise ArgumentError, "Requires at least the api_key and secret_key when instatiating" if args.size == 0

  base_url   = 'https://api.att.com'

  if args.size == 1 && args[0].instance_of?(Hash)
    args = args.shift
    @api_key    = args[:api_key]
    @secret_key = args[:secret_key]
    @base_url   = args[:base_url]   || base_url
    set_ssl_verify args[:ssl_verify]
  else
    @api_key    = args.shift
    @secret_key = args.shift
    @base_url   = args.shift || base_url
    set_ssl_verify args.shift
  end

  @grant_type    = 'client_credentials'
  @access_token  = ''
  @refresh_token = ''

  create_connection 'application/json'

  get_tokens

  Actor.current
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def access_token
  @access_token
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def base_url
  @base_url
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def refresh_token
  @refresh_token
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def secret_key
  @secret_key
end

#ssl_verifyObject (readonly)

Returns the value of attribute ssl_verify.



9
10
11
# File 'lib/att_speech/att_speech.rb', line 9

def ssl_verify
  @ssl_verify
end

Instance Method Details

#speech_to_text(file_contents, type = 'audio/wav', speech_context = 'Generic', options = {}) ⇒ Hash

Allows you to send a file and return the speech to text result

Parameters:

  • file_contents (String)

    to be processed

  • type (String) (defaults to: 'audio/wav')

    of file to be processed, may be audio/wav, application/octet-stream or audio/amr

  • speech_context (String) (defaults to: 'Generic')

    to use to evaluate the audio BusinessSearch, Gaming, Generic, QuestionAndAnswer, SMS, SocialMedia, TV, VoiceMail, WebSearch

Returns:

  • (Hash)

    the resulting response from the AT&T Speech API



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/att_speech/att_speech.rb', line 63

def speech_to_text(file_contents, type='audio/wav', speech_context='Generic', options = {})
  resource = "/speech/v3/speechToText"

  # FIXME: Is this necessary?
  if type == "application/octet-stream"
    type = "audio/amr"
  end

  headers = {
    :Authorization             => "Bearer #{@access_token}",
    :Content_Transfer_Encoding => 'chunked',
    :Accept                    => 'application/json'
  }

  if options.has_key?(:grammar)
    # Assume this is a Speech-To-Text-Custom query
    resource << 'Custom'
    options[:grammar] = "<?xml version=\"1.0\"?>\n#{options[:grammar]}"
    body = {
      'x-grammar' => Faraday::UploadIO.new(StringIO.new(options[:grammar]), 'application/srgs+xml'),
      'x-voice'   => Faraday::UploadIO.new(StringIO.new(file_contents), type)
    }
  else
    headers[:X_SpeechContext] = speech_context
    body = file_contents
  end

  response = @connection.post resource, body, headers

  result = process_response(response)
  result
end

#text_to_speech(text_data, options = {}) ⇒ String

Allows you to send a string or plain text file and return the text to speech result

Parameters:

  • text_data (String)

    string or file_contents to be processed

  • options (String) (defaults to: {})

    hash with options which will be send to AT&T Speech API

Returns:

  • (String)

    the bytes of the resulting response from the AT&T Speech API



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/att_speech/att_speech.rb', line 103

def text_to_speech(text_data, options = {})
  resource = '/speech/v3/textToSpeech'
  params = {
    :Authorization => "Bearer #{@access_token}",
    :Content_Type  => 'text/plain',
    :Accept        => 'audio/x-wav'
  }.merge(options)

  begin
    response = @connection.post( resource, text_data, params )
    response.body
  rescue => e
    raise RuntimeError, e.to_s
  end
end