Class: LWS::Config

Inherits:
Hashie::Dash
  • Object
show all
Defined in:
lib/lws/config.rb

Overview

Note:

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, :http_debug, :http_debug_headers, :json_debug]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_tokenString?



27
# File 'lib/lws/config.rb', line 27

property :api_token

#api_token_middlewareFaraday::Middleware?



32
# File 'lib/lws/config.rb', line 32

property :api_token_middleware

#caching_object#read, #write



36
# File 'lib/lws/config.rb', line 36

property :caching_object, default: nil

#endpointsHash{Symbol=>String}



41
# File 'lib/lws/config.rb', line 41

property :endpoints, default: {}

#environmentSymbol



46
# File 'lib/lws/config.rb', line 46

property :environment, default: :production

#http_cachingBoolean



50
# File 'lib/lws/config.rb', line 50

property :http_caching, default: true

#http_caching_object#read, ...



55
# File 'lib/lws/config.rb', line 55

property :http_caching_object, default: nil

#http_debugBoolean



59
# File 'lib/lws/config.rb', line 59

property :http_debug, default: false

#http_debug_headersBoolean



64
# File 'lib/lws/config.rb', line 64

property :http_debug_headers, default: false

#http_persistentBoolean



68
# File 'lib/lws/config.rb', line 68

property :http_persistent, default: true

#json_debugBoolean



72
# File 'lib/lws/config.rb', line 72

property :json_debug, default: false

#logger#fatal, ...



77
# File 'lib/lws/config.rb', line 77

property :logger

#stubbingString



82
# File 'lib/lws/config.rb', line 82

property :stubbing

Instance Method Details

#load_config_file(config_file, force_environment = nil) ⇒ Boolean

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).

Examples:

A simple configuration that sets the API token per environment

production:
  api_token: "my-prod-api-token"

development:
  api_token: "my-dev-api-token"

A elaborate configuration that sets the development environment as default, enables debugging and overrides an endpoint

default:
  environment: "development"

production:
  api_token: "my-prod-api-token"
  http_debug: false
  json_debug: false

development:
  api_token: "my-dev-api-token"
  endpoints:
    maps: http://maps.leftclick.cloud
  http_debug: true
  json_debug: true


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lws/config.rb', line 123

def load_config_file(config_file, force_environment = nil)
  return false unless File.exist? config_file
  config_data = YAML.load_file(config_file)
  environment = force_environment ||
                ENV["LC_LWS_ENV"] ||
                config_data.dig("default", "environment") ||
                self.environment
  self.environment = environment.to_sym
  config = config_data[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