google-cloud-translate 0.21.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00a78c2f05368b2605ee490aa45a5a739c87aa24
4
- data.tar.gz: be0de578f2c0307ecff03e49926c419398dc5932
3
+ metadata.gz: 66242d7872855d86aa8dcf46cf6c9ae0e1fdebae
4
+ data.tar.gz: 7e680a3dda42c379d9324dd0e47c6ce9d8f968a6
5
5
  SHA512:
6
- metadata.gz: cdac52bd7be6b4a0aa777b3ae23939ff96f7a2700b8bf67acef6cb42181f15f0cf9e622053380f8b87642d79dc7601ff16bd5c2416e631c54dd675951bfc20d0
7
- data.tar.gz: 4a56b6ef0ad05e2fc27ac459fb8b300982f353ee69a52e78c767ff3b5e4a2f48f879c8f18c6587985bd47ee79bc78410457014b5fbd9adba539788280837ebe1
6
+ metadata.gz: 5e3e74fe9552e6cc422b0adf69576f37b2e1113ea237f1a9858efdc7a1364175ae58fcee6c34f0343384d23e2fe0b5976c53175eb169a11c9987529e4258fdd8
7
+ data.tar.gz: 43ae4eb5ccf6816d1e1f3ce7e558e6c92ff87b18fa7aee7ad613b08c26bc0e4c289e372f44daa70eaec15b1400f198cb081e150465b0fd4d3951433df4d56b99
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --no-private
2
+ --title=Google Cloud Translate
3
+ --markup markdown
4
+
5
+ ./lib/**/*.rb
6
+ -
7
+ README.md
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # google-cloud-translate
2
+
3
+ [Google Translate](https://cloud.google.com/translate/) ([docs](https://cloud.google.com/translate/docs)) provides a simple, programmatic interface for translating an arbitrary string into any supported language. It is highly responsive, so websites and applications can integrate with Translate API for fast, dynamic translation of source text. Language detection is also available in cases where the source language is unknown.
4
+
5
+ Translate API supports more than ninety different languages, from Afrikaans to Zulu. Used in combination, this enables translation between thousands of language pairs. Also, you can send in HTML and receive HTML with translated text back. You don't need to extract your source text or reassemble the translated content.
6
+
7
+ - [google-cloud-translate API documentation](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/master/google/cloud/translate)
8
+ - [google-cloud-translate on RubyGems](https://rubygems.org/gems/google-cloud-translate)
9
+ - [Google Translate documentation](https://cloud.google.com/translate/docs)
10
+
11
+ ## Quick Start
12
+
13
+ ```sh
14
+ $ gem install google-cloud-translate
15
+ ```
16
+
17
+ ## Authentication
18
+
19
+ Like other Cloud Platform services, Google Translate API supports
20
+ authentication using a project ID and OAuth 2.0 credentials. In addition,
21
+ it supports authentication using a public API access key. (If both the API
22
+ key and the project and OAuth 2.0 credentials are provided, the API key
23
+ will be used.) Instructions and configuration options are covered in the
24
+ [Authentication Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/guides/authentication).
25
+
26
+ ## Example
27
+
28
+ ```ruby
29
+ require "google/cloud/translate"
30
+
31
+ translate = Google::Cloud::Translate.new
32
+
33
+ translation = translate.translate "Hello world!", to: "la"
34
+
35
+ puts translation #=> Salve mundi!
36
+
37
+ translation.from #=> "en"
38
+ translation.origin #=> "Hello world!"
39
+ translation.to #=> "la"
40
+ translation.text #=> "Salve mundi!"
41
+ ```
42
+
43
+ ## Supported Ruby Versions
44
+
45
+ This library is supported on Ruby 2.0+.
46
+
47
+ ## Versioning
48
+
49
+ This library follows [Semantic Versioning](http://semver.org/).
50
+
51
+ It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.
52
+
53
+ ## Contributing
54
+
55
+ Contributions to this library are always welcome and highly encouraged.
56
+
57
+ See the [Contributing Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/contributing) for more information on how to get started.
58
+
59
+ Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See [Code of Conduct](../CODE_OF_CONDUCT.md) for more information.
60
+
61
+ ## License
62
+
63
+ This library is licensed under Apache 2.0. Full license text is available in [LICENSE](LICENSE).
64
+
65
+ ## Support
66
+
67
+ Please [report bugs at the project on Github](https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues).
68
+ Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby) about the client or APIs on [StackOverflow](http://stackoverflow.com).
@@ -27,15 +27,22 @@ module Google
27
27
  # Creates a new object for connecting to the Translate service.
28
28
  # Each call creates a new connection.
29
29
  #
30
- # Unlike other Cloud Platform services, which authenticate using a project
31
- # ID and OAuth 2.0 credentials, Google Translate API requires a public API
32
- # access key. (This may change in future releases of Google Translate API.)
33
- # Follow the general instructions at [Identifying your application to
34
- # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
35
- # specific instructions for [Server
36
- # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
30
+ # Like other Cloud Platform services, Google Translate API supports
31
+ # authentication using a project ID and OAuth 2.0 credentials. In addition,
32
+ # it supports authentication using a public API access key. (If both the API
33
+ # key and the project and OAuth 2.0 credentials are provided, the API key
34
+ # will be used.) Instructions and configuration options are covered in the
35
+ # [Authentication Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/guides/authentication).
37
36
  #
38
37
  # @param [String] key a public API access key (not an OAuth 2.0 token)
38
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
39
+ # set of resources and operations that the connection can access. See
40
+ # [Using OAuth 2.0 to Access Google
41
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
42
+ #
43
+ # The default scope is:
44
+ #
45
+ # * `https://www.googleapis.com/auth/cloud-platform`
39
46
  # @param [Integer] retries Number of times to retry requests on server
40
47
  # error. The default value is `3`. Optional.
41
48
  # @param [Integer] timeout Default timeout to use in requests. Optional.
@@ -62,8 +69,10 @@ module Google
62
69
  # translation = translate.translate "Hello world!", to: "la"
63
70
  # translation.text #=> "Salve mundi!"
64
71
  #
65
- def translate key = nil, retries: nil, timeout: nil
66
- Google::Cloud.translate key, retries: (retries || @retries),
72
+ def translate key = nil, scope: nil, retries: nil, timeout: nil
73
+ Google::Cloud.translate key, project: @project, keyfile: @keyfile,
74
+ scope: scope,
75
+ retries: (retries || @retries),
67
76
  timeout: (timeout || @timeout)
68
77
  end
69
78
 
@@ -71,15 +80,26 @@ module Google
71
80
  # Creates a new object for connecting to the Translate service.
72
81
  # Each call creates a new connection.
73
82
  #
74
- # Unlike other Cloud Platform services, which authenticate using a project
75
- # ID and OAuth 2.0 credentials, Google Translate API requires a public API
76
- # access key. (This may change in future releases of Google Translate API.)
77
- # Follow the general instructions at [Identifying your application to
78
- # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
79
- # specific instructions for [Server
80
- # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
83
+ # Like other Cloud Platform services, Google Translate API supports
84
+ # authentication using a project ID and OAuth 2.0 credentials. In addition,
85
+ # it supports authentication using a public API access key. (If both the API
86
+ # key and the project and OAuth 2.0 credentials are provided, the API key
87
+ # will be used.) Instructions and configuration options are covered in the
88
+ # [Authentication Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/guides/authentication).
81
89
  #
82
90
  # @param [String] key a public API access key (not an OAuth 2.0 token)
91
+ # @param [String] project Project identifier for the Translate service you
92
+ # are connecting to.
93
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
94
+ # file path the file must be readable.
95
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
96
+ # set of resources and operations that the connection can access. See
97
+ # [Using OAuth 2.0 to Access Google
98
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
99
+ #
100
+ # The default scope is:
101
+ #
102
+ # * `https://www.googleapis.com/auth/cloud-platform`
83
103
  # @param [Integer] retries Number of times to retry requests on server
84
104
  # error. The default value is `3`. Optional.
85
105
  # @param [Integer] timeout Default timeout to use in requests. Optional.
@@ -104,9 +124,12 @@ module Google
104
124
  # translation = translate.translate "Hello world!", to: "la"
105
125
  # translation.text #=> "Salve mundi!"
106
126
  #
107
- def self.translate key = nil, retries: nil, timeout: nil
127
+ def self.translate key = nil, project: nil, keyfile: nil, scope: nil,
128
+ retries: nil, timeout: nil
108
129
  require "google/cloud/translate"
109
- Google::Cloud::Translate.new key: key, retries: retries, timeout: timeout
130
+ Google::Cloud::Translate.new key: key, project: project, keyfile: keyfile,
131
+ scope: scope, retries: retries,
132
+ timeout: timeout
110
133
  end
111
134
  end
112
135
  end
@@ -34,15 +34,25 @@ module Google
34
34
  # with translated text back. You don't need to extract your source text or
35
35
  # reassemble the translated content.
36
36
  #
37
+ # ## Premium Edition
38
+ #
39
+ # Using the `model` parameter, you can set the model used by the service to
40
+ # perform the translation. The neural machine translation model (`nmt`) is
41
+ # billed as a premium edition feature. Because neural machine translation is
42
+ # computationally significantly more resource intensive than the standard
43
+ # model, the price for the premium edition is higher than the standard
44
+ # edition. If the `model` parameter not set or is set to `base`, then the
45
+ # service will return translation using the current standard model and
46
+ # standard edition pricing.
47
+ #
37
48
  # ## Authenticating
38
49
  #
39
- # Unlike other Cloud Platform services, which authenticate using a project
40
- # ID and OAuth 2.0 credentials, Translate API requires a public API access
41
- # key. (This may change in future releases of Translate API.) Follow the
42
- # general instructions at [Identifying your application to
43
- # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
44
- # specific instructions for [Server
45
- # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
50
+ # Like other Cloud Platform services, Google Translate API supports
51
+ # authentication using a project ID and OAuth 2.0 credentials. In addition,
52
+ # it supports authentication using a public API access key. (If both the API
53
+ # key and the project and OAuth 2.0 credentials are provided, the API key
54
+ # will be used.) Instructions and configuration options are covered in the
55
+ # [Authentication Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/guides/authentication).
46
56
  #
47
57
  # ## Translating texts
48
58
  #
@@ -211,14 +221,26 @@ module Google
211
221
  # Creates a new object for connecting to the Translate service.
212
222
  # Each call creates a new connection.
213
223
  #
214
- # Unlike other Cloud Platform services, which authenticate using a project
215
- # ID and OAuth 2.0 credentials, Google Translate API requires a public API
216
- # access key. (This may change in future releases of Google Translate
217
- # API.) Follow the general instructions at [Identifying your application
218
- # to Google](https://cloud.google.com/translate/v2/using_rest#auth), and
219
- # the specific instructions for [Server
220
- # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
224
+ # Like other Cloud Platform services, Google Translate API supports
225
+ # authentication using a project ID and OAuth 2.0 credentials. In
226
+ # addition, it supports authentication using a public API access key. (If
227
+ # both the API key and the project and OAuth 2.0 credentials are provided,
228
+ # the API key will be used.) Instructions and configuration options are
229
+ # covered in the [Authentication
230
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-translate/guides/authentication).
231
+ #
232
+ # @param [String] project Project identifier for the Translate service you
233
+ # are connecting to.
234
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
235
+ # file path the file must be readable.
236
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
237
+ # the set of resources and operations that the connection can access.
238
+ # See [Using OAuth 2.0 to Access Google
239
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
240
+ #
241
+ # The default scope is:
221
242
  #
243
+ # * `https://www.googleapis.com/auth/cloud-platform`
222
244
  # @param [String] key a public API access key (not an OAuth 2.0 token)
223
245
  # @param [Integer] retries Number of times to retry requests on server
224
246
  # error. The default value is `3`. Optional.
@@ -229,7 +251,20 @@ module Google
229
251
  # @example
230
252
  # require "google/cloud/translate"
231
253
  #
232
- # translate = Google::Cloud::Translate.new key: "api-key-abc123XYZ789"
254
+ # translate = Google::Cloud::Translate.new(
255
+ # project: "my-todo-project",
256
+ # keyfile: "/path/to/keyfile.json"
257
+ # )
258
+ #
259
+ # translation = translate.translate "Hello world!", to: "la"
260
+ # translation.text #=> "Salve mundi!"
261
+ #
262
+ # @example Using API Key.
263
+ # require "google/cloud/translate"
264
+ #
265
+ # translate = Google::Cloud::Translate.new(
266
+ # key: "api-key-abc123XYZ789"
267
+ # )
233
268
  #
234
269
  # translation = translate.translate "Hello world!", to: "la"
235
270
  # translation.text #=> "Salve mundi!"
@@ -244,17 +279,30 @@ module Google
244
279
  # translation = translate.translate "Hello world!", to: "la"
245
280
  # translation.text #=> "Salve mundi!"
246
281
  #
247
- def self.new key: nil, retries: nil, timeout: nil
282
+ def self.new project: nil, keyfile: nil, scope: nil, key: nil,
283
+ retries: nil, timeout: nil
284
+ project ||= Google::Cloud::Translate::Api.default_project
285
+ project = project.to_s # Always cast to a string
286
+
248
287
  key ||= ENV["TRANSLATE_KEY"]
249
288
  key ||= ENV["GOOGLE_CLOUD_KEY"]
250
- if key.nil?
251
- key_missing_msg = "An API key is required to use the Translate API."
252
- fail ArgumentError, key_missing_msg
289
+ if key
290
+ return Google::Cloud::Translate::Api.new(
291
+ Google::Cloud::Translate::Service.new(
292
+ project, nil, retries: retries, timeout: timeout, key: key))
293
+ end
294
+
295
+ if keyfile.nil?
296
+ credentials = Google::Cloud::Translate::Credentials.default(
297
+ scope: scope)
298
+ else
299
+ credentials = Google::Cloud::Translate::Credentials.new(
300
+ keyfile, scope: scope)
253
301
  end
254
302
 
255
303
  Google::Cloud::Translate::Api.new(
256
304
  Google::Cloud::Translate::Service.new(
257
- key, retries: retries, timeout: timeout))
305
+ project, credentials, retries: retries, timeout: timeout))
258
306
  end
259
307
  end
260
308
  end
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
+ require "google/cloud/core/environment"
16
17
  require "google/cloud/translate/service"
17
18
  require "google/cloud/translate/translation"
18
19
  require "google/cloud/translate/detection"
@@ -24,13 +25,12 @@ module Google
24
25
  ##
25
26
  # # Api
26
27
  #
27
- # Represents top-level access to the Google Translate API. Each instance
28
- # requires a public API access key. To create a key, follow the general
29
- # instructions at [Identifying your application to
30
- # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
31
- # specific instructions for [Server
32
- # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
33
- # See {Google::Cloud#translate}.
28
+ # Represents top-level access to the Google Translate API. Translate API
29
+ # supports more than ninety different languages, from Afrikaans to Zulu.
30
+ # Used in combination, this enables translation between thousands of
31
+ # language pairs. Also, you can send in HTML and receive HTML with
32
+ # translated text back. You don't need to extract your source text or
33
+ # reassemble the translated content.
34
34
  #
35
35
  # @see https://cloud.google.com/translate/v2/getting_started Translate API
36
36
  # Getting Started
@@ -62,6 +62,32 @@ module Google
62
62
  @service = service
63
63
  end
64
64
 
65
+ ##
66
+ # The Translate project connected to.
67
+ #
68
+ # @example
69
+ # require "google/cloud/translate"
70
+ #
71
+ # translate = Google::Cloud::Translate.new(
72
+ # project: "my-todo-project",
73
+ # keyfile: "/path/to/keyfile.json"
74
+ # )
75
+ #
76
+ # translate.project #=> "my-todo-project"
77
+ #
78
+ def project
79
+ service.project
80
+ end
81
+
82
+ ##
83
+ # @private Default project.
84
+ def self.default_project
85
+ ENV["TRANSLATE_PROJECT"] ||
86
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
87
+ ENV["GCLOUD_PROJECT"] ||
88
+ Google::Cloud::Core::Environment.project_id
89
+ end
90
+
65
91
  ##
66
92
  # Returns text translations from one language to another.
67
93
  #
@@ -80,6 +106,17 @@ module Google
80
106
  # @param [String] format The format of the text. Possible values include
81
107
  # `:text` and `:html`. This is optional. The Translate API default is
82
108
  # `:html`.
109
+ # @param [String] model The model used by the service to perform the
110
+ # translation. The neural machine translation model (`nmt`) is billed
111
+ # as a premium edition feature. If this is set to `base`, then the
112
+ # service will return translation using the current standard model.
113
+ # The default value is `base`.
114
+ #
115
+ # Acceptable values are:
116
+ #
117
+ # * `nmt` - Use the neural machine translation model
118
+ # * `base` - Use the current standard model
119
+ #
83
120
  # @param [String] cid The customization id for translate. This is
84
121
  # optional.
85
122
  #
@@ -101,6 +138,18 @@ module Google
101
138
  # translation.origin #=> "Hello world!"
102
139
  # translation.to #=> "la"
103
140
  # translation.text #=> "Salve mundi!"
141
+ # translation.model #=> "base"
142
+ #
143
+ # @example Using the neural machine translation model:
144
+ # require "google/cloud/translate"
145
+ #
146
+ # translate = Google::Cloud::Translate.new
147
+ #
148
+ # translation = translate.translate "Hello world!",
149
+ # to: "la", model: "nmt"
150
+ #
151
+ # translation.to_s #=> "Salve mundi!"
152
+ # translation.model #=> "nmt"
104
153
  #
105
154
  # @example Setting the `from` language.
106
155
  # require "google/cloud/translate"
@@ -133,7 +182,8 @@ module Google
133
182
  # to: :la
134
183
  # translation.text #=> "<strong>Salve</strong> mundi!"
135
184
  #
136
- def translate *text, to: nil, from: nil, format: nil, cid: nil
185
+ def translate *text, to: nil, from: nil, format: nil, model: nil,
186
+ cid: nil
137
187
  return nil if text.empty?
138
188
  fail ArgumentError, "to is required" if to.nil?
139
189
  to = to.to_s
@@ -141,7 +191,7 @@ module Google
141
191
  format = format.to_s if format
142
192
  text = Array(text).flatten
143
193
  gapi = service.translate text, to: to, from: from,
144
- format: format, cid: cid
194
+ format: format, model: model, cid: cid
145
195
  Translation.from_gapi_list gapi, text, to, from
146
196
  end
147
197
 
@@ -227,7 +277,7 @@ module Google
227
277
  def languages language = nil
228
278
  language = language.to_s if language
229
279
  gapi = service.languages language
230
- Array(gapi.languages).map { |g| Language.from_gapi g }
280
+ Array(gapi["languages"]).map { |g| Language.from_gapi g }
231
281
  end
232
282
  end
233
283
  end
@@ -0,0 +1,42 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+ # http://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/credentials"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Translate
21
+ ##
22
+ # @private Represents the OAuth 2.0 signing logic for Translate.
23
+ class Credentials < Google::Cloud::Credentials
24
+ SCOPE = ["https://www.googleapis.com/auth/cloud-platform"]
25
+ PATH_ENV_VARS = %w(TRANSLATE_KEYFILE GOOGLE_CLOUD_KEYFILE
26
+ GCLOUD_KEYFILE)
27
+ JSON_ENV_VARS = %w(TRANSLATE_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON
28
+ GCLOUD_KEYFILE_JSON)
29
+
30
+ ##
31
+ # @private Sign Oauth2 API calls.
32
+ def sign_http_request request #:nodoc:
33
+ if @client
34
+ @client.fetch_access_token! if @client.expires_within? 30
35
+ @client.generate_authenticated_request request: request
36
+ end
37
+ request
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -88,8 +88,8 @@ module Google
88
88
  # @private New Detection from a ListDetectionsResponse object as
89
89
  # defined by the Google API Client object.
90
90
  def self.from_gapi gapi, text
91
- res = text.zip(Array(gapi.detections)).map do |txt, detections_gapi|
92
- results = detections_gapi.map { |g| Result.from_gapi g }
91
+ res = text.zip(Array(gapi["detections"])).map do |txt, detections|
92
+ results = detections.map { |g| Result.from_gapi g }
93
93
  new txt, results
94
94
  end
95
95
  return res.first if res.size == 1
@@ -130,7 +130,7 @@ module Google
130
130
  # @private New Detection::Result from a DetectionsResource object as
131
131
  # defined by the Google API Client object.
132
132
  def self.from_gapi gapi
133
- new gapi.confidence, gapi.language
133
+ new gapi["confidence"], gapi["language"]
134
134
  end
135
135
  end
136
136
  end
@@ -62,7 +62,7 @@ module Google
62
62
  # @private New Language from a LanguagesResource object as defined by
63
63
  # the Google API Client object.
64
64
  def self.from_gapi gapi
65
- new gapi.language, gapi.name
65
+ new gapi["language"], gapi["name"]
66
66
  end
67
67
  end
68
68
  end
@@ -14,61 +14,60 @@
14
14
 
15
15
 
16
16
  require "google/cloud/errors"
17
+ require "google/cloud/translate/credentials"
17
18
  require "google/cloud/translate/version"
18
- require "google/apis/translate_v2"
19
+ require "faraday" # comes from googleauth, comes from google-cloud-core
19
20
 
20
21
  module Google
21
22
  module Cloud
22
23
  module Translate
23
24
  ##
24
25
  # @private
25
- # Represents the service to Translate, exposing the API calls.
26
- class Service
27
- ##
28
- # Alias to the Google Client API module
29
- API = Google::Apis::TranslateV2
26
+ # Represents the Translate REST service, exposing the API calls.
27
+ class Service #:nodoc:
28
+ API_VERSION = "v2"
29
+ API_URL = "https://translation.googleapis.com"
30
30
 
31
- attr_accessor :credentials
31
+ # @private
32
+ attr_accessor :project, :credentials, :retries, :timeout, :key
32
33
 
33
34
  ##
34
35
  # Creates a new Service instance.
35
- def initialize key, retries: nil, timeout: nil
36
- @service = API::TranslateService.new
37
- @service.client_options.application_name = "gcloud-ruby"
38
- @service.client_options.application_version = \
39
- Google::Cloud::Translate::VERSION
40
- @service.request_options.retries = retries || 3
41
- @service.request_options.timeout_sec = timeout
42
- @service.request_options.open_timeout_sec = timeout
43
- @service.authorization = nil
44
- @service.key = key
45
- end
46
-
47
- def service
48
- return mocked_service if mocked_service
49
- @service
36
+ def initialize project, credentials, retries: nil, timeout: nil,
37
+ key: nil
38
+ @project = project
39
+ @credentials = credentials
40
+ @retries = retries
41
+ @timeout = timeout
42
+ @key = key
50
43
  end
51
- attr_accessor :mocked_service
52
44
 
53
45
  ##
54
- # Returns API::ListTranslationsResponse
55
- def translate text, to: nil, from: nil, format: nil, cid: nil
56
- execute do
57
- service.list_translations Array(text), to, cid: cid, format: format,
58
- source: from
59
- end
46
+ # Returns Hash of ListTranslationsResponse JSON
47
+ def translate text, to: nil, from: nil, format: nil, model: nil,
48
+ cid: nil
49
+ body = {
50
+ q: Array(text), target: to, source: from, format: format,
51
+ model: model, cid: cid
52
+ }.delete_if { |_k, v| v.nil? }.to_json
53
+
54
+ post "/language/translate/v2", body
60
55
  end
61
56
 
62
57
  ##
63
58
  # Returns API::ListDetectionsResponse
64
59
  def detect text
65
- execute { service.list_detections Array(text) }
60
+ body = { q: Array(text) }.to_json
61
+
62
+ post "language/translate/v2/detect", body
66
63
  end
67
64
 
68
65
  ##
69
66
  # Returns API::ListLanguagesResponse
70
67
  def languages language = nil
71
- execute { service.list_languages target: language }
68
+ body = { target: language }.to_json
69
+
70
+ post "language/translate/v2/languages", body
72
71
  end
73
72
 
74
73
  def inspect
@@ -77,10 +76,118 @@ module Google
77
76
 
78
77
  protected
79
78
 
79
+ def post path, body = nil
80
+ response = execute do
81
+ http.post path do |req|
82
+ req.headers.merge! default_http_headers
83
+ req.body = body unless body.nil?
84
+
85
+ if @key
86
+ req.params = { key: @key }
87
+ else
88
+ @credentials.sign_http_request req
89
+ end
90
+ end
91
+ end
92
+
93
+ return JSON.parse(response.body)["data"] if response.success?
94
+
95
+ fail Google::Cloud::Error.gapi_error_class_for(response.status)
96
+ rescue Faraday::ConnectionFailed
97
+ raise Google::Cloud::ResourceExhaustedError
98
+ end
99
+
100
+ ##
101
+ # The HTTP object that makes calls to API.
102
+ # This must be a Faraday object.
103
+ def http
104
+ @http ||= Faraday.new url: API_URL, request: {
105
+ open_timeout: @timeout, timeout: @timeout
106
+ }.delete_if { |_k, v| v.nil? }
107
+ end
108
+
109
+ ##
110
+ # The default HTTP headers to be sent on all API calls.
111
+ def default_http_headers
112
+ @default_http_headers ||= {
113
+ "User-Agent" => "gcloud-ruby/#{Google::Cloud::Translate::VERSION}",
114
+ "google-cloud-resource-prefix" => "projects/#{@project}",
115
+ "Content-Type" => "application/json"
116
+ }
117
+ end
118
+
119
+ ##
120
+ # Make a request and apply incremental backoff
80
121
  def execute
81
- yield
82
- rescue Google::Apis::Error => e
83
- raise Google::Cloud::Error.from_error(e)
122
+ backoff = Backoff.new retries: retries
123
+ backoff.execute do
124
+ yield
125
+ end
126
+ rescue Faraday::ConnectionFailed
127
+ raise Google::Cloud::ResourceExhaustedError
128
+ end
129
+
130
+ ##
131
+ # @private Backoff
132
+ class Backoff
133
+ class << self
134
+ attr_accessor :retries
135
+ attr_accessor :http_codes
136
+ attr_accessor :reasons
137
+ attr_accessor :backoff # :nodoc:
138
+ end
139
+
140
+ # Set the default values
141
+ self.retries = 3
142
+ self.http_codes = [500, 503]
143
+ self.reasons = %w(rateLimitExceeded userRateLimitExceeded)
144
+ self.backoff = ->(retries) { sleep retries.to_i }
145
+
146
+ def initialize options = {} #:nodoc:
147
+ @max_retries = (options[:retries] || Backoff.retries).to_i
148
+ @http_codes = (options[:http_codes] || Backoff.http_codes).to_a
149
+ @reasons = (options[:reasons] || Backoff.reasons).to_a
150
+ @backoff = options[:backoff] || Backoff.backoff
151
+ end
152
+
153
+ def execute #:nodoc:
154
+ current_retries = 0
155
+ loop do
156
+ response = yield # Expecting Faraday::Response
157
+ return response if response.success?
158
+ break response unless retry? response, current_retries
159
+ current_retries += 1
160
+ @backoff.call current_retries
161
+ end
162
+ end
163
+
164
+ protected
165
+
166
+ def retry? result, current_retries #:nodoc:
167
+ if current_retries < @max_retries
168
+ return true if retry_http_code? result
169
+ return true if retry_error_reason? result
170
+ end
171
+ false
172
+ end
173
+
174
+ def retry_http_code? response #:nodoc:
175
+ @http_codes.include? response.status
176
+ end
177
+
178
+ def retry_error_reason? response #:nodoc:
179
+ result = JSON.parse(response.body)["data"]
180
+ if result &&
181
+ result["error"] &&
182
+ result["error"]["errors"]
183
+ Array(result["error"]["errors"]).each do |error|
184
+ if error["reason"] && @reasons.include?(error["reason"])
185
+ return true
186
+ end
187
+ end
188
+ end
189
+ false
190
+ end
84
191
  end
85
192
  end
86
193
  end
@@ -64,16 +64,36 @@ module Google
64
64
 
65
65
  ##
66
66
  # The source language from which the text was translated.
67
+ #
68
+ # @return [String]
67
69
  attr_reader :from
68
70
  alias_method :source, :from
69
71
 
72
+ ##
73
+ # The model used by the service to perform the translation. When this is
74
+ # set to `nmt`, the translation was performed using premium neural
75
+ # machine translation model. If it is not set or model is set to `base`,
76
+ # then the translation was done using standard model. In almost all
77
+ # cases, the model type in response should match the model type
78
+ # requested. However, in some limited situations this might not be the
79
+ # case. In these cases, the request had `nmt` parameter, but the
80
+ # response has `base` set or model is not returned. This happens when
81
+ # neural translation did not give a satisfactory translation and we
82
+ # completed the translation using the standard model. If this happens,
83
+ # you will charged at the standard edition rate and not at the premium
84
+ # rate.
85
+ #
86
+ # @return [String]
87
+ attr_reader :model
88
+
70
89
  ##
71
90
  # @private Create a new object.
72
- def initialize text, to, origin, from, detected
91
+ def initialize text, to, origin, from, model, detected
73
92
  @text = text
74
93
  @to = to
75
94
  @origin = origin
76
95
  @from = from
96
+ @model = model
77
97
  @detected = detected
78
98
  end
79
99
 
@@ -92,7 +112,7 @@ module Google
92
112
  # @private New Translation from a TranslationsListResponse object as
93
113
  # defined by the Google API Client object.
94
114
  def self.from_gapi_list gapi, text, to, from
95
- res = text.zip(Array(gapi.translations)).map do |origin, g|
115
+ res = text.zip(Array(gapi["translations"])).map do |origin, g|
96
116
  from_gapi g, to, origin, from
97
117
  end
98
118
  return res.first if res.size == 1
@@ -103,9 +123,9 @@ module Google
103
123
  # @private New Translation from a TranslationsResource object as defined
104
124
  # by the Google API Client object.
105
125
  def self.from_gapi gapi, to, origin, from
106
- from ||= gapi.detected_source_language
107
- detected = !gapi.detected_source_language.nil?
108
- new gapi.translated_text, to, origin, from, detected
126
+ from ||= gapi["detectedSourceLanguage"]
127
+ detected = !gapi["detectedSourceLanguage"].nil?
128
+ new gapi["translatedText"], to, origin, from, gapi["model"], detected
109
129
  end
110
130
  end
111
131
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Translate
19
- VERSION = "0.21.0"
19
+ VERSION = "0.22.0"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-20 00:00:00.000000000 Z
12
+ date: 2016-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -25,20 +25,6 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: 0.21.0
28
- - !ruby/object:Gem::Dependency
29
- name: google-api-client
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - "~>"
33
- - !ruby/object:Gem::Version
34
- version: 0.9.11
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: 0.9.11
42
28
  - !ruby/object:Gem::Dependency
43
29
  name: minitest
44
30
  requirement: !ruby/object:Gem::Requirement
@@ -157,14 +143,14 @@ dependencies:
157
143
  requirements:
158
144
  - - "~>"
159
145
  - !ruby/object:Gem::Version
160
- version: 0.1.6
146
+ version: 0.1.8
161
147
  type: :development
162
148
  prerelease: false
163
149
  version_requirements: !ruby/object:Gem::Requirement
164
150
  requirements:
165
151
  - - "~>"
166
152
  - !ruby/object:Gem::Version
167
- version: 0.1.6
153
+ version: 0.1.8
168
154
  description: google-cloud-translate is the official library for Google Translate API.
169
155
  email:
170
156
  - mike@blowmage.com
@@ -173,9 +159,13 @@ executables: []
173
159
  extensions: []
174
160
  extra_rdoc_files: []
175
161
  files:
162
+ - ".yardopts"
163
+ - LICENSE
164
+ - README.md
176
165
  - lib/google-cloud-translate.rb
177
166
  - lib/google/cloud/translate.rb
178
167
  - lib/google/cloud/translate/api.rb
168
+ - lib/google/cloud/translate/credentials.rb
179
169
  - lib/google/cloud/translate/detection.rb
180
170
  - lib/google/cloud/translate/language.rb
181
171
  - lib/google/cloud/translate/service.rb
@@ -201,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
191
  version: '0'
202
192
  requirements: []
203
193
  rubyforge_project:
204
- rubygems_version: 2.6.4
194
+ rubygems_version: 2.5.1
205
195
  signing_key:
206
196
  specification_version: 4
207
197
  summary: API Client library for Google Translate API