google-cloud-translate 1.2.4 → 3.2.3

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.
@@ -1,208 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "google/cloud/errors"
17
- require "google/cloud/translate/credentials"
18
- require "google/cloud/translate/version"
19
- require "faraday"
20
-
21
- module Google
22
- module Cloud
23
- module Translate
24
- ##
25
- # @private
26
- # Represents the Translation API REST service, exposing the API calls.
27
- class Service #:nodoc:
28
- API_VERSION = "v2".freeze
29
- API_URL = "https://translation.googleapis.com".freeze
30
-
31
- # @private
32
- attr_accessor :project, :credentials, :retries, :timeout, :key
33
-
34
- ##
35
- # Creates a new Service instance.
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
43
- end
44
-
45
- ##
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
55
- end
56
-
57
- ##
58
- # Returns API::ListDetectionsResponse
59
- def detect text
60
- body = { q: Array(text) }.to_json
61
-
62
- post "language/translate/v2/detect", body
63
- end
64
-
65
- ##
66
- # Returns API::ListLanguagesResponse
67
- def languages language = nil
68
- body = { target: language }.to_json
69
-
70
- post "language/translate/v2/languages", body
71
- end
72
-
73
- def inspect
74
- self.class.to_s
75
- end
76
-
77
- protected
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
- sign_http_request! req
89
- end
90
- end
91
- end
92
-
93
- return JSON.parse(response.body)["data"] if response.success?
94
-
95
- raise 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
- "x-goog-api-client" => "gl-ruby/#{RUBY_VERSION} " \
117
- "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 = %w[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 &&
194
- result["error"] &&
195
- result["error"]["errors"]
196
- Array(result["error"]["errors"]).each do |error|
197
- if error["reason"] && @reasons.include?(error["reason"])
198
- return true
199
- end
200
- end
201
- end
202
- false
203
- end
204
- end
205
- end
206
- end
207
- end
208
- end
@@ -1,125 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Google
17
- module Cloud
18
- module Translate
19
- ##
20
- # # Translation
21
- #
22
- # Represents a translation query result. Returned by
23
- # {Google::Cloud::Translate::Api#translate}.
24
- #
25
- # @see https://cloud.google.com/translation/docs/translating-text#Translate
26
- # Translating Text
27
- #
28
- # @example
29
- # require "google/cloud/translate"
30
- #
31
- # translate = Google::Cloud::Translate.new
32
- #
33
- # translation = translate.translate "Hello world!", to: "la"
34
- #
35
- # translation.to_s #=> "Salve mundi!"
36
- #
37
- # translation.from #=> "en"
38
- # translation.origin #=> "Hello world!"
39
- # translation.to #=> "la"
40
- # translation.text #=> "Salve mundi!"
41
- #
42
- class Translation
43
- ##
44
- # The translated result.
45
- #
46
- # @return [String]
47
- attr_reader :text
48
- alias to_s text
49
- alias to_str text
50
-
51
- ##
52
- # The original query text that was translated.
53
- #
54
- # @return [String]
55
- attr_reader :origin
56
-
57
- ##
58
- # The target language into which the text was translated.
59
- #
60
- # @return [String]
61
- attr_reader :to
62
- alias language to
63
- alias target to
64
-
65
- ##
66
- # The source language from which the text was translated.
67
- #
68
- # @return [String]
69
- attr_reader :from
70
- alias source from
71
-
72
- ##
73
- # The translation model. Can be either `base` for the Phrase-Based
74
- # Machine Translation (PBMT) model, or `nmt` for the Neural Machine
75
- # Translation (NMT) model. If you did not include a model parameter with
76
- # your request, then this field is not included in the response.
77
- #
78
- # @return [String]
79
- attr_reader :model
80
-
81
- ##
82
- # @private Create a new object.
83
- def initialize text, to, origin, from, model, detected
84
- @text = text
85
- @to = to
86
- @origin = origin
87
- @from = from
88
- @model = model
89
- @detected = detected
90
- end
91
-
92
- ##
93
- # Determines if the source language was detected by the Google Cloud
94
- # Cloud Translation API.
95
- #
96
- # @return [Boolean] `true` if the source language was detected by the
97
- # Cloud Translation API, `false` if the source language was provided
98
- # in the request
99
- def detected?
100
- @detected
101
- end
102
-
103
- ##
104
- # @private New Translation from a TranslationsListResponse object as
105
- # defined by the Google API Client object.
106
- def self.from_gapi_list gapi, text, to, from
107
- res = text.zip(Array(gapi["translations"])).map do |origin, g|
108
- from_gapi g, to, origin, from
109
- end
110
- return res.first if res.size == 1
111
- res
112
- end
113
-
114
- ##
115
- # @private New Translation from a TranslationsResource object as defined
116
- # by the Google API Client object.
117
- def self.from_gapi gapi, to, origin, from
118
- from ||= gapi["detectedSourceLanguage"]
119
- detected = !gapi["detectedSourceLanguage"].nil?
120
- new gapi["translatedText"], to, origin, from, gapi["model"], detected
121
- end
122
- end
123
- end
124
- end
125
- end