Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set custom timeout for Faraday request + fix headers in speech_to_text method #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/att_speech/att_speech.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ATTSpeech
include Celluloid
Celluloid.logger = nil

attr_reader :api_key, :secret_key, :access_token, :refresh_token, :base_url, :ssl_verify
attr_reader :api_key, :secret_key, :access_token, :refresh_token, :base_url, :ssl_verify, :timeout

##
# Creates an ATTSpeech object
Expand All @@ -17,11 +17,13 @@ class ATTSpeech
# @option args [String] :secret_key the AT&T Speech API Secret Key
# @option args [String] :base_url the url for the AT&T Speech API, default is 'https://api.att.com'
# @option args [Boolean] :ssl_verify determines if the peer Cert is verified for SSL, default is true
# @option args [Integer] :timeout timeout for http request in seconds, by default set to 60
# @overload initialize(api_key, secret_key, base_url='https://api.att.com')
# @param [String] api_key the AT&T Speech API Key
# @param [String] secret_key the AT&T Speech API Secret Key
# @param [String] base_url the url for the AT&T Speech API, default is 'https://api.att.com'
# @param [Boolean] ssl_verify determines if the peer Cert is verified for SSL, default is true
# @param [Integer] timeout timeout for http request in seconds, by default set to 60
#
# @return [Object] an instance of ATTSpeech
def initialize(*args)
Expand All @@ -35,11 +37,13 @@ def initialize(*args)
@secret_key = args[:secret_key]
@base_url = args[:base_url] || base_url
set_ssl_verify args[:ssl_verify]
@timeout = args[:timeout] || 60
else
@api_key = args.shift
@secret_key = args.shift
@base_url = args.shift || base_url
set_ssl_verify args.shift
@timeout = args.shift || 60
end

@grant_type = 'client_credentials'
Expand Down Expand Up @@ -68,22 +72,22 @@ def speech_to_text(file_contents, type='audio/wav', speech_context='Generic', op
type = "audio/amr"
end

headers = {
:Authorization => "Bearer #{@access_token}",
:Content_Transfer_Encoding => 'chunked',
:Accept => 'application/json'
}
headers = { :Authorization => "Bearer #{@access_token}", :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)
'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
headers.merge!({
:Content_Length => file_contents.length.to_s,
:Content_Type => type,
:X_SpeechContext => speech_context
})
body = file_contents
end

Expand Down Expand Up @@ -126,6 +130,7 @@ def create_connection(accept_type='application/json')
faraday.headers['Accept'] = accept_type
faraday.request :att_multipart
faraday.adapter Faraday.default_adapter
faraday.options[:timeout] = @timeout
end
end

Expand Down