Class: Castle::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/castle/configuration.rb

Overview

manages configuration variables

Constant Summary collapse

HOST =
'api.castle.io'
PORT =
443
URL_PREFIX =
'/v1'
FAILOVER_STRATEGY =
:allow
REQUEST_TIMEOUT =

in milliseconds

500
FAILOVER_STRATEGIES =
i[allow deny challenge throw].freeze
TRUSTED_PROXIES =

regexp of trusted proxies which is always appended to the trusted proxy list

[/
  \A127\.0\.0\.1\Z|
  \A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|
  \A::1\Z|\Afd[0-9a-f]{2}:.+|
  \Alocalhost\Z|
  \Aunix\Z|
  \Aunix:
/ix].freeze
DEFAULT_WHITELIST =
Note:

this value is not assigned as we don’t recommend using a whitelist. If you need to use one, this constant is provided as a good default.

%w[
  Accept
  Accept-Charset
  Accept-Datetime
  Accept-Encoding
  Accept-Language
  Cache-Control
  Connection
  Content-Length
  Content-Type
  Host
  Origin
  Pragma
  Referer
  TE
  Upgrade-Insecure-Requests
  X-Castle-Client-Id
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



48
49
50
51
52
# File 'lib/castle/configuration.rb', line 48

def initialize
  @formatter = Castle::HeadersFormatter
  @request_timeout = REQUEST_TIMEOUT
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(setting, *_args) ⇒ Object (private)



109
110
111
# File 'lib/castle/configuration.rb', line 109

def method_missing(setting, *_args)
  raise Castle::ConfigurationError, "there is no such a config #{setting}"
end

Instance Attribute Details

#api_secretObject

Returns the value of attribute api_secret.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def api_secret
  @api_secret
end

#blacklistedObject

Returns the value of attribute blacklisted.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def blacklisted
  @blacklisted
end

#failover_strategyObject

Returns the value of attribute failover_strategy.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def failover_strategy
  @failover_strategy
end

#hostObject

Returns the value of attribute host.



45
46
47
# File 'lib/castle/configuration.rb', line 45

def host
  @host
end

#ip_headersObject

Returns the value of attribute ip_headers.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def ip_headers
  @ip_headers
end

#portObject

Returns the value of attribute port.



45
46
47
# File 'lib/castle/configuration.rb', line 45

def port
  @port
end

#request_timeoutObject

Returns the value of attribute request_timeout.



45
46
47
# File 'lib/castle/configuration.rb', line 45

def request_timeout
  @request_timeout
end

#trusted_proxiesObject

Returns the value of attribute trusted_proxies.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def trusted_proxies
  @trusted_proxies
end

#url_prefixObject

Returns the value of attribute url_prefix.



45
46
47
# File 'lib/castle/configuration.rb', line 45

def url_prefix
  @url_prefix
end

#whitelistedObject

Returns the value of attribute whitelisted.



46
47
48
# File 'lib/castle/configuration.rb', line 46

def whitelisted
  @whitelisted
end

Instance Method Details

#resetObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/castle/configuration.rb', line 54

def reset
  self.failover_strategy = FAILOVER_STRATEGY
  self.host = HOST
  self.port = PORT
  self.url_prefix = URL_PREFIX
  self.whitelisted = [].freeze
  self.blacklisted = [].freeze
  self.api_secret = ENV.fetch('CASTLE_API_SECRET', '')
  self.ip_headers = [].freeze
  self.trusted_proxies = [].freeze
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !api_secret.to_s.empty? && !host.to_s.empty? && !port.to_s.empty?
end