class Firstclasspostcodes::Configuration

Attributes

api_key[RW]

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'
base_path[R]

Defines url base path

cert_file[RW]
TLS/SSL setting

Client certificate file (for client certificate)

content[R]

Defines the content type requested and returned

debug[RW]

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]

host[R]

Defines url host

key_file[RW]
TLS/SSL setting

Client private key file (for client certificate)

logger[RW]

Defines the logger used for debugging. Default to `Rails.logger` (when in Rails) or logging to STDOUT.

@return [#debug]

protocol[R]

Defines HTTP protocol to be used

ssl_ca_cert[RW]
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

timeout[RW]

The time limit for HTTP request in seconds. Default to 0 (never times out).

verify_ssl[RW]
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]

verify_ssl_host[RW]
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

default() click to toggle source

The default Configuration object.

# File lib/firstclasspostcodes/configuration.rb, line 94
def self.default
  @default ||= Configuration.new
end
new() { |self| ... } click to toggle source
# 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

base_path=(base_path) click to toggle source
# 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
base_url() click to toggle source
# File lib/firstclasspostcodes/configuration.rb, line 132
def base_url
  path = [host, base_path].join("/").gsub(%r{/+}, "/")
  "#{protocol}://#{path}".sub(%r{/+\z}, "")
end
configure() { |self| ... } click to toggle source
# File lib/firstclasspostcodes/configuration.rb, line 98
def configure
  yield(self) if block_given?
end
content=(content) click to toggle source
# 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
debug?() click to toggle source
# File lib/firstclasspostcodes/configuration.rb, line 102
def debug?
  debug
end
geo_json?() click to toggle source
# File lib/firstclasspostcodes/configuration.rb, line 106
def geo_json?
  content == "geo+json"
end
host=(host) click to toggle source
# 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
protocol=(protocol) click to toggle source
# File lib/firstclasspostcodes/configuration.rb, line 116
def protocol=(protocol)
  # remove :// from protocol
  @protocol = protocol.sub(%r{://}, "")
end
to_request_params() click to toggle source
# 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