Module: Kreds::Fetch
- Included in:
- Kreds
- Defined in:
- lib/kreds/fetch.rb
Instance Method Summary collapse
Instance Method Details
#env_fetch!(*keys, var: nil) ⇒ Object
24 25 26 |
# File 'lib/kreds/fetch.rb', line 24 def env_fetch!(*keys, var: nil, &) fetch!(Rails.env, *keys, var:, &) end |
#fetch!(*keys, var: nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/kreds/fetch.rb', line 3 def fetch!(*keys, var: nil, &) symbolized_keys = Kreds::Inputs.process( keys, as: :symbol_array, message: "Expected an array of symbols or strings, got `#{keys.inspect}`" ) path = [] symbolized_keys.reduce(Rails.application.credentials) do |hash, key| path << key fetch_key(hash, key, path, symbolized_keys) end rescue Kreds::BlankCredentialsError, Kreds::UnknownCredentialsError => e fallback_to_var( e, Kreds::Inputs.process( var, as: :non_empty_string, optional: true, message: "Expected a non-empty string, got `#{var.inspect}`" ), & ) end |
#var!(var) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/kreds/fetch.rb', line 28 def var!(var, &) value = ENV.fetch( Kreds::Inputs.process(var, as: :non_empty_string, message: "Expected a non-empty string, got `#{var.inspect}`") ) return raise_or_yield(Kreds::BlankEnvironmentVariableError.new("Blank value in environment variable: #{var.inspect}"), &) if value.blank? value rescue KeyError raise_or_yield(Kreds::UnknownEnvironmentVariableError.new("Environment variable not found: #{var.inspect}"), &) end |