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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_tokenString?

Returns the API token necessary to gain access.

Returns:

  • (String, nil)

    the API token necessary to gain access



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

property :api_token

#api_token_middlewareFaraday::Middleware?

Returns the API token middleware that provides the API token to the request at runtime.

Returns:

  • (Faraday::Middleware, nil)

    the API token middleware that provides the API token to the request at runtime



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

property :api_token_middleware

#caching_object#read, #write

Returns an object that can cache request results.

Returns:

  • (#read, #write)

    an object that can cache request results



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

property :caching_object, default: nil

#endpointsHash{Symbol=>String}

Returns a mapping of application endpoint overrides (default: {}).

Returns:

  • (Hash{Symbol=>String})

    a mapping of application endpoint overrides (default: {})



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

property :endpoints, default: {}

#environmentSymbol

Returns the (default) API environment (default: :production).

Returns:

  • (Symbol)

    the (default) API environment (default: :production)



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

property :environment, default: :production

#http_cachingBoolean

Returns whether HTTP caching is enabled.

Returns:

  • (Boolean)

    whether HTTP caching is enabled



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

property :http_caching, default: false

#http_caching_object#read, ...

Returns an object that caches results respecting HTTP expiration (instead of an in-memory hash).

Returns:

  • (#read, #write, #delete)

    an object that caches results respecting HTTP expiration (instead of an in-memory hash)



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

property :http_caching_object, default: nil

#http_debugBoolean

Returns whether to show HTTP debug messages (default: false).

Returns:

  • (Boolean)

    whether to show HTTP debug messages (default: false)



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

property :http_debug, default: false

#http_debug_headersBoolean

Returns whether to show HTTP headers in the debug messages (default: false).

Returns:

  • (Boolean)

    whether to show HTTP headers in the debug messages (default: false)



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

property :http_debug_headers, default: false

#http_persistentBoolean

Returns whether persistent HTTP connections are used (default: true).

Returns:

  • (Boolean)

    whether persistent HTTP connections are used (default: true)



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

property :http_persistent, default: true

#json_debugBoolean

Returns whether to show JSON debug messages (default: false).

Returns:

  • (Boolean)

    whether to show JSON debug messages (default: false)



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

property :json_debug, default: false

#logger#fatal, ...

Returns the logger object (Rails logger, Logger, etc.).

Returns:

  • (#fatal, #error, #warn, #info, #debug)

    the logger object (Rails logger, Logger, etc.)



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

property :logger

#proxyHash, ...

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.

Returns:

  • (Hash, String, URI)

    the proxy configuration, either as URL or as a Hash



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

property :proxy

#stubbingString

Returns the path to a directory with stubbing fixtures (setting this enables the default stubs).

Returns:

  • (String)

    the path to a directory with stubbing fixtures (setting this enables the default stubs)



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

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

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 proxy and the development environment as default, enables debugging and overrides an endpoint

default:
  environment: "development"
  proxy: "http://proxyserver:8080"

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

Parameters:

  • config_file (String)

    the path to the config file to load

  • force_environment (Symbol, nil) (defaults to: nil)

    the environment to enforce (if any)

Returns:

  • (Boolean)

    whether the config file was used



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/lws/config.rb', line 156

def load_config_file(config_file, force_environment = nil)
  return false unless File.exist? config_file
  config_data = File.open(config_file) do |f|
    YAML.safe_load(f, filename: config_file, aliases: true)
  end
  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