Class: Firstclasspostcodes::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/firstclasspostcodes/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

Instance Attribute Details

#api_keyString

Defines API keys used with API Key authentications.

Examples:

parameter name is “api_key”, API key is “xxx”

config.api_key['api_key'] = 'xxx'

Returns:

  • (String)

    the value of the API key being used



11
12
13
# File 'lib/firstclasspostcodes/configuration.rb', line 11

def api_key
  @api_key
end

#base_pathObject

Defines url base path



23
24
25
# File 'lib/firstclasspostcodes/configuration.rb', line 23

def base_path
  @base_path
end

#cert_fileObject

TLS/SSL setting Client certificate file (for client certificate)



71
72
73
# File 'lib/firstclasspostcodes/configuration.rb', line 71

def cert_file
  @cert_file
end

#contentObject

Defines the content type requested and returned



17
18
19
# File 'lib/firstclasspostcodes/configuration.rb', line 17

def content
  @content
end

#debugtrue, false

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.

Returns:

  • (true, false)


36
37
38
# File 'lib/firstclasspostcodes/configuration.rb', line 36

def debug
  @debug
end

#hostObject

Defines url host



14
15
16
# File 'lib/firstclasspostcodes/configuration.rb', line 14

def host
  @host
end

#key_fileObject

TLS/SSL setting Client private key file (for client certificate)



75
76
77
# File 'lib/firstclasspostcodes/configuration.rb', line 75

def key_file
  @key_file
end

#logger#debug

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

Returns:



29
30
31
# File 'lib/firstclasspostcodes/configuration.rb', line 29

def logger
  @logger
end

#protocolObject

Defines HTTP protocol to be used



20
21
22
# File 'lib/firstclasspostcodes/configuration.rb', line 20

def protocol
  @protocol
end

#ssl_ca_certString

TLS/SSL setting Set this to customize the certificate file to verify the peer.

github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145

Returns:

  • (String)

    the path to the certificate file

See Also:

  • `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:


67
68
69
# File 'lib/firstclasspostcodes/configuration.rb', line 67

def ssl_ca_cert
  @ssl_ca_cert
end

#timeoutObject

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



40
41
42
# File 'lib/firstclasspostcodes/configuration.rb', line 40

def timeout
  @timeout
end

#verify_ssltrue, false

Note:

Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.

TLS/SSL setting Set this to false to skip verifying SSL certificate when calling API from https server. Default to true.

Returns:

  • (true, false)


49
50
51
# File 'lib/firstclasspostcodes/configuration.rb', line 49

def verify_ssl
  @verify_ssl
end

#verify_ssl_hosttrue, false

Note:

Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.

TLS/SSL setting Set this to false to skip verifying SSL host name Default to true.

Returns:

  • (true, false)


58
59
60
# File 'lib/firstclasspostcodes/configuration.rb', line 58

def verify_ssl_host
  @verify_ssl_host
end

Class Method Details

.defaultObject

The default Configuration object.



94
95
96
# File 'lib/firstclasspostcodes/configuration.rb', line 94

def self.default
  @default ||= Configuration.new
end

Instance Method Details

#base_urlObject



132
133
134
135
# 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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



98
99
100
# File 'lib/firstclasspostcodes/configuration.rb', line 98

def configure
  yield(self) if block_given?
end

#debug?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/firstclasspostcodes/configuration.rb', line 102

def debug?
  debug
end

#geo_json?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/firstclasspostcodes/configuration.rb', line 106

def geo_json?
  content == "geo+json"
end

#to_request_paramsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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