diff --git a/translate/Gemfile.lock b/translate/Gemfile.lock index 6390d0338..98507ea57 100644 --- a/translate/Gemfile.lock +++ b/translate/Gemfile.lock @@ -1,23 +1,15 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.4.0) + addressable (2.5.0) + public_suffix (~> 2.0, >= 2.0.2) diff-lcs (1.2.5) - faraday (0.9.2) + faraday (0.10.0) multipart-post (>= 1.2, < 3) - google-api-client (0.9.15) - addressable (~> 2.3) - googleauth (~> 0.5) - httpclient (~> 2.7) - hurley (~> 0.1) - memoist (~> 0.11) - mime-types (>= 1.6) - representable (~> 2.3.0) - retriable (~> 2.0) - google-cloud-core (0.20.1) - google-cloud-translate (0.20.1) - google-api-client (~> 0.9.11) - google-cloud-core (~> 0.20.0) + google-cloud-core (0.21.0) + google-cloud-translate (0.22.1) + google-cloud-core (~> 0.21.0) + googleauth (~> 0.5.1) googleauth (0.5.1) faraday (~> 0.9) jwt (~> 1.4) @@ -26,28 +18,21 @@ GEM multi_json (~> 1.11) os (~> 0.9) signet (~> 0.7) - httpclient (2.8.2.4) - hurley (0.2) jwt (1.5.6) little-plugger (1.1.4) logging (2.1.0) little-plugger (~> 1.1) multi_json (~> 1.10) memoist (0.15.0) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) multi_json (1.12.1) multipart-post (2.0.0) os (0.9.6) - representable (2.3.0) - uber (~> 0.0.7) - retriable (2.1.0) + public_suffix (2.0.4) rspec (3.5.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) rspec-mocks (~> 3.5.0) - rspec-core (3.5.3) + rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) @@ -61,7 +46,6 @@ GEM faraday (~> 0.9) jwt (~> 1.5) multi_json (~> 1.10) - uber (0.0.15) PLATFORMS ruby @@ -71,4 +55,4 @@ DEPENDENCIES rspec BUNDLED WITH - 1.12.5 + 1.13.6 diff --git a/translate/quickstart.rb b/translate/quickstart.rb index 8827fe3f6..b3978a478 100644 --- a/translate/quickstart.rb +++ b/translate/quickstart.rb @@ -16,12 +16,12 @@ # Imports the Google Cloud client library require "google/cloud" -# Your Translate API key -api_key = "YOUR_API_KEY" +# Your Google Cloud Platform project ID +project_id = "YOUR_PROJECT_ID" # Instantiates a client -gcloud = Google::Cloud.new -translate = gcloud.translate api_key +gcloud = Google::Cloud.new project_id +translate = gcloud.translate # The text to translate text = "Hello, world!" diff --git a/translate/spec/quickstart_spec.rb b/translate/spec/quickstart_spec.rb index 0d119d892..232ed1c96 100644 --- a/translate/spec/quickstart_spec.rb +++ b/translate/spec/quickstart_spec.rb @@ -18,11 +18,9 @@ describe "Translate Quickstart" do it "translates Hello, world! to Russian" do - gcloud = Google::Cloud.new - translate = gcloud.translate ENV["TRANSLATE_API_KEY"] - expect(Google::Cloud).to receive(:new).and_return(gcloud) - expect(gcloud).to receive(:translate).with("YOUR_API_KEY"). - and_return(translate) + gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"] + expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID"). + and_return(gcloud) expect { load File.expand_path("../quickstart.rb", __dir__) diff --git a/translate/spec/translate_samples_spec.rb b/translate/spec/translate_samples_spec.rb index cc80b6cb1..ba8a13358 100644 --- a/translate/spec/translate_samples_spec.rb +++ b/translate/spec/translate_samples_spec.rb @@ -18,7 +18,7 @@ describe "Google Translate API samples" do before do - @api_key = ENV["TRANSLATE_API_KEY"] + @project_id = ENV["GOOGLE_CLOUD_PROJECT"] end # Capture and return STDOUT output by block @@ -34,7 +34,7 @@ def capture &block example "translate text" do capture do - translate_text api_key: @api_key, + translate_text project_id: @project_id, language_code: "fr", text: "Alice and Bob are kind" end @@ -47,14 +47,14 @@ def capture &block example "detect language" do expect { - detect_language api_key: @api_key, text: "Sample text written in English" + detect_language project_id: @project_id, text: "Sample text written in English" }.to output( /'Sample text written in English' detected as language: en/ ).to_stdout end example "list supported language codes" do - capture { list_supported_language_codes api_key: @api_key } + capture { list_supported_language_codes project_id: @project_id } # Check for a few supported language codes (first sorted alphabetically) expect(captured_output).to include "af" @@ -66,7 +66,7 @@ def capture &block example "list supported language names" do capture do - list_supported_language_names api_key: @api_key, language_code: "en" + list_supported_language_names project_id: @project_id, language_code: "en" end # Check for a few supported language codes (first sorted alphabetically) diff --git a/translate/translate_samples.rb b/translate/translate_samples.rb index ad0f63655..c01f7ceba 100644 --- a/translate/translate_samples.rb +++ b/translate/translate_samples.rb @@ -12,16 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -def translate_text api_key:, text:, language_code: +def translate_text project_id:, text:, language_code: # [START translate_text] - # api_key = "Your API access key" + # project_id = "Your Google Cloud project ID" # text = "The text you would like to translate" # language_code = "The ISO 639-1 code of language to translate to, eg. 'en'" require "google/cloud" - gcloud = Google::Cloud.new - translate = gcloud.translate api_key + gcloud = Google::Cloud.new project_id + translate = gcloud.translate translation = translate.translate text, to: language_code puts "Translated '#{text}' to '#{translation.text.inspect}'" @@ -29,15 +29,15 @@ def translate_text api_key:, text:, language_code: # [END translate_text] end -def detect_language api_key:, text: +def detect_language project_id:, text: # [START detect_language] - # api_key = "Your API access key" + # project_id = "Your Google Cloud project ID" # text = "The text you would like to detect the language of" require "google/cloud" - gcloud = Google::Cloud.new - translate = gcloud.translate api_key + gcloud = Google::Cloud.new project_id + translate = gcloud.translate detection = translate.detect text puts "'#{text}' detected as language: #{detection.language}" @@ -45,14 +45,14 @@ def detect_language api_key:, text: # [END detect_language] end -def list_supported_language_codes api_key: +def list_supported_language_codes project_id: # [START list_supported_language_codes] - # api_key = "Your API access key" + # project_id = "Your Google Cloud project ID" require "google/cloud" - gcloud = Google::Cloud.new - translate = gcloud.translate api_key + gcloud = Google::Cloud.new project_id + translate = gcloud.translate languages = translate.languages puts "Supported language codes:" @@ -62,9 +62,9 @@ def list_supported_language_codes api_key: # [END list_supported_language_codes] end -def list_supported_language_names api_key:, language_code: "en" +def list_supported_language_names project_id:, language_code: "en" # [START list_supported_language_names] - # api_key = "Your API access key" + # project_id = "Your Google Cloud project ID" # To receive the names of the supported languages, provide the code # for the language in which you wish to receive the names @@ -72,8 +72,8 @@ def list_supported_language_names api_key:, language_code: "en" require "google/cloud" - gcloud = Google::Cloud.new - translate = gcloud.translate api_key + gcloud = Google::Cloud.new project_id + translate = gcloud.translate languages = translate.languages language_code puts "Supported languages:" @@ -84,20 +84,22 @@ def list_supported_language_names api_key:, language_code: "en" end if __FILE__ == $PROGRAM_NAME - api_key = ENV["TRANSLATE_API_KEY"] + project_id = ENV["GOOGLE_CLOUD_PROJECT"] command = ARGV.shift case command when "translate" - translate_text api_key: api_key, + translate_text project_id: project_id, language_code: ARGV.shift, text: ARGV.shift when "detect_language" - detect_language api_key: api_key, text: ARGV.shift + detect_language project_id: project_id, + text: ARGV.shift when "list_codes" - list_supported_language_codes api_key: api_key + list_supported_language_codes project_id: project_id when "list_names" - list_supported_language_names api_key: api_key, language_code: ARGV.shift + list_supported_language_names project_id: project_id, + language_code: ARGV.shift else puts <<-usage Usage: ruby translate_samples.rb [arguments]