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

Creates a Base bootstrapped with environmental configuration

# 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"

service = ConfigurationService::Factory::EnvironmentContext.create
configuraton = 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(env = ENV, prefix = DEFAULT_PREFIX) ⇒ EnvironmentContext

Returns a new factory

The env defaults to the process ENV and the prefix defaults to the DEFAULT_PREFIX.

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.



43
44
45
# File 'lib/configuration_service/factory/environment_context.rb', line 43

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

Instance Attribute Details

#prefixObject (readonly)

The prefix for matching environment variable names

Names match if they begin with the prefix and an underscore.



26
27
28
# File 'lib/configuration_service/factory/environment_context.rb', line 26

def prefix
  @prefix
end

Class Method Details

.createObject

Return a Base bootstrapped with environmental configuration

See #create.

Uses the process ENV and the DEFAULT_PREFIX.



89
90
91
# File 'lib/configuration_service/factory/environment_context.rb', line 89

def self.create
  new.create
end

Instance Method Details

#createObject

Return a Base 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 provider documentation)

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

Returns the constructed service Base.



76
77
78
79
80
# File 'lib/configuration_service/factory/environment_context.rb', line 76

def create
  ConfigurationService.new(@env[:identifier], @env[:token], provider).tap do
    @env.scrub! # TODO mandates that scrub not try to mutate the strings given to provider
  end
end