google-cloud-translate 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +6 -2
  3. data/CHANGELOG.md +33 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/LICENSE +1 -1
  6. data/OVERVIEW.md +223 -18
  7. data/lib/google-cloud-translate.rb +79 -91
  8. data/lib/google/cloud/translate.rb +127 -114
  9. data/lib/google/cloud/translate/v2.rb +169 -0
  10. data/lib/google/cloud/translate/v2/api.rb +255 -0
  11. data/lib/google/cloud/translate/v2/credentials.rb +58 -0
  12. data/lib/google/cloud/translate/v2/detection.rb +132 -0
  13. data/lib/google/cloud/translate/v2/language.rb +68 -0
  14. data/lib/google/cloud/translate/v2/service.rb +205 -0
  15. data/lib/google/cloud/translate/v2/translation.rb +120 -0
  16. data/lib/google/cloud/translate/v3.rb +144 -0
  17. data/lib/google/cloud/translate/v3/credentials.rb +42 -0
  18. data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +663 -0
  19. data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +51 -0
  20. data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +131 -0
  21. data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +113 -0
  22. data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +87 -0
  23. data/lib/google/cloud/translate/v3/translation_service_client.rb +927 -0
  24. data/lib/google/cloud/translate/v3/translation_service_client_config.json +66 -0
  25. data/lib/google/cloud/translate/v3/translation_service_pb.rb +226 -0
  26. data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +68 -0
  27. data/lib/google/cloud/translate/version.rb +1 -1
  28. metadata +47 -37
  29. data/lib/google/cloud/translate/api.rb +0 -274
  30. data/lib/google/cloud/translate/credentials.rb +0 -57
  31. data/lib/google/cloud/translate/detection.rb +0 -139
  32. data/lib/google/cloud/translate/language.rb +0 -70
  33. data/lib/google/cloud/translate/service.rb +0 -206
  34. data/lib/google/cloud/translate/translation.rb +0 -125
@@ -1,4 +1,4 @@
1
- # Copyright 2016 Google LLC
1
+ # Copyright 2019 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
14
14
 
15
15
 
16
16
  require "google-cloud-translate"
17
- require "google/cloud/translate/api"
18
17
  require "google/cloud/config"
19
- require "google/cloud/env"
18
+ require "google/gax"
19
+ require "pathname"
20
20
 
21
21
  module Google
22
22
  module Cloud
@@ -36,50 +36,113 @@ module Google
36
36
  # with translated text back. You don't need to extract your source text or
37
37
  # reassemble the translated content.
38
38
  #
39
+ # The google-cloud-translate 2.0 gem contains a generated v3 client and a legacy hand-written v2 client.
40
+ # To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`.
41
+ # See [Migrating to Translation v3](https://cloud.google.com/translate/docs/migrate-to-v3) for details regarding
42
+ # differences between v2 and v3.
43
+ #
39
44
  # See {file:OVERVIEW.md Translation Overview}.
40
45
  #
41
46
  module Translate
47
+ FILE_DIR = File.realdirpath Pathname.new(__FILE__).join("..").join("translate")
48
+
49
+ AVAILABLE_VERSIONS = Dir["#{FILE_DIR}/*"]
50
+ .select { |file| File.directory? file }
51
+ .select { |dir| Google::Gax::VERSION_MATCHER.match File.basename(dir) }
52
+ .select { |dir| File.exist? dir + ".rb" }
53
+ .map { |dir| File.basename dir }
54
+
42
55
  ##
43
- # Creates a new object for connecting to Cloud Translation API. Each call
44
- # creates a new connection.
45
- #
46
- # Like other Cloud Platform services, Google Cloud Translation API
47
- # supports authentication using a project ID and OAuth 2.0 credentials. In
48
- # addition, it supports authentication using a public API access key. (If
49
- # both the API key and the project and OAuth 2.0 credentials are provided,
50
- # the API key will be used.) Instructions and configuration options are
51
- # covered in the {file:AUTHENTICATION.md Authentication Guide}.
52
- #
53
- # @param [String] project_id Project identifier for the Cloud Translation
54
- # service you are connecting to. If not present, the default project for
55
- # the credentials is used.
56
- # @param [String, Hash, Google::Auth::Credentials] credentials The path to
57
- # the keyfile as a String, the contents of the keyfile as a Hash, or a
58
- # Google::Auth::Credentials object. (See {Translate::Credentials})
59
- # @param [String] key a public API access key (not an OAuth 2.0 token)
60
- # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
61
- # the set of resources and operations that the connection can access.
62
- # See [Using OAuth 2.0 to Access Google
63
- # APIs](https://developers.google.com/identity/protocols/OAuth2).
64
- #
65
- # The default scope is:
66
- #
67
- # * `https://www.googleapis.com/auth/cloud-platform`
68
- # @param [Integer] retries Number of times to retry requests on server
69
- # error. The default value is `3`. Optional.
70
- # @param [Integer] timeout Default timeout to use in requests. Optional.
71
- # @param [String] endpoint Override of the endpoint host name. Optional.
72
- # If the param is nil, uses the default endpoint.
73
- # @param [String] project Alias for the `project_id` argument. Deprecated.
74
- # @param [String] keyfile Alias for the `credentials` argument.
75
- # Deprecated.
76
- #
77
- # @return [Google::Cloud::Translate::Api]
78
- #
79
- # @example
56
+ # Provides natural language translation operations.
57
+ #
58
+ # @overload new(version:, credentials:, scopes:, client_config:, timeout:)
59
+ # @param version [Symbol, String]
60
+ # The major version of the service to be used. By default `:v3` is used.
61
+ # @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel,
62
+ # GRPC::Core::ChannelCredentials, Proc]
63
+ # Provides the means for authenticating requests made by the client. This parameter can be many types.
64
+ # A `Google::Auth::Credentials` uses a the properties of its represented keyfile for authenticating requests
65
+ # made by this client.
66
+ # A `String` will be treated as the path to the keyfile to be used for the construction of credentials for
67
+ # this client.
68
+ # A `Hash` will be treated as the contents of a keyfile to be used for the construction of credentials for
69
+ # this client.
70
+ # A `GRPC::Core::Channel` will be used to make calls through.
71
+ # A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials should already
72
+ # be composed with a `GRPC::Core::CallCredentials` object.
73
+ # A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for
74
+ # requests, generally, to give OAuth credentials.
75
+ # @param scopes [Array<String>]
76
+ # The OAuth scopes for this service. This parameter is ignored if an updater_proc is supplied.
77
+ # @param client_config [Hash]
78
+ # A Hash for call options for each method. See Google::Gax#construct_settings for the structure of this data.
79
+ # Falls back to the default config if not specified or the specified config is missing data points.
80
+ # @param timeout [Numeric]
81
+ # The default timeout, in seconds, for calls made through this client.
82
+ # @param metadata [Hash]
83
+ # Default metadata to be sent with each request. This can be overridden on a per call basis.
84
+ # @param service_address [String]
85
+ # Override for the service hostname, or `nil` to leave as the default.
86
+ # @param service_port [Integer]
87
+ # Override for the service port, or `nil` to leave as the default.
88
+ # @param exception_transformer [Proc]
89
+ # An optional proc that intercepts any exceptions raised during an API call to inject custom error handling.
90
+ # @overload new(version:, project_id:, credentials:, key:, scope:, retries:, timeout:, endpoint:)
91
+ # @param version [Symbol, String]
92
+ # The major version of the service to be used. Specifying `:v2` will return the legacy client.
93
+ # @param [String] project_id Project identifier for the Cloud Translation service you are connecting to. If not
94
+ # present, the default project for the credentials is used.
95
+ # @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents
96
+ # of the keyfile as a Hash, or a Google::Auth::Credentials object. (See
97
+ # {Google::Cloud::Translate::V2::Credentials})
98
+ # @param [String] key a public API access key (not an OAuth 2.0 token)
99
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the set of resources and operations that
100
+ # the connection can access. See [Using OAuth 2.0 to Access Google
101
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
102
+ #
103
+ # The default scope is:
104
+ #
105
+ # * `https://www.googleapis.com/auth/cloud-platform`
106
+ # @param [Integer] retries Number of times to retry requests on server error. The default value is `3`.
107
+ # Optional.
108
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
109
+ # @param [String] endpoint Override of the endpoint host name. Optional. If the param is nil, uses the default
110
+ # endpoint.
111
+ #
112
+ # @example Using the v3 client.
113
+ # require "google/cloud/translate"
114
+ #
115
+ # client = Google::Cloud::Translate.new
116
+ #
117
+ # project_id = "my-project-id"
118
+ # location_id = "us-central1"
119
+ # model_id = "my-automl-model-id"
120
+ #
121
+ # # The `model` type requested for this translation.
122
+ # model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
123
+ # # The content to translate in string format
124
+ # contents = ["Hello, world!"]
125
+ # # Required. The BCP-47 language code to use for translation.
126
+ # target_language = "fr"
127
+ # # Optional. The BCP-47 language code of the input text.
128
+ # source_language = "en"
129
+ # # Optional. Can be "text/plain" or "text/html".
130
+ # mime_type = "text/plain"
131
+ # parent = client.class.location_path project_id, location_id
132
+ #
133
+ # response = client.translate_text contents, target_language, parent,
134
+ # source_language_code: source_language, model: model, mime_type: mime_type
135
+ #
136
+ # # Display the translation for each input text provided
137
+ # response.translations.each do |translation|
138
+ # puts "Translated text: #{translation.translated_text}"
139
+ # end
140
+ #
141
+ # @example Using the legacy v2 client.
80
142
  # require "google/cloud/translate"
81
143
  #
82
144
  # translate = Google::Cloud::Translate.new(
145
+ # version: :v2,
83
146
  # project_id: "my-todo-project",
84
147
  # credentials: "/path/to/keyfile.json"
85
148
  # )
@@ -87,10 +150,11 @@ module Google
87
150
  # translation = translate.translate "Hello world!", to: "la"
88
151
  # translation.text #=> "Salve mundi!"
89
152
  #
90
- # @example Using API Key.
153
+ # @example Using the legacy v2 client with an API Key.
91
154
  # require "google/cloud/translate"
92
155
  #
93
156
  # translate = Google::Cloud::Translate.new(
157
+ # version: :v2,
94
158
  # key: "api-key-abc123XYZ789"
95
159
  # )
96
160
  #
@@ -102,45 +166,23 @@ module Google
102
166
  #
103
167
  # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
104
168
  #
105
- # translate = Google::Cloud::Translate.new
169
+ # translate = Google::Cloud::Translate.new version: :v2
106
170
  #
107
171
  # translation = translate.translate "Hello world!", to: "la"
108
172
  # translation.text #=> "Salve mundi!"
109
173
  #
110
- def self.new project_id: nil, credentials: nil, key: nil, scope: nil,
111
- retries: nil, timeout: nil, endpoint: nil, project: nil,
112
- keyfile: nil
113
- project_id ||= (project || default_project_id)
114
- key ||= configure.key
115
- retries ||= configure.retries
116
- timeout ||= configure.timeout
117
- endpoint ||= configure.endpoint
118
-
119
- if key
120
- return Google::Cloud::Translate::Api.new(
121
- Google::Cloud::Translate::Service.new(
122
- project_id.to_s, nil,
123
- retries: retries, timeout: timeout, key: key, host: endpoint
124
- )
125
- )
174
+ def self.new *args, version: :v3, **kwargs
175
+ unless AVAILABLE_VERSIONS.include? version.to_s.downcase
176
+ raise "The version: #{version} is not available. The available versions " \
177
+ "are: [#{AVAILABLE_VERSIONS.join ', '}]"
126
178
  end
127
179
 
128
- scope ||= configure.scope
129
- credentials ||= keyfile || default_credentials(scope: scope)
130
-
131
- unless credentials.is_a? Google::Auth::Credentials
132
- credentials = Translate::Credentials.new credentials, scope: scope
133
- end
134
-
135
- project_id = resolve_project_id project_id, credentials
136
- raise ArgumentError, "project_id is missing" if project_id.empty?
137
-
138
- Translate::Api.new(
139
- Translate::Service.new(
140
- project_id, credentials,
141
- retries: retries, timeout: timeout, host: endpoint
142
- )
143
- )
180
+ require "#{FILE_DIR}/#{version.to_s.downcase}"
181
+ version_module = Google::Cloud::Translate
182
+ .constants
183
+ .select { |sym| sym.to_s.casecmp(version.to_s).zero? }
184
+ .first
185
+ Google::Cloud::Translate.const_get(version_module).new(*args, **kwargs)
144
186
  end
145
187
 
146
188
  ##
@@ -148,54 +190,25 @@ module Google
148
190
  #
149
191
  # The following Translate configuration parameters are supported:
150
192
  #
151
- # * `project_id` - (String) Identifier for a Translate project. (The
152
- # parameter `project` is considered deprecated, but may also be used.)
153
- # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
154
- # the keyfile as a String, the contents of the keyfile as a Hash, or a
155
- # Google::Auth::Credentials object. (See {Translate::Credentials}) (The
156
- # parameter `keyfile` is considered deprecated, but may also be used.)
157
- # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
158
- # the set of resources and operations that the connection can access.
159
- # * `retries` - (Integer) Number of times to retry requests on server
160
- # error.
193
+ # * `project_id` - (String) Identifier for a Translate project.
194
+ # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to the keyfile as a String, the contents of
195
+ # the keyfile as a Hash, or a Google::Auth::Credentials object. (See
196
+ # {Google::Cloud::Translate::V2::Credentials})
197
+ # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling the set of resources and operations that
198
+ # the connection can access.
199
+ # * `retries` - (Integer) Number of times to retry requests on server error.
161
200
  # * `timeout` - (Integer) Default timeout to use in requests.
162
- # * `endpoint` - (String) Override of the endpoint host name, or `nil`
163
- # to use the default endpoint.
201
+ # * `endpoint` - (String) Override of the endpoint host name, or `nil` to use the default endpoint.
202
+ #
203
+ # @note These values are only used by the legacy v2 client.
164
204
  #
165
- # @return [Google::Cloud::Config] The configuration object the
166
- # Google::Cloud::Translate library uses.
205
+ # @return [Google::Cloud::Config] The configuration object the Google::Cloud::Translate library uses.
167
206
  #
168
207
  def self.configure
169
208
  yield Google::Cloud.configure.translate if block_given?
170
209
 
171
210
  Google::Cloud.configure.translate
172
211
  end
173
-
174
- ##
175
- # @private Default project.
176
- def self.default_project_id
177
- Google::Cloud.configure.translate.project_id ||
178
- Google::Cloud.configure.project_id ||
179
- Google::Cloud.env.project_id
180
- end
181
-
182
- ##
183
- # @private Default credentials.
184
- def self.default_credentials scope: nil
185
- Google::Cloud.configure.translate.credentials ||
186
- Google::Cloud.configure.credentials ||
187
- Translate::Credentials.default(scope: scope)
188
- end
189
-
190
- ##
191
- # @private Resolve project.
192
- def self.resolve_project_id project_id, credentials
193
- # Always cast to a string
194
- return project_id.to_s unless credentials.respond_to? :project_id
195
-
196
- # Always cast to a string
197
- project_id || credentials.project_id.to_s
198
- end
199
212
  end
200
213
  end
201
214
  end
@@ -0,0 +1,169 @@
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-translate"
17
+ require "google/cloud/translate/v2/api"
18
+ require "google/cloud/config"
19
+ require "google/cloud/env"
20
+
21
+ module Google
22
+ module Cloud
23
+ module Translate
24
+ ##
25
+ # # Google Cloud Translation API
26
+ #
27
+ # [Google Cloud Translation API](https://cloud.google.com/translation/)
28
+ # provides a simple, programmatic interface for translating an arbitrary
29
+ # string into any supported language. It is highly responsive, so websites
30
+ # and applications can integrate with Translation API for fast, dynamic
31
+ # translation of source text. Language detection is also available in cases
32
+ # where the source language is unknown.
33
+ #
34
+ # Translation API supports more than one hundred different languages, from
35
+ # Afrikaans to Zulu. Used in combination, this enables translation between
36
+ # thousands of language pairs. Also, you can send in HTML and receive HTML
37
+ # with translated text back. You don't need to extract your source text or
38
+ # reassemble the translated content.
39
+ #
40
+ # See {file:OVERVIEW.md Translation Overview}.
41
+ #
42
+ module V2
43
+ ##
44
+ # Creates a new object for connecting to Cloud Translation API. Each call creates a new connection.
45
+ #
46
+ # Like other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID
47
+ # and OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the
48
+ # API key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and
49
+ # configuration options are covered in the {file:AUTHENTICATION.md Authentication Guide}.
50
+ #
51
+ # @param [String] project_id Project identifier for the Cloud Translation service you are connecting to. If not
52
+ # present, the default project for the credentials is used.
53
+ # @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents
54
+ # of the keyfile as a Hash, or a Google::Auth::Credentials object. (See {Translate::V2::Credentials})
55
+ # @param [String] key a public API access key (not an OAuth 2.0 token)
56
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the set of resources and operations that
57
+ # the connection can access. See [Using OAuth 2.0 to Access Google
58
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
59
+ #
60
+ # The default scope is:
61
+ #
62
+ # * `https://www.googleapis.com/auth/cloud-platform`
63
+ # @param [Integer] retries Number of times to retry requests on server error. The default value is `3`.
64
+ # Optional.
65
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
66
+ # @param [String] endpoint Override of the endpoint host name. Optional. If the param is nil, uses the default
67
+ # endpoint.
68
+ #
69
+ # @return [Google::Cloud::Translate::V2::Api]
70
+ #
71
+ # @example
72
+ # require "google/cloud/translate/v2"
73
+ #
74
+ # translate = Google::Cloud::Translate::V2.new(
75
+ # version: :v2,
76
+ # project_id: "my-todo-project",
77
+ # credentials: "/path/to/keyfile.json"
78
+ # )
79
+ #
80
+ # translation = translate.translate "Hello world!", to: "la"
81
+ # translation.text #=> "Salve mundi!"
82
+ #
83
+ # @example Using API Key.
84
+ # require "google/cloud/translate/v2"
85
+ #
86
+ # translate = Google::Cloud::Translate::V2.new(
87
+ # key: "api-key-abc123XYZ789"
88
+ # )
89
+ #
90
+ # translation = translate.translate "Hello world!", to: "la"
91
+ # translation.text #=> "Salve mundi!"
92
+ #
93
+ # @example Using API Key from the environment variable.
94
+ # require "google/cloud/translate/v2"
95
+ #
96
+ # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
97
+ #
98
+ # translate = Google::Cloud::Translate::V2.new
99
+ #
100
+ # translation = translate.translate "Hello world!", to: "la"
101
+ # translation.text #=> "Salve mundi!"
102
+ #
103
+ def self.new project_id: nil, credentials: nil, key: nil, scope: nil, retries: nil, timeout: nil, endpoint: nil
104
+ project_id ||= default_project_id
105
+ key ||= Google::Cloud.configure.translate.key
106
+ retries ||= Google::Cloud.configure.translate.retries
107
+ timeout ||= Google::Cloud.configure.translate.timeout
108
+ endpoint ||= Google::Cloud.configure.translate.endpoint
109
+
110
+ if key
111
+ return Google::Cloud::Translate::V2::Api.new(
112
+ Google::Cloud::Translate::V2::Service.new(
113
+ project_id.to_s, nil, retries: retries, timeout: timeout, key: key, host: endpoint
114
+ )
115
+ )
116
+ end
117
+
118
+ scope ||= Google::Cloud.configure.translate.scope
119
+ credentials ||= default_credentials scope: scope
120
+
121
+ unless credentials.is_a? Google::Auth::Credentials
122
+ credentials = Google::Cloud::Translate::V2::Credentials.new credentials, scope: scope
123
+ end
124
+
125
+ project_id = resolve_project_id project_id, credentials
126
+ raise ArgumentError, "project_id is missing" if project_id.empty?
127
+
128
+ Google::Cloud::Translate::V2::Api.new(
129
+ Google::Cloud::Translate::V2::Service.new(
130
+ project_id, credentials, retries: retries, timeout: timeout, host: endpoint
131
+ )
132
+ )
133
+ end
134
+
135
+ ##
136
+ # @private Default project.
137
+ def self.default_project_id
138
+ Google::Cloud.configure.translate.project_id ||
139
+ Google::Cloud.configure.project_id ||
140
+ Google::Cloud.env.project_id
141
+ end
142
+
143
+ ##
144
+ # @private Default credentials.
145
+ def self.default_credentials scope: nil
146
+ Google::Cloud.configure.translate.credentials ||
147
+ Google::Cloud.configure.credentials ||
148
+ Google::Cloud::Translate::V2::Credentials.default(scope: scope)
149
+ end
150
+
151
+ ##
152
+ # @private Default configuration.
153
+ def self.default_config
154
+ Google::Cloud.configure
155
+ end
156
+
157
+ ##
158
+ # @private Resolve project.
159
+ def self.resolve_project_id project_id, credentials
160
+ # Always cast to a string
161
+ return project_id.to_s unless credentials.respond_to? :project_id
162
+
163
+ # Always cast to a string
164
+ project_id || credentials.project_id.to_s
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end