Class: ATTSpeech
- Inherits:
-
Object
- Object
- ATTSpeech
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#ssl_verify ⇒ Object
readonly
Returns the value of attribute ssl_verify.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Object
constructor
Creates an ATTSpeech object.
-
#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.
-
#text_to_speech(text_data, options = {}) ⇒ String
Allows you to send a string or plain text file and return the text to speech result.
Constructor Details
#initialize(args) ⇒ Object #initialize(api_key, secret_key, base_url = 'https://api.att.com') ⇒ Object
Creates an ATTSpeech object
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_token ⇒ Object (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_key ⇒ Object (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_url ⇒ Object (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_token ⇒ Object (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_key ⇒ Object (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_verify ⇒ Object (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
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', = {}) 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 .has_key?(:grammar) # Assume this is a Speech-To-Text-Custom query resource << 'Custom' [:grammar] = "<?xml version=\"1.0\"?>\n#{[:grammar]}" body = { 'x-grammar' => Faraday::UploadIO.new(StringIO.new([: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
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, = {}) resource = '/speech/v3/textToSpeech' params = { :Authorization => "Bearer #{@access_token}", :Content_Type => 'text/plain', :Accept => 'audio/x-wav' }.merge() begin response = @connection.post( resource, text_data, params ) response.body rescue => e raise RuntimeError, e.to_s end end |