lib/signet/oauth_1/client.rb in signet-0.14.1 vs lib/signet/oauth_1/client.rb in signet-0.15.0
- old
+ new
@@ -87,11 +87,11 @@
# )
#
# @see Signet::OAuth1::Client#initialize
def update! options = {}
# Normalize key to String to allow indifferent access.
- options = options.each_with_object({}) { |(k, v), accu| accu[k.to_s] = v; }
+ options = options.to_h.transform_keys(&:to_s)
self.temporary_credential_uri = options["temporary_credential_uri"]
self.authorization_uri = options["authorization_uri"]
self.token_credential_uri = options["token_credential_uri"]
# Technically... this would allow you to pass in a :client key...
# But that would be weird. Don't do that.
@@ -126,16 +126,16 @@
#
# @param [Addressable::URI, String, #to_str]
# new_temporary_credential_uri
# The temporary credentials URI.
def temporary_credential_uri= new_temporary_credential_uri
- if !new_temporary_credential_uri.nil?
+ if new_temporary_credential_uri.nil?
+ @temporary_credential_uri = nil
+ else
new_temporary_credential_uri =
Addressable::URI.parse new_temporary_credential_uri
@temporary_credential_uri = new_temporary_credential_uri
- else
- @temporary_credential_uri = nil
end
end
alias request_token_uri= temporary_credential_uri=
##
@@ -161,18 +161,18 @@
# Sets the authorization URI for this client.
#
# @param [Addressable::URI, String, #to_str] new_authorization_uri
# The authorization URI.
def authorization_uri= new_authorization_uri
- if !new_authorization_uri.nil?
+ if new_authorization_uri.nil?
+ @authorization_uri = nil
+ else
new_authorization_uri = Addressable::URI.send(
new_authorization_uri.is_a?(Hash) ? :new : :parse,
new_authorization_uri
)
@authorization_uri = new_authorization_uri
- else
- @authorization_uri = nil
end
end
##
# Returns the token credential URI for this client.
@@ -187,18 +187,18 @@
# Sets the token credential URI for this client.
#
# @param [Addressable::URI, Hash, String, #to_str] new_token_credential_uri
# The token credential URI.
def token_credential_uri= new_token_credential_uri
- if !new_token_credential_uri.nil?
+ if new_token_credential_uri.nil?
+ @token_credential_uri = nil
+ else
new_token_credential_uri = Addressable::URI.send(
new_token_credential_uri.is_a?(Hash) ? :new : :parse,
new_token_credential_uri
)
@token_credential_uri = new_token_credential_uri
- else
- @token_credential_uri = nil
end
end
alias access_token_uri= token_credential_uri=
# Lots of duplicated code here, but for the sake of auto-generating
@@ -215,33 +215,32 @@
client_credential_secret
)
elsif !client_credential_key && !client_credential_secret
nil
else
- raise ArgumentError,
- "The client credential key and secret must be set."
+ raise ArgumentError, "The client credential key and secret must be set."
end
end
alias consumer_token client_credential
##
# Sets the client credential for this client.
#
# @param [Signet::OAuth1::Credential] new_client_credential
# The client credentials.
def client_credential= new_client_credential
- if !new_client_credential.nil?
+ if new_client_credential.nil?
+ @client_credential_key = nil
+ @client_credential_secret = nil
+ else
unless new_client_credential.is_a? ::Signet::OAuth1::Credential
raise TypeError,
"Expected Signet::OAuth1::Credential, " \
"got #{new_client_credential.class}."
end
@client_credential_key = new_client_credential.key
@client_credential_secret = new_client_credential.secret
- else
- @client_credential_key = nil
- @client_credential_secret = nil
end
end
alias consumer_token= client_credential=
##
@@ -257,19 +256,19 @@
# Sets the client credential key for this client.
#
# @param [String, #to_str] new_client_credential_key
# The client credential key.
def client_credential_key= new_client_credential_key
- if !new_client_credential_key.nil?
+ if new_client_credential_key.nil?
+ @client_credential_key = nil
+ else
unless new_client_credential_key.respond_to? :to_str
raise TypeError,
"Can't convert #{new_client_credential_key.class} into String."
end
new_client_credential_key = new_client_credential_key.to_str
@client_credential_key = new_client_credential_key
- else
- @client_credential_key = nil
end
end
alias consumer_key= client_credential_key=
##
@@ -285,20 +284,20 @@
# Sets the client credential secret for this client.
#
# @param [String, #to_str] new_client_credential_secret
# The client credential secret.
def client_credential_secret= new_client_credential_secret
- if !new_client_credential_secret.nil?
+ if new_client_credential_secret.nil?
+ @client_credential_secret = nil
+ else
unless new_client_credential_secret.respond_to? :to_str
raise TypeError,
"Can't convert #{new_client_credential_secret.class} " \
"into String."
end
new_client_credential_secret = new_client_credential_secret.to_str
@client_credential_secret = new_client_credential_secret
- else
- @client_credential_secret = nil
end
end
alias consumer_secret= client_credential_secret=
##
@@ -309,37 +308,35 @@
if temporary_credential_key && temporary_credential_secret
::Signet::OAuth1::Credential.new(
temporary_credential_key,
temporary_credential_secret
)
- elsif !temporary_credential_key &&
- !temporary_credential_secret
+ elsif !temporary_credential_key && !temporary_credential_secret
nil
else
- raise ArgumentError,
- "The temporary credential key and secret must be set."
+ raise ArgumentError, "The temporary credential key and secret must be set."
end
end
alias request_token temporary_credential
##
# Sets the temporary credential for this client.
#
# @param [Signet::OAuth1::Credential] new_temporary_credential
# The temporary credentials.
def temporary_credential= new_temporary_credential
- if !new_temporary_credential.nil?
+ if new_temporary_credential.nil?
+ @temporary_credential_key = nil
+ @temporary_credential_secret = nil
+ else
unless new_temporary_credential.is_a? ::Signet::OAuth1::Credential
raise TypeError,
"Expected Signet::OAuth1::Credential, " \
"got #{new_temporary_credential.class}."
end
@temporary_credential_key = new_temporary_credential.key
@temporary_credential_secret = new_temporary_credential.secret
- else
- @temporary_credential_key = nil
- @temporary_credential_secret = nil
end
end
alias request_token= temporary_credential=
##
@@ -355,20 +352,20 @@
# Sets the temporary credential key for this client.
#
# @param [String, #to_str] new_temporary_credential_key
# The temporary credential key.
def temporary_credential_key= new_temporary_credential_key
- if !new_temporary_credential_key.nil?
+ if new_temporary_credential_key.nil?
+ @temporary_credential_key = nil
+ else
unless new_temporary_credential_key.respond_to? :to_str
raise TypeError,
"Can't convert #{new_temporary_credential_key.class} " \
"into String."
end
new_temporary_credential_key = new_temporary_credential_key.to_str
@temporary_credential_key = new_temporary_credential_key
- else
- @temporary_credential_key = nil
end
end
alias request_token_key= temporary_credential_key=
##
@@ -384,21 +381,21 @@
# Sets the temporary credential secret for this client.
#
# @param [String, #to_str] new_temporary_credential_secret
# The temporary credential secret.
def temporary_credential_secret= new_temporary_credential_secret
- if !new_temporary_credential_secret.nil?
+ if new_temporary_credential_secret.nil?
+ @temporary_credential_secret = nil
+ else
unless new_temporary_credential_secret.respond_to? :to_str
raise TypeError,
"Can't convert #{new_temporary_credential_secret.class} " \
"into String."
end
new_temporary_credential_secret =
new_temporary_credential_secret.to_str
@temporary_credential_secret = new_temporary_credential_secret
- else
- @temporary_credential_secret = nil
end
end
alias request_token_secret= temporary_credential_secret=
##
@@ -409,37 +406,35 @@
if token_credential_key && token_credential_secret
::Signet::OAuth1::Credential.new(
token_credential_key,
token_credential_secret
)
- elsif !token_credential_key &&
- !token_credential_secret
+ elsif !token_credential_key && !token_credential_secret
nil
else
- raise ArgumentError,
- "The token credential key and secret must be set."
+ raise ArgumentError, "The token credential key and secret must be set."
end
end
alias access_token token_credential
##
# Sets the token credential for this client.
#
# @param [Signet::OAuth1::Credential] new_token_credential
# The token credentials.
def token_credential= new_token_credential
- if !new_token_credential.nil?
+ if new_token_credential.nil?
+ @token_credential_key = nil
+ @token_credential_secret = nil
+ else
unless new_token_credential.is_a? ::Signet::OAuth1::Credential
raise TypeError,
"Expected Signet::OAuth1::Credential, " \
"got #{new_token_credential.class}."
end
@token_credential_key = new_token_credential.key
@token_credential_secret = new_token_credential.secret
- else
- @token_credential_key = nil
- @token_credential_secret = nil
end
end
alias access_token= token_credential=
##
@@ -455,20 +450,20 @@
# Sets the token credential key for this client.
#
# @param [String, #to_str] new_token_credential_key
# The token credential key.
def token_credential_key= new_token_credential_key
- if !new_token_credential_key.nil?
+ if new_token_credential_key.nil?
+ @token_credential_key = nil
+ else
unless new_token_credential_key.respond_to? :to_str
raise TypeError,
"Can't convert #{new_token_credential_key.class} " \
"into String."
end
new_token_credential_key = new_token_credential_key.to_str
@token_credential_key = new_token_credential_key
- else
- @token_credential_key = nil
end
end
alias access_token_key= token_credential_key=
##
@@ -484,21 +479,21 @@
# Sets the token credential secret for this client.
#
# @param [String, #to_str] new_token_credential_secret
# The token credential secret.
def token_credential_secret= new_token_credential_secret
- if !new_token_credential_secret.nil?
+ if new_token_credential_secret.nil?
+ @token_credential_secret = nil
+ else
unless new_token_credential_secret.respond_to? :to_str
raise TypeError,
"Can't convert #{new_token_credential_secret.class} " \
"into String."
end
new_token_credential_secret =
new_token_credential_secret.to_str
@token_credential_secret = new_token_credential_secret
- else
- @token_credential_secret = nil
end
end
alias access_token_secret= token_credential_secret=
##
@@ -513,19 +508,19 @@
# Sets the callback for this client.
#
# @param [String, #to_str] new_callback
# The OAuth callback.
def callback= new_callback
- if !new_callback.nil?
+ if new_callback.nil?
+ @callback = nil
+ else
unless new_callback.respond_to? :to_str
raise TypeError,
"Can't convert #{new_callback.class} into String."
end
new_callback = new_callback.to_str
@callback = new_callback
- else
- @callback = nil
end
end
##
# Returns whether the client is in two-legged mode.
@@ -554,11 +549,11 @@
# Serialize the client object to JSON.
#
# @note A serialized client contains sensitive information. Persist or transmit with care.
#
# @return [String] A serialized JSON representation of the client.
- def to_json
+ def to_json *_args
MultiJson.dump(
"temporary_credential_uri" => temporary_credential_uri,
"authorization_uri" => authorization_uri,
"token_credential_uri" => token_credential_uri,
"callback" => callback,
@@ -569,12 +564,10 @@
"temporary_credential_secret" => temporary_credential_secret,
"token_credential_key" => token_credential_key,
"token_credential_secret" => token_credential_secret
)
end
- # rubocop:disable Metrics/AbcSize
- # rubocop:disable Metrics/MethodLength
##
# Generates a request for temporary credentials.
#
# @param [Hash] options
@@ -633,12 +626,10 @@
temporary_credential_uri.to_str
).normalize.to_s)
req.headers = Faraday::Utils::Headers.new headers
end
end
- # rubocop:enable Metrics/AbcSize
- # rubocop:enable Metrics/MethodLength
alias generate_request_token_request generate_temporary_credential_request
##
# Transmits a request for a temporary credential. This method does not
# have side-effects within the client.
@@ -707,12 +698,10 @@
def fetch_temporary_credential! options = {}
credential = fetch_temporary_credential options
self.temporary_credential = credential
end
alias fetch_request_token! fetch_temporary_credential!
- # rubocop:disable Metrics/AbcSize
- # rubocop:disable Metrics/MethodLength
##
# Generates a request for token credentials.
#
# @param [Hash] options
@@ -774,11 +763,10 @@
token_credential_uri.to_str
).normalize.to_s)
req.headers = Faraday::Utils::Headers.new headers
end
end
- # rubocop:enable Metrics/MethodLength
alias generate_access_token_request generate_token_credential_request
##
# Transmits a request for a token credential. This method does not
# have side-effects within the client.
@@ -816,11 +804,10 @@
message += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty?
raise ::Signet::AuthorizationError.new(
message, request: request, response: response
)
end
- # rubocop:enable Metrics/AbcSize
alias fetch_access_token fetch_token_credential
##
# Transmits a request for a token credential. This method updates
# the client with the new token credential.
@@ -844,14 +831,10 @@
def fetch_token_credential! options = {}
credential = fetch_token_credential options
self.token_credential = credential
end
alias fetch_access_token! fetch_token_credential!
- # rubocop:disable Metrics/AbcSize
- # rubocop:disable Metrics/CyclomaticComplexity
- # rubocop:disable Metrics/MethodLength
- # rubocop:disable Metrics/PerceivedComplexity
##
# Generates an authenticated request for protected resources.
#
# @param [Hash] options
@@ -948,11 +931,11 @@
encoder = Faraday::Request::UrlEncoded.new(->(_env) {})
encoder.call env
request.body = env[:body]
post_parameters = Addressable::URI.form_unencode env[:body]
- parameters = parameters.concat post_parameters
+ parameters.concat post_parameters
end
# No need to attach URI query parameters, the .sign_parameters
# method takes care of that automatically.
signature = ::Signet::OAuth1.sign_parameters(
@@ -968,13 +951,9 @@
parameters, options[:realm]
)
request["Cache-Control"] = "no-store"
request
end
- # rubocop:enable Metrics/AbcSize
- # rubocop:enable Metrics/CyclomaticComplexity
- # rubocop:enable Metrics/MethodLength
- # rubocop:enable Metrics/PerceivedComplexity
##
# Transmits a request for a protected resource.
#
# @param [Hash] options