Skip to content

Commit cb76900

Browse files
authored
Merge pull request #4 from watson-developer-cloud/post-generation-monkeypatch
Post generation monkey patching
2 parents e94eef5 + c8605d7 commit cb76900

21 files changed

+118
-82
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ p "Result: #{response.result}"
207207
This would give an output of `DetailedResponse` having the structure:
208208
```ruby
209209
Status: 200
210-
Headers: <http response headers>
211-
Result: <response returned by service>
210+
Headers: "<http response headers>"
211+
Result: "<response returned by service>"
212212
```
213213

214214
## Using Websockets

examples/speech_to_text_v1.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require("ibm_watson/speech_to_text_v1")
4-
require("ibm_watson/recognize_callback")
54

65
# If using IAM
76
speech_to_text = IBMWatson::SpeechToTextV1.new(

lib/ibm_watson/assistant_v1.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
require_relative "./watson_service"
2727

28+
# Module for the Watson APIs
2829
module IBMWatson
2930
##
3031
# The Assistant V1 service.

lib/ibm_watson/discovery_v1.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
require_relative "./watson_service"
2929

30+
# Module for the Watson APIs
3031
module IBMWatson
3132
##
3233
# The Discovery V1 service.

lib/ibm_watson/language_translator_v3.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
require_relative "./watson_service"
2929

30+
# Module for the Watson APIs
3031
module IBMWatson
3132
##
3233
# The Language Translator V3 service.

lib/ibm_watson/natural_language_classifier_v1.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
require_relative "./watson_service"
2828

29+
# Module for the Watson APIs
2930
module IBMWatson
3031
##
3132
# The Natural Language Classifier V1 service.

lib/ibm_watson/natural_language_understanding_v1.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
require_relative "./watson_service"
3333

34+
# Module for the Watson APIs
3435
module IBMWatson
3536
##
3637
# The Natural Language Understanding V1 service.

lib/ibm_watson/personality_insights_v3.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
require_relative "./watson_service"
4444

45+
# Module for the Watson APIs
4546
module IBMWatson
4647
##
4748
# The Personality Insights V3 service.

lib/ibm_watson/speech_to_text_v1.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
require_relative "./watson_service"
7676

77+
# Module for the Watson APIs
7778
module IBMWatson
7879
##
7980
# The Speech to Text V1 service.
@@ -408,6 +409,7 @@ def recognize(audio:, content_type:, model: nil, customization_id: nil, acoustic
408409
response
409410
end
410411

412+
##
411413
# @!method recognize_with_websocket(audio: nil,chunk_data: false,content_type: "audio/l16; rate=44100",model: "en-US_BroadbandModel",recognize_callback: nil,customization_id: nil,acoustic_customization_id: nil,customization_weight: nil,version: nil,inactivity_timeout: 30,interim_results: false,keywords: nil,keywords_threshold: nil,max_alternatives: 1,word_alternatives_threshold: nil,word_confidence: false,timestamps: false,profanity_filter: nil,smart_formatting: false,speaker_labels: nil)
412414
# Sends audio for speech recognition using web sockets.
413415
# @param audio [IO] Audio to transcribe in the format specified by the `Content-Type` header.
@@ -455,13 +457,16 @@ def recognize_with_websocket(
455457
)
456458
raise ArgumentError("Audio must be provided") if audio.nil? && !chunk_data
457459
raise ArgumentError("Recognize callback must be provided") if recognize_callback.nil?
458-
raise TypeError("Callback is not a derived class of RecognizeCallback") unless recognize_callback.is_a?(RecognizeCallback)
459-
460+
raise TypeError("Callback is not a derived class of RecognizeCallback") unless recognize_callback.is_a?(IBMWatson::RecognizeCallback)
460461
require_relative("./websocket/speech_to_text_websocket_listener.rb")
461-
462462
headers = {}
463463
headers = @watson_service.conn.default_options.headers.to_hash unless @watson_service.conn.default_options.headers.to_hash.empty?
464-
headers["Authorization"] = "Basic " + Base64.strict_encode64("#{@watson_service.username}:#{@watson_service.password}")
464+
if !@watson_service.token_manager.nil?
465+
access_token = @watson_service.token_manager._token
466+
headers["Authorization"] = "Bearer #{access_token}"
467+
elsif !@watson_service.username.nil? && !@watson_service.password.nil?
468+
headers["Authorization"] = "Basic " + Base64.strict_encode64("#{@watson_service.username}:#{@watson_service.password}")
469+
end
465470
url = @watson_service.url.gsub("https:", "wss:")
466471
params = {
467472
"model" => model,
@@ -472,7 +477,6 @@ def recognize_with_websocket(
472477
}
473478
params.delete_if { |_, v| v.nil? }
474479
url += "/v1/recognize?" + HTTP::URI.form_encode(params)
475-
476480
options = {
477481
"content_type" => content_type,
478482
"inactivity_timeout" => inactivity_timeout,
@@ -490,7 +494,6 @@ def recognize_with_websocket(
490494
options.delete_if { |_, v| v.nil? }
491495
WebSocketClient.new(audio: audio, chunk_data: chunk_data, options: options, recognize_callback: recognize_callback, url: url, headers: headers)
492496
end
493-
494497
#########################
495498
# Asynchronous
496499
#########################
@@ -1467,7 +1470,7 @@ def add_words(customization_id:, words:)
14671470
end
14681471

14691472
##
1470-
# @!method add_word(customization_id:, word_name:, word: nil, sounds_like: nil, display_as: nil)
1473+
# @!method add_word(customization_id:, word_name:, sounds_like: nil, display_as: nil)
14711474
# Add a custom word.
14721475
# Adds a custom word to a custom language model. The service populates the words
14731476
# resource for a custom model with out-of-vocabulary (OOV) words found in each
@@ -1509,11 +1512,6 @@ def add_words(customization_id:, words:)
15091512
# @param word_name [String] The custom word for the custom language model. When you add or update a custom
15101513
# word with the **Add a custom word** method, do not include spaces in the word. Use
15111514
# a `-` (dash) or `_` (underscore) to connect the tokens of compound words.
1512-
# @param word [String] For the **Add custom words** method, you must specify the custom word that is to
1513-
# be added to or updated in the custom model. Do not include spaces in the word. Use
1514-
# a `-` (dash) or `_` (underscore) to connect the tokens of compound words.
1515-
#
1516-
# Omit this field for the **Add a custom word** method.
15171515
# @param sounds_like [Array[String]] An array of sounds-like pronunciations for the custom word. Specify how words that
15181516
# are difficult to pronounce, foreign words, acronyms, and so on can be pronounced
15191517
# by users. For a word that is not in the service's base vocabulary, omit the
@@ -1528,13 +1526,13 @@ def add_words(customization_id:, words:)
15281526
# the parameter when you want the word to have a spelling that is different from its
15291527
# usual representation or from its spelling in corpora training data.
15301528
# @return [nil]
1531-
def add_word(customization_id:, word_name:, word: nil, sounds_like: nil, display_as: nil)
1529+
def add_word(customization_id:, word_name:, sounds_like: nil, display_as: nil)
15321530
raise ArgumentError("customization_id must be provided") if customization_id.nil?
15331531
raise ArgumentError("word_name must be provided") if word_name.nil?
15341532
headers = {
15351533
}
15361534
data = {
1537-
"word" => word,
1535+
"word" => word_name,
15381536
"sounds_like" => sounds_like,
15391537
"display_as" => display_as
15401538
}

lib/ibm_watson/text_to_speech_v1.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
require_relative "./watson_service"
7777

78+
# Module for the Watson APIs
7879
module IBMWatson
7980
##
8081
# The Text to Speech V1 service.

0 commit comments

Comments
 (0)