class Firstclasspostcodes::Configuration
Attributes
Defines API keys used with API Key authentications.
@return [String] the value of the API key being used
@example parameter name is “api_key”, API key is “xxx”
config.api_key['api_key'] = 'xxx'
Defines url base path
TLS/SSL setting
Client
certificate file (for client certificate)
Defines the content type requested and returned
Set this to enable/disable debugging. When enabled (set to true), HTTP request/response details will be logged with `logger.debug` (see the `logger` attribute). Default to false.
@return [true, false]
Defines url host
TLS/SSL setting
Client
private key file (for client certificate)
Defines the logger used for debugging. Default to `Rails.logger` (when in Rails) or logging to STDOUT.
@return [#debug]
Defines HTTP protocol to be used
TLS/SSL setting
Set this to customize the certificate file to verify the peer.
@return [String] the path to the certificate file
@see The `cainfo` option of Typhoeus, `–cert` option of libcurl. Related source code: github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
The time limit for HTTP request in seconds. Default to 0 (never times out).
TLS/SSL setting
Set this to false to skip verifying SSL certificate when calling API from https server. Default to true.
@note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
@return [true, false]
TLS/SSL setting
Set this to false to skip verifying SSL host name Default to true.
@note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
@return [true, false]
Public Class Methods
The default Configuration
object.
# File lib/firstclasspostcodes/configuration.rb, line 94 def self.default @default ||= Configuration.new end
# File lib/firstclasspostcodes/configuration.rb, line 77 def initialize @api_key = nil @debug = false @host = "api.firstclasspostcodes.com" @content = "json" @protocol = "https" @base_path = "/data" @timeout = 30 @verify_ssl = true @verify_ssl_host = true @cert_file = nil @key_file = nil @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) yield(self) if block_given? end
Public Instance Methods
# File lib/firstclasspostcodes/configuration.rb, line 126 def base_path=(base_path) # Add leading and trailing slashes to base_path @base_path = "/#{base_path}".gsub(%r{/+}, "/") @base_path = "" if @base_path == "/" end
# File lib/firstclasspostcodes/configuration.rb, line 132 def base_url path = [host, base_path].join("/").gsub(%r{/+}, "/") "#{protocol}://#{path}".sub(%r{/+\z}, "") end
# File lib/firstclasspostcodes/configuration.rb, line 98 def configure yield(self) if block_given? end
# File lib/firstclasspostcodes/configuration.rb, line 110 def content=(content) raise StandardError, `"#{content}" is not a valid content-type` unless %w[json geo+json].include?(content) @content = content end
# File lib/firstclasspostcodes/configuration.rb, line 102 def debug? debug end
# File lib/firstclasspostcodes/configuration.rb, line 106 def geo_json? content == "geo+json" end
# File lib/firstclasspostcodes/configuration.rb, line 121 def host=(host) # remove http(s):// and anything after a slash @host = host.sub(%r{https?://}, "").split("/").first end
# File lib/firstclasspostcodes/configuration.rb, line 116 def protocol=(protocol) # remove :// from protocol @protocol = protocol.sub(%r{://}, "") end
# File lib/firstclasspostcodes/configuration.rb, line 137 def to_request_params params = { headers: { 'x-api-key': api_key, accept: "application/#{content}; q=1.0, application/json; q=0.5", }, timeout: timeout, ssl_verifypeer: verify_ssl, ssl_verifyhost: verify_ssl_host ? 2 : 0, sslcert: cert_file, sslkey: key_file, verbose: debug, } params[:cainfo] = ssl_ca_cert if ssl_ca_cert params end