module DeskApi::Configuration
{DeskApi::Configuration} allows to configure a {DeskApi::Client}. It exposes all available configuration options to the client and makes sure secrets are only readable by the client.
@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License
Attributes
Public Class Methods
Registers the middleware when the module is included.
# File lib/desk_api/configuration.rb, line 87 def included(_base) register_middleware :request, :desk_encode_dates, :EncodeDates register_middleware :request, :desk_encode_json, :EncodeJson register_middleware :request, :desk_oauth, :OAuth register_middleware :request, :desk_retry, :Retry register_middleware :response, :desk_parse_dates, :ParseDates register_middleware :response, :desk_parse_json, :ParseJson register_middleware :response, :desk_raise_error, :RaiseError register_middleware :response, :desk_follow_redirects, :FollowRedirects end
Returns an array of possible configuration options.
@return [Array]
# File lib/desk_api/configuration.rb, line 63 def keys @keys ||= [ :consumer_key, :consumer_secret, :token, :token_secret, :username, :password, :subdomain, :endpoint, :connection_options ] end
Allows to register middleware for Faraday v0.8 and v0.9
@param type [Symbol] either :request or :response @param sym [Symbol] the symbol to register the middleware as @param cls [Symbol] the class name of the middleware
# File lib/desk_api/configuration.rb, line 77 def register_middleware(type, sym, cls) cls = DeskApi.const_get(type.capitalize).const_get(cls) if Faraday.respond_to?(:register_middleware) Faraday.register_middleware type, sym => cls else Faraday.const_get(type.capitalize).register_middleware sym => cls end end
Public Instance Methods
Allows to configure the client by yielding self.
@yield [DeskApi::Client] @return [DeskApi::Client]
# File lib/desk_api/configuration.rb, line 130 def configure yield self validate_credentials! validate_endpoint! self end
Returns true if either all oauth values or all basic auth values are set.
@return [Boolean]
# File lib/desk_api/configuration.rb, line 152 def credentials? oauth.values.all? || basic_auth.values.all? end
Resets the client to the default settings.
@return [DeskApi::Client]
# File lib/desk_api/configuration.rb, line 140 def reset! DeskApi::Configuration.keys.each do |key| send("#{key}=", DeskApi::Default.options[key]) end self end
Private Instance Methods
Returns the basic auth configuration options.
@return [Hash]
# File lib/desk_api/configuration.rb, line 184 def basic_auth { username: @username, password: @password } end
Returns the oauth configuration options.
@return [Hash]
# File lib/desk_api/configuration.rb, line 172 def oauth { consumer_key: @consumer_key, consumer_secret: @consumer_secret, token: @token, token_secret: @token_secret } end
Returns a hash of current configuration options.
@return [Hash]
# File lib/desk_api/configuration.rb, line 161 def options Hash[ DeskApi::Configuration.keys.map do |key| [key, instance_variable_get(:"@#{key}")] end ] end
Raises an error if credentials are not set or of the wrong type.
@raise [DeskApi::Error::ConfigurationError]
# File lib/desk_api/configuration.rb, line 207 def validate_credentials! fail( DeskApi::Error::ConfigurationError, 'Invalid credentials: ' \ 'Either username/password or OAuth credentials must be specified.' ) unless credentials? validate_oauth! if oauth.values.all? validate_basic_auth! if basic_auth.values.all? end
Raises an error if the endpoint is not a valid URL.
@raises [DeskApi::Error::ConfigurationError]
# File lib/desk_api/configuration.rb, line 236 def validate_endpoint! fail( DeskApi::Error::ConfigurationError, "Invalid endpoint specified: `#{endpoint}` must be a valid url." ) unless endpoint =~ /^#{URI.regexp}$/ end