Class: ConfigurationService::Factory::EnvironmentContext

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration_service/factory/environment_context.rb,
lib/configuration_service/factory/environment_context/env_dict.rb

Overview

A factory for creating a configuration service bootstrapped from environmental configuration

Examples:

Requesting configuration from the Vault service provider


# With the following in the environment:
#
# CFGSRV_IDENTIFIER="acme"
# CFGSRV_TOKEN="0b2a80f4-54ce-45f4-8267-f6558fee64af"
# CFGSRV_PROVIDER="vault"
# CFGSRV_PROVIDER_ADDRESS="http://127.0.0.1:8200"

# And the following in Gemfile:
#
# source 'https://rubygems.org'
#
# gem 'configuration_service-provider-vault'
# gem 'acme_application'

# Now main.rb (or config.ru or whatever) is decoupled from provider
# selection and service configuration:

require 'bundler'
Bundler.require(:default) # Registers the vault provider

configuration_service = ConfigurationService::Factory::EnvironmentContext.create
configuraton = configuration_service.request_configuration
AcmeApplication.new(configuration.data).run

Defined Under Namespace

Classes: EnvDict

Constant Summary collapse

DEFAULT_PREFIX =

The default prefix for matching environment variable names

"CFGSRV"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources = default_sources, prefix = DEFAULT_PREFIX) ⇒ EnvironmentContext

Returns a new factory

Most consumers will not need to call this method; they can use create which instantiates a factory with default env and prefix and uses that internally.

Parameters:

  • sources (Hash, Array<Hash>) (defaults to: default_sources)

    one or a list of the string-keyed environments

  • prefix (String) (defaults to: DEFAULT_PREFIX)

    the prefix for matching environment variable names



58
59
60
# File 'lib/configuration_service/factory/environment_context.rb', line 58

def initialize(sources = default_sources, prefix = DEFAULT_PREFIX)
  @env = EnvDict.new(sources, prefix)
end

Instance Attribute Details

#prefixString (readonly)

prefix for matching environment variable names. Names match if they begin with the prefix and an underscore.

Returns:

  • (String)

    the current value of prefix



37
38
39
# File 'lib/configuration_service/factory/environment_context.rb', line 37

def prefix
  @prefix
end

Class Method Details

.createObject

Return a configuration service bootstrapped with environmental configuration

Instantiates a new factory with the process ENV and the DEFAULT_PREFIX and uses it to create a configuration service.

See Also:



105
106
107
# File 'lib/configuration_service/factory/environment_context.rb', line 105

def self.create
  new.create
end

Instance Method Details

#createConfigurationService::Base

Create a configuration service bootstrapped with environmental configuration

The environment is scanned for #prefix matches, within which the following variables are used:

IDENTIFIER

the unique identity of the configuration data (see Base#initialize)

TOKEN

authorization token for the identified configuration data (see Base#initialize)

PROVIDER

the unique identity of the service provider (see ProviderRegistry)

PROVIDER_*

configuration options for the service provider (see service provider documentation)

On JRuby, system properties are also scanned. Where a system property and environment variable of the same name exist, the environment variable is preferred.

The service provider class is fetched from the ProviderRegistry using PROVIDER. A service provider instance is then constructed with a dictionary of the PROVIDER_* variables, in which the keys are the name of the variable without PROVIDER_, downcased and intern’d.

Then a service Base is constructed with the IDENTIFIER, TOKEN and service provider instance.

And finally, the environment is scrubbed of the variables used, to protect them from accidental exposure (e.g. in an exception handler that prints the environment). On JRuby, system properties are scrubbed of variables used as well, regardless of whether they were overridden by environment variables.

Returns:

Raises:



92
93
94
95
96
# File 'lib/configuration_service/factory/environment_context.rb', line 92

def create
  ConfigurationService.new(@env[:identifier], @env[:token], provider).tap do
    @env.scrub!
  end
end