From 2a3e6d1b85f6bc92ffcf35472c9825df1152be95 Mon Sep 17 00:00:00 2001 From: Jason Goecke Date: Tue, 14 Aug 2012 08:18:37 -0700 Subject: [PATCH 1/5] Added a Gemfile to use Bundler to install the appropriate dependencies --- Speech/Ruby/app1/Gemfile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Speech/Ruby/app1/Gemfile 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 From 598b56d16b82ac9639e5ca995e2c737ec8536b02 Mon Sep 17 00:00:00 2001 From: Jason Goecke Date: Tue, 14 Aug 2012 08:26:52 -0700 Subject: [PATCH 2/5] Added a config.ru in order to be able to run rackup --- Speech/Ruby/app1/config.ru | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Speech/Ruby/app1/config.ru 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 From c6199918d7bee17f08f32abd59f2eaa14de8ba76 Mon Sep 17 00:00:00 2001 From: Jason Goecke Date: Tue, 14 Aug 2012 08:55:25 -0700 Subject: [PATCH 3/5] Fixed indentation and spacing to make the code more readable --- Speech/Ruby/app1/speech.rb | 48 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/Speech/Ruby/app1/speech.rb b/Speech/Ruby/app1/speech.rb index cf7864b..35ffd48 100644 --- a/Speech/Ruby/app1/speech.rb +++ b/Speech/Ruby/app1/speech.rb @@ -32,32 +32,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] - - @file_contents = File.read(temp_file.path) + temp_file = params[:f1][:tempfile] - if @type == "application/octet-stream" - @type = "audio/amr" - end + @file_contents = File.read(temp_file.path) - 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 +64,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 + '/' + @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 From 23865e0b1e498afa76bbd6b722cabae46c717a62 Mon Sep 17 00:00:00 2001 From: Jason Goecke Date: Tue, 14 Aug 2012 09:01:40 -0700 Subject: [PATCH 4/5] Added error handling for when keys are not configured in the YML --- Speech/Ruby/app1/config.yml | 2 +- Speech/Ruby/app1/speech.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 35ffd48..e80ce05 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 From 8050a2e8f6ce47412f5662383b230253934831b5 Mon Sep 17 00:00:00 2001 From: Jason Goecke Date: Tue, 14 Aug 2012 09:17:07 -0700 Subject: [PATCH 5/5] Added the correct path so the default file scenario works. Fixes #2 --- Speech/Ruby/app1/speech.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Speech/Ruby/app1/speech.rb b/Speech/Ruby/app1/speech.rb index e80ce05..dddc30e 100644 --- a/Speech/Ruby/app1/speech.rb +++ b/Speech/Ruby/app1/speech.rb @@ -73,11 +73,11 @@ def speech_to_text def speech_default_file @filename = 'bostonSeltics.wav' - @type = ' audio/wav' + @type = 'audio/wav' fullname = File.expand_path(File.dirname(File.dirname(__FILE__))) - final = fullname + '/' + @filename + final = fullname + '/app1/' + @filename @file_contents = File.read(final) url = "#{settings.FQDN}/rest/1/SpeechToText"