google-cloud-translate 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +6 -2
  3. data/CHANGELOG.md +33 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/LICENSE +1 -1
  6. data/OVERVIEW.md +223 -18
  7. data/lib/google-cloud-translate.rb +79 -91
  8. data/lib/google/cloud/translate.rb +127 -114
  9. data/lib/google/cloud/translate/v2.rb +169 -0
  10. data/lib/google/cloud/translate/v2/api.rb +255 -0
  11. data/lib/google/cloud/translate/v2/credentials.rb +58 -0
  12. data/lib/google/cloud/translate/v2/detection.rb +132 -0
  13. data/lib/google/cloud/translate/v2/language.rb +68 -0
  14. data/lib/google/cloud/translate/v2/service.rb +205 -0
  15. data/lib/google/cloud/translate/v2/translation.rb +120 -0
  16. data/lib/google/cloud/translate/v3.rb +144 -0
  17. data/lib/google/cloud/translate/v3/credentials.rb +42 -0
  18. data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +663 -0
  19. data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +51 -0
  20. data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +131 -0
  21. data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +113 -0
  22. data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +87 -0
  23. data/lib/google/cloud/translate/v3/translation_service_client.rb +927 -0
  24. data/lib/google/cloud/translate/v3/translation_service_client_config.json +66 -0
  25. data/lib/google/cloud/translate/v3/translation_service_pb.rb +226 -0
  26. data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +68 -0
  27. data/lib/google/cloud/translate/version.rb +1 -1
  28. metadata +47 -37
  29. data/lib/google/cloud/translate/api.rb +0 -274
  30. data/lib/google/cloud/translate/credentials.rb +0 -57
  31. data/lib/google/cloud/translate/detection.rb +0 -139
  32. data/lib/google/cloud/translate/language.rb +0 -70
  33. data/lib/google/cloud/translate/service.rb +0 -206
  34. data/lib/google/cloud/translate/translation.rb +0 -125
@@ -1,70 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Google
17
- module Cloud
18
- module Translate
19
- ##
20
- # # Language
21
- #
22
- # Represents a supported languages query result. Returned by
23
- # {Google::Cloud::Translate::Api#languages}.
24
- #
25
- # @see https://cloud.google.com/translation/docs/discovering-supported-languages
26
- # Discovering Supported Languages
27
- #
28
- # @example
29
- # require "google/cloud/translate"
30
- #
31
- # translate = Google::Cloud::Translate.new
32
- #
33
- # languages = translate.languages "en"
34
- #
35
- # languages.size #=> 104
36
- # languages[0].code #=> "af"
37
- # languages[0].name #=> "Afrikaans"
38
- #
39
- class Language
40
- ##
41
- # The language code. This is an [ISO
42
- # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
43
- # code.
44
- #
45
- # @return [String]
46
- attr_reader :code
47
-
48
- ##
49
- # The localized name of the language, if available.
50
- #
51
- # @return [String]
52
- attr_reader :name
53
-
54
- ##
55
- # @private Create a new object.
56
- def initialize code, name
57
- @code = code
58
- @name = name
59
- end
60
-
61
- ##
62
- # @private New Language from a LanguagesResource object as defined by
63
- # the Google API Client object.
64
- def self.from_gapi gapi
65
- new gapi["language"], gapi["name"]
66
- end
67
- end
68
- end
69
- end
70
- end
@@ -1,206 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "google/cloud/errors"
17
- require "google/cloud/translate/credentials"
18
- require "google/cloud/translate/version"
19
- require "faraday"
20
- require "uri"
21
-
22
- module Google
23
- module Cloud
24
- module Translate
25
- ##
26
- # @private
27
- # Represents the Translation API REST service, exposing the API calls.
28
- class Service #:nodoc:
29
- API_VERSION = "v2".freeze
30
- API_URL = "https://translation.googleapis.com".freeze
31
-
32
- # @private
33
- attr_accessor :project, :credentials, :retries, :timeout, :key
34
-
35
- ##
36
- # Creates a new Service instance.
37
- def initialize project, credentials, retries: nil, timeout: nil,
38
- key: nil, host: nil
39
- @project = project
40
- @credentials = credentials
41
- @retries = retries
42
- @timeout = timeout
43
- @key = key
44
- @host = host || API_URL
45
- end
46
-
47
- ##
48
- # Returns Hash of ListTranslationsResponse JSON
49
- def translate text, to: nil, from: nil, format: nil, model: nil,
50
- cid: nil
51
- body = {
52
- q: Array(text), target: to, source: from, format: format,
53
- model: model, cid: cid
54
- }.delete_if { |_k, v| v.nil? }.to_json
55
-
56
- post "/language/translate/v2", body
57
- end
58
-
59
- ##
60
- # Returns API::ListDetectionsResponse
61
- def detect text
62
- body = { q: Array(text) }.to_json
63
-
64
- post "language/translate/v2/detect", body
65
- end
66
-
67
- ##
68
- # Returns API::ListLanguagesResponse
69
- def languages language = nil
70
- body = { target: language }.to_json
71
-
72
- post "language/translate/v2/languages", body
73
- end
74
-
75
- def inspect
76
- self.class.to_s
77
- end
78
-
79
- protected
80
-
81
- def post path, body = nil
82
- response = execute do
83
- http.post path do |req|
84
- req.headers.merge! default_http_headers
85
- req.body = body unless body.nil?
86
-
87
- if @key
88
- req.params = { key: @key }
89
- else
90
- sign_http_request! req
91
- end
92
- end
93
- end
94
-
95
- return JSON.parse(response.body)["data"] if response.success?
96
-
97
- raise Google::Cloud::Error.gapi_error_class_for(response.status)
98
- rescue Faraday::ConnectionFailed
99
- raise Google::Cloud::ResourceExhaustedError
100
- end
101
-
102
- ##
103
- # The HTTP object that makes calls to API.
104
- # This must be a Faraday object.
105
- def http
106
- @http ||= Faraday.new url: @host, request: {
107
- open_timeout: @timeout, timeout: @timeout
108
- }.delete_if { |_k, v| v.nil? }
109
- end
110
-
111
- ##
112
- # The default HTTP headers to be sent on all API calls.
113
- def default_http_headers
114
- @default_http_headers ||= {
115
- "User-Agent" => "gcloud-ruby/#{Google::Cloud::Translate::VERSION}",
116
- "google-cloud-resource-prefix" => "projects/#{@project}",
117
- "Content-Type" => "application/json",
118
- "x-goog-api-client" => "gl-ruby/#{RUBY_VERSION} " \
119
- "gccl/#{Google::Cloud::Translate::VERSION}"
120
- }
121
- end
122
-
123
- ##
124
- # Make a request and apply incremental backoff
125
- def execute
126
- backoff = Backoff.new retries: retries
127
- backoff.execute do
128
- yield
129
- end
130
- rescue Faraday::ConnectionFailed
131
- raise Google::Cloud::ResourceExhaustedError
132
- end
133
-
134
- ##
135
- # Sign Oauth2 API calls.
136
- def sign_http_request! request
137
- client = credentials.client
138
- return if client.nil?
139
-
140
- client.fetch_access_token! if client.expires_within? 30
141
- client.generate_authenticated_request request: request
142
- request
143
- end
144
-
145
- ##
146
- # @private Backoff
147
- class Backoff
148
- class << self
149
- attr_accessor :retries
150
- attr_accessor :http_codes
151
- attr_accessor :reasons
152
- attr_accessor :backoff # :nodoc:
153
- end
154
-
155
- # Set the default values
156
- self.retries = 3
157
- self.http_codes = [500, 503]
158
- self.reasons = ["rateLimitExceeded", "userRateLimitExceeded"]
159
- self.backoff = ->(retries) { sleep retries.to_i }
160
-
161
- def initialize options = {} #:nodoc:
162
- @max_retries = (options[:retries] || Backoff.retries).to_i
163
- @http_codes = (options[:http_codes] || Backoff.http_codes).to_a
164
- @reasons = (options[:reasons] || Backoff.reasons).to_a
165
- @backoff = options[:backoff] || Backoff.backoff
166
- end
167
-
168
- def execute #:nodoc:
169
- current_retries = 0
170
- loop do
171
- response = yield # Expecting Faraday::Response
172
- return response if response.success?
173
- break response unless retry? response, current_retries
174
- current_retries += 1
175
- @backoff.call current_retries
176
- end
177
- end
178
-
179
- protected
180
-
181
- def retry? result, current_retries #:nodoc:
182
- if current_retries < @max_retries
183
- return true if retry_http_code? result
184
- return true if retry_error_reason? result
185
- end
186
- false
187
- end
188
-
189
- def retry_http_code? response #:nodoc:
190
- @http_codes.include? response.status
191
- end
192
-
193
- def retry_error_reason? response #:nodoc:
194
- result = JSON.parse response.body
195
- if result && result["error"] && result["error"]["errors"]
196
- Array(result["error"]["errors"]).each do |error|
197
- return true if error["reason"] && @reasons.include?(error["reason"])
198
- end
199
- end
200
- false
201
- end
202
- end
203
- end
204
- end
205
- end
206
- end
@@ -1,125 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Google
17
- module Cloud
18
- module Translate
19
- ##
20
- # # Translation
21
- #
22
- # Represents a translation query result. Returned by
23
- # {Google::Cloud::Translate::Api#translate}.
24
- #
25
- # @see https://cloud.google.com/translation/docs/translating-text#Translate
26
- # Translating Text
27
- #
28
- # @example
29
- # require "google/cloud/translate"
30
- #
31
- # translate = Google::Cloud::Translate.new
32
- #
33
- # translation = translate.translate "Hello world!", to: "la"
34
- #
35
- # translation.to_s #=> "Salve mundi!"
36
- #
37
- # translation.from #=> "en"
38
- # translation.origin #=> "Hello world!"
39
- # translation.to #=> "la"
40
- # translation.text #=> "Salve mundi!"
41
- #
42
- class Translation
43
- ##
44
- # The translated result.
45
- #
46
- # @return [String]
47
- attr_reader :text
48
- alias to_s text
49
- alias to_str text
50
-
51
- ##
52
- # The original query text that was translated.
53
- #
54
- # @return [String]
55
- attr_reader :origin
56
-
57
- ##
58
- # The target language into which the text was translated.
59
- #
60
- # @return [String]
61
- attr_reader :to
62
- alias language to
63
- alias target to
64
-
65
- ##
66
- # The source language from which the text was translated.
67
- #
68
- # @return [String]
69
- attr_reader :from
70
- alias source from
71
-
72
- ##
73
- # The translation model. Can be either `base` for the Phrase-Based
74
- # Machine Translation (PBMT) model, or `nmt` for the Neural Machine
75
- # Translation (NMT) model. If you did not include a model parameter with
76
- # your request, then this field is not included in the response.
77
- #
78
- # @return [String]
79
- attr_reader :model
80
-
81
- ##
82
- # @private Create a new object.
83
- def initialize text, to, origin, from, model, detected
84
- @text = text
85
- @to = to
86
- @origin = origin
87
- @from = from
88
- @model = model
89
- @detected = detected
90
- end
91
-
92
- ##
93
- # Determines if the source language was detected by the Google Cloud
94
- # Cloud Translation API.
95
- #
96
- # @return [Boolean] `true` if the source language was detected by the
97
- # Cloud Translation API, `false` if the source language was provided
98
- # in the request
99
- def detected?
100
- @detected
101
- end
102
-
103
- ##
104
- # @private New Translation from a TranslationsListResponse object as
105
- # defined by the Google API Client object.
106
- def self.from_gapi_list gapi, text, to, from
107
- res = text.zip(Array(gapi["translations"])).map do |origin, g|
108
- from_gapi g, to, origin, from
109
- end
110
- return res.first if res.size == 1
111
- res
112
- end
113
-
114
- ##
115
- # @private New Translation from a TranslationsResource object as defined
116
- # by the Google API Client object.
117
- def self.from_gapi gapi, to, origin, from
118
- from ||= gapi["detectedSourceLanguage"]
119
- detected = !gapi["detectedSourceLanguage"].nil?
120
- new gapi["translatedText"], to, origin, from, gapi["model"], detected
121
- end
122
- end
123
- end
124
- end
125
- end