lib/google/cloud/translate.rb in google-cloud-translate-1.1.0 vs lib/google/cloud/translate.rb in google-cloud-translate-1.2.0
- old
+ new
@@ -1,22 +1,24 @@
-# Copyright 2016 Google Inc. All rights reserved.
+# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
-# http://www.apache.org/licenses/LICENSE-2.0
+# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "google-cloud-translate"
require "google/cloud/translate/api"
+require "google/cloud/config"
+require "google/cloud/env"
module Google
module Cloud
##
# # Google Cloud Translation API
@@ -273,31 +275,78 @@
# translation = translate.translate "Hello world!", to: "la"
# translation.text #=> "Salve mundi!"
#
def self.new project_id: nil, credentials: nil, key: nil, scope: nil,
retries: nil, timeout: nil, project: nil, keyfile: nil
- project_id ||= (project || Translate::Api.default_project_id)
+ project_id ||= (project || default_project_id)
project_id = project_id.to_s # Always cast to a string
- key ||= ENV["TRANSLATE_KEY"]
- key ||= ENV["GOOGLE_CLOUD_KEY"]
+ key ||= configure.key
if key
return Google::Cloud::Translate::Api.new(
Google::Cloud::Translate::Service.new(
- project_id, nil, retries: retries, timeout: timeout, key: key))
+ project_id, nil, retries: retries, timeout: timeout, key: key
+ )
+ )
end
- fail ArgumentError, "project_id is missing" if project_id.empty?
+ raise ArgumentError, "project_id is missing" if project_id.empty?
- credentials ||= keyfile
- credentials ||= Translate::Credentials.default(scope: scope)
+ scope ||= configure.scope
+ retries ||= configure.retries
+ timeout ||= configure.timeout
+ credentials ||= keyfile || default_credentials(scope: scope)
unless credentials.is_a? Google::Auth::Credentials
credentials = Translate::Credentials.new credentials, scope: scope
end
Translate::Api.new(
Translate::Service.new(
- project_id, credentials, retries: retries, timeout: timeout))
+ project_id, credentials, retries: retries, timeout: timeout
+ )
+ )
+ end
+
+ ##
+ # Configure the Google Cloud Translate library.
+ #
+ # The following Translate configuration parameters are supported:
+ #
+ # * `project_id` - (String) Identifier for a Translate project. (The
+ # parameter `project` is considered deprecated, but may also be used.)
+ # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
+ # the keyfile as a String, the contents of the keyfile as a Hash, or a
+ # Google::Auth::Credentials object. (See {Translate::Credentials}) (The
+ # parameter `keyfile` is considered deprecated, but may also be used.)
+ # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
+ # the set of resources and operations that the connection can access.
+ # * `retries` - (Integer) Number of times to retry requests on server
+ # error.
+ # * `timeout` - (Integer) Default timeout to use in requests.
+ #
+ # @return [Google::Cloud::Config] The configuration object the
+ # Google::Cloud::Translate library uses.
+ #
+ def self.configure
+ yield Google::Cloud.configure.translate if block_given?
+
+ Google::Cloud.configure.translate
+ end
+
+ ##
+ # @private Default project.
+ def self.default_project_id
+ Google::Cloud.configure.translate.project_id ||
+ Google::Cloud.configure.project_id ||
+ Google::Cloud.env.project_id
+ end
+
+ ##
+ # @private Default credentials.
+ def self.default_credentials scope: nil
+ Google::Cloud.configure.translate.credentials ||
+ Google::Cloud.configure.credentials ||
+ Translate::Credentials.default(scope: scope)
end
end
end
end