google-cloud-translate 2.3.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 +203 -0
  5. data/MIGRATING.md +302 -0
  6. data/README.md +80 -0
  7. data/lib/google-cloud-translate.rb +5 -146
  8. data/lib/google/cloud/translate.rb +81 -178
  9. data/lib/google/cloud/translate/helpers.rb +100 -0
  10. data/lib/google/cloud/translate/version.rb +6 -2
  11. metadata +35 -135
  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,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
@@ -1,120 +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
- # # 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
@@ -1,144 +0,0 @@
1
- # Copyright 2020 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
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