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"

# 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

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.



56
57
58
# File 'lib/configuration_service/factory/environment_context.rb', line 56

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.



39
40
41
# File 'lib/configuration_service/factory/environment_context.rb', line 39

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.



102
103
104
# File 'lib/configuration_service/factory/environment_context.rb', line 102

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.



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

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