google-cloud-translate 2.3.0 → 3.1.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 (35) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -8
  3. data/AUTHENTICATION.md +67 -81
  4. data/LICENSE.md +201 -0
  5. data/MIGRATING.md +302 -0
  6. data/README.md +139 -0
  7. data/lib/google-cloud-translate.rb +5 -146
  8. data/lib/google/cloud/translate.rb +79 -178
  9. data/lib/google/cloud/translate/helpers.rb +107 -0
  10. data/lib/google/cloud/translate/version.rb +6 -2
  11. metadata +38 -138
  12. data/CHANGELOG.md +0 -164
  13. data/CODE_OF_CONDUCT.md +0 -40
  14. data/CONTRIBUTING.md +0 -188
  15. data/LICENSE +0 -201
  16. data/OVERVIEW.md +0 -390
  17. data/TROUBLESHOOTING.md +0 -37
  18. data/lib/google/cloud/translate/v2.rb +0 -169
  19. data/lib/google/cloud/translate/v2/api.rb +0 -255
  20. data/lib/google/cloud/translate/v2/credentials.rb +0 -58
  21. data/lib/google/cloud/translate/v2/detection.rb +0 -132
  22. data/lib/google/cloud/translate/v2/language.rb +0 -68
  23. data/lib/google/cloud/translate/v2/service.rb +0 -209
  24. data/lib/google/cloud/translate/v2/translation.rb +0 -120
  25. data/lib/google/cloud/translate/v3.rb +0 -144
  26. data/lib/google/cloud/translate/v3/credentials.rb +0 -42
  27. data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +0 -663
  28. data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +0 -51
  29. data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +0 -131
  30. data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +0 -113
  31. data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +0 -39
  32. data/lib/google/cloud/translate/v3/translation_service_client.rb +0 -930
  33. data/lib/google/cloud/translate/v3/translation_service_client_config.json +0 -66
  34. data/lib/google/cloud/translate/v3/translation_service_pb.rb +0 -226
  35. data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +0 -68
@@ -1,58 +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 "googleauth"
17
-
18
- module Google
19
- module Cloud
20
- module Translate
21
- module V2
22
- ##
23
- # # Credentials
24
- #
25
- # Represents the authentication and authorization used to connect to the Cloud Translation API.
26
- #
27
- # @example
28
- # require "google/cloud/translate"
29
- #
30
- # keyfile = "/path/to/keyfile.json"
31
- # creds = Google::Cloud::Translate::V2::Credentials.new keyfile
32
- #
33
- # translate = Google::Cloud::Translate.new(
34
- # version: :v2,
35
- # project_id: "my-todo-project",
36
- # credentials: creds
37
- # )
38
- #
39
- # translate.project_id #=> "my-todo-project"
40
- #
41
- class Credentials < Google::Auth::Credentials
42
- SCOPE = ["https://www.googleapis.com/auth/cloud-platform"].freeze
43
- PATH_ENV_VARS = ["TRANSLATE_CREDENTIALS",
44
- "TRANSLATE_KEYFILE",
45
- "GOOGLE_CLOUD_CREDENTIALS",
46
- "GOOGLE_CLOUD_KEYFILE",
47
- "GCLOUD_KEYFILE"].freeze
48
- JSON_ENV_VARS = ["TRANSLATE_CREDENTIALS_JSON",
49
- "TRANSLATE_KEYFILE_JSON",
50
- "GOOGLE_CLOUD_CREDENTIALS_JSON",
51
- "GOOGLE_CLOUD_KEYFILE_JSON",
52
- "GCLOUD_KEYFILE_JSON"].freeze
53
- DEFAULT_PATHS = ["~/.config/gcloud/application_default_credentials.json"].freeze
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,132 +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
- module V2
20
- ##
21
- # # Detection
22
- #
23
- # Represents a detect language query result. Returned by {Google::Cloud::Translate::V2::Api#detect}.
24
- #
25
- # @see https://cloud.google.com/translation/docs/detecting-language Detecting Language
26
- #
27
- # @example
28
- # require "google/cloud/translate"
29
- #
30
- # translate = Google::Cloud::Translate.new version: :v2
31
- #
32
- # detections = translate.detect "chien", "chat"
33
- #
34
- # detections.size #=> 2
35
- # detections[0].text #=> "chien"
36
- # detections[0].language #=> "fr"
37
- # detections[0].confidence #=> 0.7109375
38
- # detections[1].text #=> "chat"
39
- # detections[1].language #=> "en"
40
- # detections[1].confidence #=> 0.59922177
41
- #
42
- class Detection
43
- ##
44
- # The text upon which the language detection was performed.
45
- #
46
- # @return [String]
47
- attr_reader :text
48
-
49
- ##
50
- # The list of detection results for the given text. The most likely language is listed first, and its
51
- # attributes can be accessed through {#language} and {#confidence}.
52
- #
53
- # @return [Array<Detection::Result>]
54
- attr_reader :results
55
-
56
- ##
57
- # @private Create a new object.
58
- def initialize text, results
59
- @text = text
60
- @results = results
61
- end
62
-
63
- ##
64
- # The confidence that the language detection result is correct. The closer this value is to 1, the higher the
65
- # confidence in language detection.
66
- #
67
- # @return [Float] a value between 0 and 1
68
- def confidence
69
- return nil if results.empty?
70
- results.first.confidence
71
- end
72
-
73
- ##
74
- # The most likely language that was detected. This is an [ISO
75
- # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.
76
- #
77
- # @return [String] the language code
78
- def language
79
- return nil if results.empty?
80
- results.first.language
81
- end
82
-
83
- ##
84
- # @private New Detection from a ListDetectionsResponse object as defined by the Google API Client object.
85
- def self.from_gapi gapi, text
86
- res = text.zip(Array(gapi["detections"])).map do |txt, detections|
87
- results = detections.map { |g| Result.from_gapi g }
88
- new txt, results
89
- end
90
- return res.first if res.size == 1
91
- res
92
- end
93
-
94
- ##
95
- # # Result
96
- #
97
- # Represents an individual result in a {Google::Cloud::Translate::V2::Detection} result.
98
- #
99
- class Result
100
- ##
101
- # The confidence that the language detection result is correct. The closer this value is to 1, the higher
102
- # the confidence in language detection.
103
- #
104
- # @return [Float] a value between 0 and 1
105
- attr_reader :confidence
106
-
107
- ##
108
- # The language detected. This is an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
109
- # language code.
110
- #
111
- # @return [String] the language code
112
- attr_reader :language
113
-
114
- ##
115
- # @private Create a new object.
116
- def initialize confidence, language
117
- @confidence = confidence
118
- @language = language
119
- end
120
-
121
- ##
122
- # @private New Detection::Result from a DetectionsResource object as defined by the Google API Client
123
- # object.
124
- def self.from_gapi gapi
125
- new gapi["confidence"], gapi["language"]
126
- end
127
- end
128
- end
129
- end
130
- end
131
- end
132
- end
@@ -1,68 +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
- module V2
20
- ##
21
- # # Language
22
- #
23
- # Represents a supported languages query result. Returned by {Google::Cloud::Translate::V2::Api#languages}.
24
- #
25
- # @see https://cloud.google.com/translation/docs/discovering-supported-languages Discovering Supported Languages
26
- #
27
- # @example
28
- # require "google/cloud/translate"
29
- #
30
- # translate = Google::Cloud::Translate.new version: :v2
31
- #
32
- # languages = translate.languages "en"
33
- #
34
- # languages.size #=> 104
35
- # languages[0].code #=> "af"
36
- # languages[0].name #=> "Afrikaans"
37
- #
38
- class Language
39
- ##
40
- # The language code. This is an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
41
- # code.
42
- #
43
- # @return [String]
44
- attr_reader :code
45
-
46
- ##
47
- # The localized name of the language, if available.
48
- #
49
- # @return [String]
50
- attr_reader :name
51
-
52
- ##
53
- # @private Create a new object.
54
- def initialize code, name
55
- @code = code
56
- @name = name
57
- end
58
-
59
- ##
60
- # @private New Language from a LanguagesResource object as defined by the Google API Client object.
61
- def self.from_gapi gapi
62
- new gapi["language"], gapi["name"]
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,209 +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/v2/credentials"
18
- require "google/cloud/translate/version"
19
- require "faraday"
20
- require "uri"
21
-
22
- module Google
23
- module Cloud
24
- module Translate
25
- module V2
26
- ##
27
- # @private
28
- # Represents the Translation API REST service, exposing the API calls.
29
- class Service #:nodoc:
30
- API_VERSION = "v2".freeze
31
- API_URL = "https://translation.googleapis.com".freeze
32
-
33
- # @private
34
- attr_accessor :project_id, :credentials, :retries, :timeout, :key
35
-
36
- ##
37
- # Creates a new Service instance.
38
- def initialize project_id, credentials, retries: nil, timeout: nil, key: nil, host: nil
39
- @project_id = project_id
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, cid: nil
50
- body = {
51
- q: Array(text), target: to, source: from, format: format,
52
- model: model, cid: cid
53
- }.delete_if { |_k, v| v.nil? }.to_json
54
-
55
- post "/language/translate/v2", body
56
- end
57
-
58
- ##
59
- # Returns API::ListDetectionsResponse
60
- def detect text
61
- body = { q: Array(text) }.to_json
62
-
63
- post "language/translate/v2/detect", body
64
- end
65
-
66
- ##
67
- # Returns API::ListLanguagesResponse
68
- def languages language = nil
69
- body = { target: language }.to_json
70
-
71
- post "language/translate/v2/languages", body
72
- end
73
-
74
- def inspect
75
- self.class.to_s
76
- end
77
-
78
- protected
79
-
80
- def post path, body = nil
81
- response = execute do
82
- http.post path do |req|
83
- req.headers.merge! default_http_headers
84
- req.body = body unless body.nil?
85
-
86
- if @key
87
- req.params = { key: @key }
88
- else
89
- sign_http_request! req
90
- end
91
- end
92
- end
93
-
94
- return JSON.parse(response.body)["data"] if response.success?
95
-
96
- raise Google::Cloud::Error.gapi_error_class_for(response.status)
97
- rescue Faraday::ConnectionFailed
98
- raise Google::Cloud::ResourceExhaustedError
99
- end
100
-
101
- ##
102
- # The HTTP object that makes calls to API.
103
- # This must be a Faraday object.
104
- def http
105
- @http ||= Faraday.new url: @host, request: {
106
- open_timeout: @timeout, timeout: @timeout
107
- }.delete_if { |_k, v| v.nil? }
108
- end
109
-
110
- ##
111
- # The default HTTP headers to be sent on all API calls.
112
- def default_http_headers
113
- @default_http_headers ||= begin
114
- 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} gccl/#{Google::Cloud::Translate::VERSION}"
119
- }
120
- headers["x-goog-user-project"] = credentials.quota_project_id if credentials.respond_to? :quota_project_id
121
- headers
122
- end
123
- end
124
-
125
- ##
126
- # Make a request and apply incremental backoff
127
- def execute
128
- backoff = Backoff.new retries: retries
129
- backoff.execute do
130
- yield
131
- end
132
- rescue Faraday::ConnectionFailed
133
- raise Google::Cloud::ResourceExhaustedError
134
- end
135
-
136
- ##
137
- # Sign Oauth2 API calls.
138
- def sign_http_request! request
139
- client = credentials.client
140
- return if client.nil?
141
-
142
- client.fetch_access_token! if client.expires_within? 30
143
- client.generate_authenticated_request request: request
144
- request
145
- end
146
-
147
- ##
148
- # @private Backoff
149
- class Backoff
150
- class << self
151
- attr_accessor :retries
152
- attr_accessor :http_codes
153
- attr_accessor :reasons
154
- attr_accessor :backoff # :nodoc:
155
- end
156
-
157
- # Set the default values
158
- self.retries = 3
159
- self.http_codes = [500, 503]
160
- self.reasons = ["rateLimitExceeded", "userRateLimitExceeded"]
161
- self.backoff = ->(retries) { sleep retries.to_i }
162
-
163
- def initialize options = {} #:nodoc:
164
- @max_retries = (options[:retries] || Backoff.retries).to_i
165
- @http_codes = (options[:http_codes] || Backoff.http_codes).to_a
166
- @reasons = (options[:reasons] || Backoff.reasons).to_a
167
- @backoff = options[:backoff] || Backoff.backoff
168
- end
169
-
170
- def execute #:nodoc:
171
- current_retries = 0
172
- loop do
173
- response = yield # Expecting Faraday::Response
174
- return response if response.success?
175
- break response unless retry? response, current_retries
176
- current_retries += 1
177
- @backoff.call current_retries
178
- end
179
- end
180
-
181
- protected
182
-
183
- def retry? result, current_retries #:nodoc:
184
- if current_retries < @max_retries
185
- return true if retry_http_code? result
186
- return true if retry_error_reason? result
187
- end
188
- false
189
- end
190
-
191
- def retry_http_code? response #:nodoc:
192
- @http_codes.include? response.status
193
- end
194
-
195
- def retry_error_reason? response #:nodoc:
196
- result = JSON.parse response.body
197
- if result && result["error"] && result["error"]["errors"]
198
- Array(result["error"]["errors"]).each do |error|
199
- return true if error["reason"] && @reasons.include?(error["reason"])
200
- end
201
- end
202
- false
203
- end
204
- end
205
- end
206
- end
207
- end
208
- end
209
- end