google-cloud-translate 1.4.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/AUTHENTICATION.md +6 -2
- data/CHANGELOG.md +33 -0
- data/CONTRIBUTING.md +1 -1
- data/LICENSE +1 -1
- data/OVERVIEW.md +223 -18
- data/lib/google-cloud-translate.rb +79 -91
- data/lib/google/cloud/translate.rb +127 -114
- data/lib/google/cloud/translate/v2.rb +169 -0
- data/lib/google/cloud/translate/v2/api.rb +255 -0
- data/lib/google/cloud/translate/v2/credentials.rb +58 -0
- data/lib/google/cloud/translate/v2/detection.rb +132 -0
- data/lib/google/cloud/translate/v2/language.rb +68 -0
- data/lib/google/cloud/translate/v2/service.rb +205 -0
- data/lib/google/cloud/translate/v2/translation.rb +120 -0
- data/lib/google/cloud/translate/v3.rb +144 -0
- data/lib/google/cloud/translate/v3/credentials.rb +42 -0
- data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +663 -0
- data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +51 -0
- data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +131 -0
- data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +113 -0
- data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +87 -0
- data/lib/google/cloud/translate/v3/translation_service_client.rb +927 -0
- data/lib/google/cloud/translate/v3/translation_service_client_config.json +66 -0
- data/lib/google/cloud/translate/v3/translation_service_pb.rb +226 -0
- data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +68 -0
- data/lib/google/cloud/translate/version.rb +1 -1
- metadata +47 -37
- data/lib/google/cloud/translate/api.rb +0 -274
- data/lib/google/cloud/translate/credentials.rb +0 -57
- data/lib/google/cloud/translate/detection.rb +0 -139
- data/lib/google/cloud/translate/language.rb +0 -70
- data/lib/google/cloud/translate/service.rb +0 -206
- data/lib/google/cloud/translate/translation.rb +0 -125
@@ -0,0 +1,205 @@
|
|
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 ||= {
|
114
|
+
"User-Agent" => "gcloud-ruby/#{Google::Cloud::Translate::VERSION}",
|
115
|
+
"google-cloud-resource-prefix" => "projects/#{@project}",
|
116
|
+
"Content-Type" => "application/json",
|
117
|
+
"x-goog-api-client" => "gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Translate::VERSION}"
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Make a request and apply incremental backoff
|
123
|
+
def execute
|
124
|
+
backoff = Backoff.new retries: retries
|
125
|
+
backoff.execute do
|
126
|
+
yield
|
127
|
+
end
|
128
|
+
rescue Faraday::ConnectionFailed
|
129
|
+
raise Google::Cloud::ResourceExhaustedError
|
130
|
+
end
|
131
|
+
|
132
|
+
##
|
133
|
+
# Sign Oauth2 API calls.
|
134
|
+
def sign_http_request! request
|
135
|
+
client = credentials.client
|
136
|
+
return if client.nil?
|
137
|
+
|
138
|
+
client.fetch_access_token! if client.expires_within? 30
|
139
|
+
client.generate_authenticated_request request: request
|
140
|
+
request
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# @private Backoff
|
145
|
+
class Backoff
|
146
|
+
class << self
|
147
|
+
attr_accessor :retries
|
148
|
+
attr_accessor :http_codes
|
149
|
+
attr_accessor :reasons
|
150
|
+
attr_accessor :backoff # :nodoc:
|
151
|
+
end
|
152
|
+
|
153
|
+
# Set the default values
|
154
|
+
self.retries = 3
|
155
|
+
self.http_codes = [500, 503]
|
156
|
+
self.reasons = ["rateLimitExceeded", "userRateLimitExceeded"]
|
157
|
+
self.backoff = ->(retries) { sleep retries.to_i }
|
158
|
+
|
159
|
+
def initialize options = {} #:nodoc:
|
160
|
+
@max_retries = (options[:retries] || Backoff.retries).to_i
|
161
|
+
@http_codes = (options[:http_codes] || Backoff.http_codes).to_a
|
162
|
+
@reasons = (options[:reasons] || Backoff.reasons).to_a
|
163
|
+
@backoff = options[:backoff] || Backoff.backoff
|
164
|
+
end
|
165
|
+
|
166
|
+
def execute #:nodoc:
|
167
|
+
current_retries = 0
|
168
|
+
loop do
|
169
|
+
response = yield # Expecting Faraday::Response
|
170
|
+
return response if response.success?
|
171
|
+
break response unless retry? response, current_retries
|
172
|
+
current_retries += 1
|
173
|
+
@backoff.call current_retries
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
protected
|
178
|
+
|
179
|
+
def retry? result, current_retries #:nodoc:
|
180
|
+
if current_retries < @max_retries
|
181
|
+
return true if retry_http_code? result
|
182
|
+
return true if retry_error_reason? result
|
183
|
+
end
|
184
|
+
false
|
185
|
+
end
|
186
|
+
|
187
|
+
def retry_http_code? response #:nodoc:
|
188
|
+
@http_codes.include? response.status
|
189
|
+
end
|
190
|
+
|
191
|
+
def retry_error_reason? response #:nodoc:
|
192
|
+
result = JSON.parse response.body
|
193
|
+
if result && result["error"] && result["error"]["errors"]
|
194
|
+
Array(result["error"]["errors"]).each do |error|
|
195
|
+
return true if error["reason"] && @reasons.include?(error["reason"])
|
196
|
+
end
|
197
|
+
end
|
198
|
+
false
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
@@ -0,0 +1,120 @@
|
|
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
|
+
# # Translation
|
22
|
+
#
|
23
|
+
# Represents a translation query result. Returned by {Google::Cloud::Translate::V2::Api#translate}.
|
24
|
+
#
|
25
|
+
# @see https://cloud.google.com/translation/docs/translating-text#Translate Translating Text
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# require "google/cloud/translate"
|
29
|
+
#
|
30
|
+
# translate = Google::Cloud::Translate.new version: :v2
|
31
|
+
#
|
32
|
+
# translation = translate.translate "Hello world!", to: "la"
|
33
|
+
#
|
34
|
+
# translation.to_s #=> "Salve mundi!"
|
35
|
+
#
|
36
|
+
# translation.from #=> "en"
|
37
|
+
# translation.origin #=> "Hello world!"
|
38
|
+
# translation.to #=> "la"
|
39
|
+
# translation.text #=> "Salve mundi!"
|
40
|
+
#
|
41
|
+
class Translation
|
42
|
+
##
|
43
|
+
# The translated result.
|
44
|
+
#
|
45
|
+
# @return [String]
|
46
|
+
attr_reader :text
|
47
|
+
alias to_s text
|
48
|
+
alias to_str text
|
49
|
+
|
50
|
+
##
|
51
|
+
# The original query text that was translated.
|
52
|
+
#
|
53
|
+
# @return [String]
|
54
|
+
attr_reader :origin
|
55
|
+
|
56
|
+
##
|
57
|
+
# The target language into which the text was translated.
|
58
|
+
#
|
59
|
+
# @return [String]
|
60
|
+
attr_reader :to
|
61
|
+
alias language to
|
62
|
+
alias target to
|
63
|
+
|
64
|
+
##
|
65
|
+
# The source language from which the text was translated.
|
66
|
+
#
|
67
|
+
# @return [String]
|
68
|
+
attr_reader :from
|
69
|
+
alias source from
|
70
|
+
|
71
|
+
##
|
72
|
+
# The translation model. Can be either `base` for the Phrase-Based Machine Translation (PBMT) model, or `nmt`
|
73
|
+
# for the Neural Machine Translation (NMT) model. If you did not include a model parameter with your request,
|
74
|
+
# then this field is not included in the response.
|
75
|
+
#
|
76
|
+
# @return [String]
|
77
|
+
attr_reader :model
|
78
|
+
|
79
|
+
##
|
80
|
+
# @private Create a new object.
|
81
|
+
def initialize text, to, origin, from, model, detected
|
82
|
+
@text = text
|
83
|
+
@to = to
|
84
|
+
@origin = origin
|
85
|
+
@from = from
|
86
|
+
@model = model
|
87
|
+
@detected = detected
|
88
|
+
end
|
89
|
+
|
90
|
+
##
|
91
|
+
# Determines if the source language was detected by the Google Cloud Cloud Translation API.
|
92
|
+
#
|
93
|
+
# @return [Boolean] `true` if the source language was detected by the Cloud Translation API, `false` if the
|
94
|
+
# source language was provided in the request
|
95
|
+
def detected?
|
96
|
+
@detected
|
97
|
+
end
|
98
|
+
|
99
|
+
##
|
100
|
+
# @private New Translation from a TranslationsListResponse object as defined by the Google API Client object.
|
101
|
+
def self.from_gapi_list gapi, text, to, from
|
102
|
+
res = text.zip(Array(gapi["translations"])).map do |origin, g|
|
103
|
+
from_gapi g, to, origin, from
|
104
|
+
end
|
105
|
+
return res.first if res.size == 1
|
106
|
+
res
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
# @private New Translation from a TranslationsResource object as defined by the Google API Client object.
|
111
|
+
def self.from_gapi gapi, to, origin, from
|
112
|
+
from ||= gapi["detectedSourceLanguage"]
|
113
|
+
detected = !gapi["detectedSourceLanguage"].nil?
|
114
|
+
new gapi["translatedText"], to, origin, from, gapi["model"], detected
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# Copyright 2019 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
|
+
require "google/cloud/translate/v3/translation_service_client"
|
16
|
+
|
17
|
+
module Google
|
18
|
+
module Cloud
|
19
|
+
module Translate
|
20
|
+
# rubocop:disable LineLength
|
21
|
+
|
22
|
+
##
|
23
|
+
# # Ruby Client for Cloud Translation API ([Alpha](https://github.com/googleapis/google-cloud-ruby#versioning))
|
24
|
+
#
|
25
|
+
# [Cloud Translation API][Product Documentation]:
|
26
|
+
# Integrates text translation into your website or application.
|
27
|
+
# - [Product Documentation][]
|
28
|
+
#
|
29
|
+
# ## Quick Start
|
30
|
+
# In order to use this library, you first need to go through the following
|
31
|
+
# steps:
|
32
|
+
#
|
33
|
+
# 1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project)
|
34
|
+
# 2. [Enable billing for your project.](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project)
|
35
|
+
# 3. [Enable the Cloud Translation API.](https://console.cloud.google.com/apis/library/translate.googleapis.com)
|
36
|
+
# 4. [Setup Authentication.](https://googleapis.dev/ruby/google-cloud-translate/latest/file.AUTHENTICATION.html)
|
37
|
+
#
|
38
|
+
# ### Installation
|
39
|
+
# ```
|
40
|
+
# $ gem install google-cloud-translate
|
41
|
+
# ```
|
42
|
+
#
|
43
|
+
# ### Next Steps
|
44
|
+
# - Read the [Cloud Translation API Product documentation][Product Documentation]
|
45
|
+
# to learn more about the product and see How-to Guides.
|
46
|
+
# - View this [repository's main README](https://github.com/googleapis/google-cloud-ruby/blob/master/README.md)
|
47
|
+
# to see the full list of Cloud APIs that we cover.
|
48
|
+
#
|
49
|
+
# [Product Documentation]: https://cloud.google.com/translate
|
50
|
+
#
|
51
|
+
# ## Enabling Logging
|
52
|
+
#
|
53
|
+
# To enable logging for this library, set the logger for the underlying [gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library.
|
54
|
+
# The logger that you set may be a Ruby stdlib [`Logger`](https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html) as shown below,
|
55
|
+
# or a [`Google::Cloud::Logging::Logger`](https://googleapis.dev/ruby/google-cloud-logging/latest)
|
56
|
+
# that will write logs to [Stackdriver Logging](https://cloud.google.com/logging/). See [grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb)
|
57
|
+
# and the gRPC [spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb) for additional information.
|
58
|
+
#
|
59
|
+
# Configuring a Ruby stdlib logger:
|
60
|
+
#
|
61
|
+
# ```ruby
|
62
|
+
# require "logger"
|
63
|
+
#
|
64
|
+
# module MyLogger
|
65
|
+
# LOGGER = Logger.new $stderr, level: Logger::WARN
|
66
|
+
# def logger
|
67
|
+
# LOGGER
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# # Define a gRPC module-level logger method before grpc/logconfig.rb loads.
|
72
|
+
# module GRPC
|
73
|
+
# extend MyLogger
|
74
|
+
# end
|
75
|
+
# ```
|
76
|
+
#
|
77
|
+
module V3
|
78
|
+
# rubocop:enable LineLength
|
79
|
+
|
80
|
+
##
|
81
|
+
# Provides natural language translation operations.
|
82
|
+
#
|
83
|
+
# @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc]
|
84
|
+
# Provides the means for authenticating requests made by the client. This parameter can
|
85
|
+
# be many types.
|
86
|
+
# A `Google::Auth::Credentials` uses a the properties of its represented keyfile for
|
87
|
+
# authenticating requests made by this client.
|
88
|
+
# A `String` will be treated as the path to the keyfile to be used for the construction of
|
89
|
+
# credentials for this client.
|
90
|
+
# A `Hash` will be treated as the contents of a keyfile to be used for the construction of
|
91
|
+
# credentials for this client.
|
92
|
+
# A `GRPC::Core::Channel` will be used to make calls through.
|
93
|
+
# A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials
|
94
|
+
# should already be composed with a `GRPC::Core::CallCredentials` object.
|
95
|
+
# A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the
|
96
|
+
# metadata for requests, generally, to give OAuth credentials.
|
97
|
+
# @param scopes [Array<String>]
|
98
|
+
# The OAuth scopes for this service. This parameter is ignored if
|
99
|
+
# an updater_proc is supplied.
|
100
|
+
# @param client_config [Hash]
|
101
|
+
# A Hash for call options for each method. See
|
102
|
+
# Google::Gax#construct_settings for the structure of
|
103
|
+
# this data. Falls back to the default config if not specified
|
104
|
+
# or the specified config is missing data points.
|
105
|
+
# @param timeout [Numeric]
|
106
|
+
# The default timeout, in seconds, for calls made through this client.
|
107
|
+
# @param metadata [Hash]
|
108
|
+
# Default metadata to be sent with each request. This can be overridden on a per call basis.
|
109
|
+
# @param service_address [String]
|
110
|
+
# Override for the service hostname, or `nil` to leave as the default.
|
111
|
+
# @param service_port [Integer]
|
112
|
+
# Override for the service port, or `nil` to leave as the default.
|
113
|
+
# @param exception_transformer [Proc]
|
114
|
+
# An optional proc that intercepts any exceptions raised during an API call to inject
|
115
|
+
# custom error handling.
|
116
|
+
def self.new \
|
117
|
+
credentials: nil,
|
118
|
+
scopes: nil,
|
119
|
+
client_config: nil,
|
120
|
+
timeout: nil,
|
121
|
+
metadata: nil,
|
122
|
+
service_address: nil,
|
123
|
+
service_port: nil,
|
124
|
+
exception_transformer: nil,
|
125
|
+
lib_name: nil,
|
126
|
+
lib_version: nil
|
127
|
+
kwargs = {
|
128
|
+
credentials: credentials,
|
129
|
+
scopes: scopes,
|
130
|
+
client_config: client_config,
|
131
|
+
timeout: timeout,
|
132
|
+
metadata: metadata,
|
133
|
+
exception_transformer: exception_transformer,
|
134
|
+
lib_name: lib_name,
|
135
|
+
service_address: service_address,
|
136
|
+
service_port: service_port,
|
137
|
+
lib_version: lib_version
|
138
|
+
}.select { |_, v| v != nil }
|
139
|
+
Google::Cloud::Translate::V3::TranslationServiceClient.new(**kwargs)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|