diff --git a/Speech/Ruby/app1/Gemfile b/Speech/Ruby/app1/Gemfile new file mode 100644 index 0000000..1ba9900 --- /dev/null +++ b/Speech/Ruby/app1/Gemfile @@ -0,0 +1,5 @@ +source "http://rubygems.org" +gem "json" +gem "rest-client" +gem "sinatra" +gem "sinatra-contrib" \ No newline at end of file diff --git a/Speech/Ruby/app1/config.ru b/Speech/Ruby/app1/config.ru new file mode 100644 index 0000000..e86632e --- /dev/null +++ b/Speech/Ruby/app1/config.ru @@ -0,0 +1,2 @@ +require './speech.rb' +run Sinatra::Application \ No newline at end of file diff --git a/Speech/Ruby/app1/config.yml b/Speech/Ruby/app1/config.yml index 5143005..04d4593 100644 --- a/Speech/Ruby/app1/config.yml +++ b/Speech/Ruby/app1/config.yml @@ -5,7 +5,7 @@ # For more information contact developer.support@att.com port: 4567 -api_key: +api_key: secret_key: tokens_file: tokens FQDN: https://api.att.com diff --git a/Speech/Ruby/app1/speech.rb b/Speech/Ruby/app1/speech.rb index cf7864b..dddc30e 100644 --- a/Speech/Ruby/app1/speech.rb +++ b/Speech/Ruby/app1/speech.rb @@ -21,7 +21,15 @@ SCOPE = 'SPEECH' +error do + @error = e.message +end + ['/SpeechToText'].each do |path| + if settings.api_key.nil? || settings.secret_key.nil? + raise RuntimeError, "The API and Secret keys must be set the config.yml file" + end + before path do obtain_tokens(settings.FQDN, settings.api_key, settings.secret_key, SCOPE, settings.tokens_file) end @@ -32,32 +40,29 @@ end post '/SpeechToText' do -if params[:f1] != nil - speech_to_text -else -speech_default_file -end + if params[:f1] != nil + speech_to_text + else + speech_default_file + end end def speech_to_text - - @type = params[:f1][:type] - temp_file = params[:f1][:tempfile] + temp_file = params[:f1][:tempfile] - @file_contents = File.read(temp_file.path) + @file_contents = File.read(temp_file.path) - if @type == "application/octet-stream" - @type = "audio/amr" - end - - url = "#{settings.FQDN}/rest/1/SpeechToText" + if @type == "application/octet-stream" + @type = "audio/amr" + end - response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json' + url = "#{settings.FQDN}/rest/1/SpeechToText" - @result = JSON.parse response + response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json' + @result = JSON.parse response rescue => e @error = e.message ensure @@ -67,24 +72,21 @@ def speech_to_text def speech_default_file -@filename = 'bostonSeltics.wav' - -@type = ' audio/wav' + @filename = 'bostonSeltics.wav' + @type = 'audio/wav' -fullname = File.expand_path(File.dirname(File.dirname(__FILE__))) -final = fullname + '/' + @filename -@file_contents = File.read(final) + fullname = File.expand_path(File.dirname(File.dirname(__FILE__))) + final = fullname + '/app1/' + @filename + @file_contents = File.read(final) url = "#{settings.FQDN}/rest/1/SpeechToText" - response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json' - - @result = JSON.parse response + response = RestClient.post "#{settings.FQDN}/rest/1/SpeechToText", "#{@file_contents}", :Authorization => "Bearer #{@access_token}", :Content_Transfer_Encoding => 'chunked', :X_SpeechContext => 'Generic', :Content_Type => "#{@type}" , :Accept => 'application/json' + @result = JSON.parse response rescue => e @error = e.message ensure return erb:speech -end - +end \ No newline at end of file