Skip to content

Commit 18eb00c

Browse files
author
remi Taylor
authored
Merge pull request #113 from GoogleCloudPlatform/translate-api-updates
Translate API no longer requires an API key.
2 parents 907bd9f + 12a613b commit 18eb00c

File tree

5 files changed

+45
-61
lines changed

5 files changed

+45
-61
lines changed

translate/Gemfile.lock

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
addressable (2.4.0)
4+
addressable (2.5.0)
5+
public_suffix (~> 2.0, >= 2.0.2)
56
diff-lcs (1.2.5)
6-
faraday (0.9.2)
7+
faraday (0.10.0)
78
multipart-post (>= 1.2, < 3)
8-
google-api-client (0.9.15)
9-
addressable (~> 2.3)
10-
googleauth (~> 0.5)
11-
httpclient (~> 2.7)
12-
hurley (~> 0.1)
13-
memoist (~> 0.11)
14-
mime-types (>= 1.6)
15-
representable (~> 2.3.0)
16-
retriable (~> 2.0)
17-
google-cloud-core (0.20.1)
18-
google-cloud-translate (0.20.1)
19-
google-api-client (~> 0.9.11)
20-
google-cloud-core (~> 0.20.0)
9+
google-cloud-core (0.21.0)
10+
google-cloud-translate (0.22.1)
11+
google-cloud-core (~> 0.21.0)
12+
googleauth (~> 0.5.1)
2113
googleauth (0.5.1)
2214
faraday (~> 0.9)
2315
jwt (~> 1.4)
@@ -26,28 +18,21 @@ GEM
2618
multi_json (~> 1.11)
2719
os (~> 0.9)
2820
signet (~> 0.7)
29-
httpclient (2.8.2.4)
30-
hurley (0.2)
3121
jwt (1.5.6)
3222
little-plugger (1.1.4)
3323
logging (2.1.0)
3424
little-plugger (~> 1.1)
3525
multi_json (~> 1.10)
3626
memoist (0.15.0)
37-
mime-types (3.1)
38-
mime-types-data (~> 3.2015)
39-
mime-types-data (3.2016.0521)
4027
multi_json (1.12.1)
4128
multipart-post (2.0.0)
4229
os (0.9.6)
43-
representable (2.3.0)
44-
uber (~> 0.0.7)
45-
retriable (2.1.0)
30+
public_suffix (2.0.4)
4631
rspec (3.5.0)
4732
rspec-core (~> 3.5.0)
4833
rspec-expectations (~> 3.5.0)
4934
rspec-mocks (~> 3.5.0)
50-
rspec-core (3.5.3)
35+
rspec-core (3.5.4)
5136
rspec-support (~> 3.5.0)
5237
rspec-expectations (3.5.0)
5338
diff-lcs (>= 1.2.0, < 2.0)
@@ -61,7 +46,6 @@ GEM
6146
faraday (~> 0.9)
6247
jwt (~> 1.5)
6348
multi_json (~> 1.10)
64-
uber (0.0.15)
6549

6650
PLATFORMS
6751
ruby
@@ -71,4 +55,4 @@ DEPENDENCIES
7155
rspec
7256

7357
BUNDLED WITH
74-
1.12.5
58+
1.13.6

translate/quickstart.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
# Imports the Google Cloud client library
1717
require "google/cloud"
1818

19-
# Your Translate API key
20-
api_key = "YOUR_API_KEY"
19+
# Your Google Cloud Platform project ID
20+
project_id = "YOUR_PROJECT_ID"
2121

2222
# Instantiates a client
23-
gcloud = Google::Cloud.new
24-
translate = gcloud.translate api_key
23+
gcloud = Google::Cloud.new project_id
24+
translate = gcloud.translate
2525

2626
# The text to translate
2727
text = "Hello, world!"

translate/spec/quickstart_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
describe "Translate Quickstart" do
1919

2020
it "translates Hello, world! to Russian" do
21-
gcloud = Google::Cloud.new
22-
translate = gcloud.translate ENV["TRANSLATE_API_KEY"]
23-
expect(Google::Cloud).to receive(:new).and_return(gcloud)
24-
expect(gcloud).to receive(:translate).with("YOUR_API_KEY").
25-
and_return(translate)
21+
gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
22+
expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").
23+
and_return(gcloud)
2624

2725
expect {
2826
load File.expand_path("../quickstart.rb", __dir__)

translate/spec/translate_samples_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
describe "Google Translate API samples" do
1919

2020
before do
21-
@api_key = ENV["TRANSLATE_API_KEY"]
21+
@project_id = ENV["GOOGLE_CLOUD_PROJECT"]
2222
end
2323

2424
# Capture and return STDOUT output by block
@@ -34,7 +34,7 @@ def capture &block
3434

3535
example "translate text" do
3636
capture do
37-
translate_text api_key: @api_key,
37+
translate_text project_id: @project_id,
3838
language_code: "fr",
3939
text: "Alice and Bob are kind"
4040
end
@@ -47,14 +47,14 @@ def capture &block
4747

4848
example "detect language" do
4949
expect {
50-
detect_language api_key: @api_key, text: "Sample text written in English"
50+
detect_language project_id: @project_id, text: "Sample text written in English"
5151
}.to output(
5252
/'Sample text written in English' detected as language: en/
5353
).to_stdout
5454
end
5555

5656
example "list supported language codes" do
57-
capture { list_supported_language_codes api_key: @api_key }
57+
capture { list_supported_language_codes project_id: @project_id }
5858

5959
# Check for a few supported language codes (first sorted alphabetically)
6060
expect(captured_output).to include "af"
@@ -66,7 +66,7 @@ def capture &block
6666

6767
example "list supported language names" do
6868
capture do
69-
list_supported_language_names api_key: @api_key, language_code: "en"
69+
list_supported_language_names project_id: @project_id, language_code: "en"
7070
end
7171

7272
# Check for a few supported language codes (first sorted alphabetically)

translate/translate_samples.rb

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,47 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
def translate_text api_key:, text:, language_code:
15+
def translate_text project_id:, text:, language_code:
1616
# [START translate_text]
17-
# api_key = "Your API access key"
17+
# project_id = "Your Google Cloud project ID"
1818
# text = "The text you would like to translate"
1919
# language_code = "The ISO 639-1 code of language to translate to, eg. 'en'"
2020

2121
require "google/cloud"
2222

23-
gcloud = Google::Cloud.new
24-
translate = gcloud.translate api_key
23+
gcloud = Google::Cloud.new project_id
24+
translate = gcloud.translate
2525
translation = translate.translate text, to: language_code
2626

2727
puts "Translated '#{text}' to '#{translation.text.inspect}'"
2828
puts "Original language: #{translation.from} translated to: #{translation.to}"
2929
# [END translate_text]
3030
end
3131

32-
def detect_language api_key:, text:
32+
def detect_language project_id:, text:
3333
# [START detect_language]
34-
# api_key = "Your API access key"
34+
# project_id = "Your Google Cloud project ID"
3535
# text = "The text you would like to detect the language of"
3636

3737
require "google/cloud"
3838

39-
gcloud = Google::Cloud.new
40-
translate = gcloud.translate api_key
39+
gcloud = Google::Cloud.new project_id
40+
translate = gcloud.translate
4141
detection = translate.detect text
4242

4343
puts "'#{text}' detected as language: #{detection.language}"
4444
puts "Confidence: #{detection.confidence}"
4545
# [END detect_language]
4646
end
4747

48-
def list_supported_language_codes api_key:
48+
def list_supported_language_codes project_id:
4949
# [START list_supported_language_codes]
50-
# api_key = "Your API access key"
50+
# project_id = "Your Google Cloud project ID"
5151

5252
require "google/cloud"
5353

54-
gcloud = Google::Cloud.new
55-
translate = gcloud.translate api_key
54+
gcloud = Google::Cloud.new project_id
55+
translate = gcloud.translate
5656
languages = translate.languages
5757

5858
puts "Supported language codes:"
@@ -62,18 +62,18 @@ def list_supported_language_codes api_key:
6262
# [END list_supported_language_codes]
6363
end
6464

65-
def list_supported_language_names api_key:, language_code: "en"
65+
def list_supported_language_names project_id:, language_code: "en"
6666
# [START list_supported_language_names]
67-
# api_key = "Your API access key"
67+
# project_id = "Your Google Cloud project ID"
6868

6969
# To receive the names of the supported languages, provide the code
7070
# for the language in which you wish to receive the names
7171
# language_code = "en"
7272

7373
require "google/cloud"
7474

75-
gcloud = Google::Cloud.new
76-
translate = gcloud.translate api_key
75+
gcloud = Google::Cloud.new project_id
76+
translate = gcloud.translate
7777
languages = translate.languages language_code
7878

7979
puts "Supported languages:"
@@ -84,20 +84,22 @@ def list_supported_language_names api_key:, language_code: "en"
8484
end
8585

8686
if __FILE__ == $PROGRAM_NAME
87-
api_key = ENV["TRANSLATE_API_KEY"]
87+
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
8888
command = ARGV.shift
8989

9090
case command
9191
when "translate"
92-
translate_text api_key: api_key,
92+
translate_text project_id: project_id,
9393
language_code: ARGV.shift,
9494
text: ARGV.shift
9595
when "detect_language"
96-
detect_language api_key: api_key, text: ARGV.shift
96+
detect_language project_id: project_id,
97+
text: ARGV.shift
9798
when "list_codes"
98-
list_supported_language_codes api_key: api_key
99+
list_supported_language_codes project_id: project_id
99100
when "list_names"
100-
list_supported_language_names api_key: api_key, language_code: ARGV.shift
101+
list_supported_language_names project_id: project_id,
102+
language_code: ARGV.shift
101103
else
102104
puts <<-usage
103105
Usage: ruby translate_samples.rb <command> [arguments]

0 commit comments

Comments
 (0)