Class: StratoEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/strato_env.rb,
lib/strato_env/version.rb,
lib/strato_env/ssm_fetcher.rb

Overview

StratoEnv loads layered configuration into ENV at boot.

A loader composes one or more paths worth of key/value pairs and writes them to ENV. The actual fetching of values is delegated to a fetcher, any callable that takes a path and returns a Hash<String, String>. The default fetcher is SSMFetcher, which reads from AWS SSM Parameter Store, but you can inject any callable (a Proc, a lambda, a Method, or a class with #call defined). This makes it easy to:

  • Test loader logic without touching AWS by passing a stub fetcher.

  • Add other backends (Secrets Manager, Vault, a YAML file) without changing the loader.

  • Preview values before applying them via #fetch.

When multiple paths are passed, they form layers: later paths override earlier ones (right-most wins on collision).

Examples:

Default usage (SSM)

StratoEnv.load(paths: ["/myapp/common/", "/myapp/web/"])

Inspect values without touching ENV

StratoEnv.fetch(paths: "/myapp/web/")
# => { "DATABASE_URL" => "postgres://...", "REDIS_URL" => "redis://..." }

Customise the SSM fetcher

fetcher = StratoEnv::SSMFetcher.new(recursive: true)
StratoEnv.new(fetcher: fetcher).load(paths: "/myapp/")

Inject any callable as a fetcher

yaml_fetcher = ->(path) { YAML.load_file(path) }
StratoEnv.new(fetcher: yaml_fetcher).load(paths: "config/app.yml")

Defined Under Namespace

Classes: SSMFetcher

Constant Summary collapse

VERSION =
"0.1.0".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fetcher: SSMFetcher.new) ⇒ StratoEnv

Returns a new instance of StratoEnv.

Parameters:

  • fetcher (#call) (defaults to: SSMFetcher.new)

    any callable that takes a path and returns a Hash<String, String>. Anything that responds to #call(path) works: a Proc, a lambda, a Method object, or a class with #call defined. Defaults to a new SSMFetcher.



40
41
42
# File 'lib/strato_env.rb', line 40

def initialize(fetcher: SSMFetcher.new)
  @fetcher = fetcher
end

Class Method Details

.apply(values) ⇒ Object

See Also:



81
82
83
84
# File 'lib/strato_env.rb', line 81

def self.apply(values)
  values.each { |name, value| ENV[name] = value }
  values.keys
end

.fetch(paths:) ⇒ Object

See Also:



78
# File 'lib/strato_env.rb', line 78

def self.fetch(paths:) = new.fetch(paths: paths)

.load(paths:) ⇒ Object

See Also:



75
# File 'lib/strato_env.rb', line 75

def self.load(paths:) = new.load(paths: paths)

Instance Method Details

#apply(values) ⇒ Array<String>

Write the given hash to ENV. Useful for composing with #fetch when you want custom logic between fetching and applying (e.g. diffing across namespaces and warning on overrides).

Parameters:

  • values (Hash<String, String>)

    keys become ENV var names.

Returns:

  • (Array<String>)

    the names that were set in ENV.



72
# File 'lib/strato_env.rb', line 72

def apply(values) = self.class.apply(values)

#fetch(paths:) ⇒ Hash<String, String>

Fetch values for the given paths and return them as a merged hash without touching ENV. Useful for previewing, testing, or applying the values to somewhere other than ENV.

Parameters:

  • paths (String, Array<String>)

    one or more paths to load, in order. Later paths override earlier ones on key collision.

Returns:

  • (Hash<String, String>)

    merged key/value pairs across all paths.



62
63
64
# File 'lib/strato_env.rb', line 62

def fetch(paths:)
  Array(paths).map { |path| @fetcher.call(path) }.reduce({}, :merge)
end

#load(paths:) ⇒ Array<String>

Fetch values for the given paths and write them to ENV.

Equivalent to apply(fetch(paths: paths)).

Parameters:

  • paths (String, Array<String>)

    one or more paths to load, in order. Later paths override earlier ones on key collision.

Returns:

  • (Array<String>)

    the names of the ENV vars that were set.



51
52
53
# File 'lib/strato_env.rb', line 51

def load(paths:)
  apply(fetch(paths: paths))
end