Class: LWS::Config
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- LWS::Config
- Defined in:
- lib/lws/config.rb
Overview
Either the API token or API token middleware needs to be configured for the library to work properly!
The LWS API configuration class
This class represents the configuration that is used for connecting to LWS.
Constant Summary collapse
- VALID_FILE_PROPERTIES =
The list of properties that can be set using a config file.
[:api_token, :endpoints, :environment, :http_debug, :http_debug_headers, :json_debug, :proxy]
Instance Attribute Summary collapse
-
#api_token ⇒ String?
The API token necessary to gain access.
-
#api_token_middleware ⇒ Faraday::Middleware?
The API token middleware that provides the API token to the request at runtime.
-
#caching_object ⇒ #read, #write
An object that can cache request results.
-
#endpoints ⇒ Hash{Symbol=>String}
A mapping of application endpoint overrides (default: {}).
-
#environment ⇒ Symbol
The (default) API environment (default:
:production). -
#http_caching ⇒ Boolean
Whether HTTP caching is enabled.
-
#http_caching_object ⇒ #read, ...
An object that caches results respecting HTTP expiration (instead of an in-memory hash).
-
#http_debug ⇒ Boolean
Whether to show HTTP debug messages (default:
false). -
#http_debug_headers ⇒ Boolean
Whether to show HTTP headers in the debug messages (default:
false). -
#http_persistent ⇒ Boolean
Whether persistent HTTP connections are used.
-
#json_debug ⇒ Boolean
Whether to show JSON debug messages (default:
false). -
#logger ⇒ #fatal, ...
The logger object (Rails logger, Logger, etc.).
-
#proxy ⇒ Hash, ...
When passing a Hash, it should at least contain
:urias key with a String or URI as value and optionally the:userand:passwordkeys with appropriate values. -
#stubbing ⇒ String
The path to a directory with stubbing fixtures (setting this enables the default stubs).
Instance Method Summary collapse
-
#load_config_file(config_file, force_environment = nil) ⇒ Boolean
Supplements the configuration with settings from a config file.
Instance Attribute Details
#api_token ⇒ String?
Returns the API token necessary to gain access.
28 |
# File 'lib/lws/config.rb', line 28 property :api_token |
#api_token_middleware ⇒ Faraday::Middleware?
Returns the API token middleware that provides the API token to the request at runtime.
33 |
# File 'lib/lws/config.rb', line 33 property :api_token_middleware |
#caching_object ⇒ #read, #write
Returns an object that can cache request results.
37 |
# File 'lib/lws/config.rb', line 37 property :caching_object, default: nil |
#endpoints ⇒ Hash{Symbol=>String}
Returns a mapping of application endpoint overrides (default: {}).
42 |
# File 'lib/lws/config.rb', line 42 property :endpoints, default: {} |
#environment ⇒ Symbol
Returns the (default) API environment (default: :production).
47 |
# File 'lib/lws/config.rb', line 47 property :environment, default: :production |
#http_caching ⇒ Boolean
Returns whether HTTP caching is enabled.
51 |
# File 'lib/lws/config.rb', line 51 property :http_caching, default: false |
#http_caching_object ⇒ #read, ...
Returns an object that caches results respecting HTTP expiration (instead of an in-memory hash).
56 |
# File 'lib/lws/config.rb', line 56 property :http_caching_object, default: nil |
#http_debug ⇒ Boolean
Returns whether to show HTTP debug messages (default: false).
60 |
# File 'lib/lws/config.rb', line 60 property :http_debug, default: false |
#http_debug_headers ⇒ Boolean
Returns whether to show HTTP headers in the debug messages (default: false).
65 |
# File 'lib/lws/config.rb', line 65 property :http_debug_headers, default: false |
#http_persistent ⇒ Boolean
Returns whether persistent HTTP connections are used.
69 |
# File 'lib/lws/config.rb', line 69 property :http_persistent, default: true |
#json_debug ⇒ Boolean
Returns whether to show JSON debug messages (default: false).
73 |
# File 'lib/lws/config.rb', line 73 property :json_debug, default: false |
#logger ⇒ #fatal, ...
Returns the logger object (Rails logger, Logger, etc.).
78 |
# File 'lib/lws/config.rb', line 78 property :logger |
#proxy ⇒ Hash, ...
When passing a Hash, it should at least contain :uri as key with a String or URI as value and optionally the :user and :password keys with appropriate values.
86 |
# File 'lib/lws/config.rb', line 86 property :proxy |
#stubbing ⇒ String
Returns the path to a directory with stubbing fixtures (setting this enables the default stubs).
91 |
# File 'lib/lws/config.rb', line 91 property :stubbing |
Instance Method Details
#load_config_file(config_file, force_environment = nil) ⇒ Boolean
Supplements the configuration with settings from a config file.
The configuration file has a section per environment that indicates per property what to use if it is unset.
Note that this is only done for a specific subset of of properties. See VALID_FILE_PROPERTIES for this subset!
The configuration file can optionally have a “default” section with an environment key that selects the default environment (unless overriden by the LC_LWS_ENV environment variable).
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/lws/config.rb', line 154 def load_config_file(config_file, force_environment = nil) return false unless File.exist? config_file config_data = YAML.load_file(config_file) default_config = config_data["default"] || {} self.environment = force_environment || ENV["LC_LWS_ENV"] || config_data.dig("default", "environment") || self.environment config = default_config.merge(config_data[self.environment.to_s] || {}) config.each_pair do |key, value| unless VALID_FILE_PROPERTIES.include? key.to_sym raise "encountered an invalid config property \"#{key}\" " + "in config file #{config_file}!" end configure(key, value) end true end |