0.0
No commit activity in last 3 years
No release in over 3 years
Check environment variable presence with minimal YAML file
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 1.0
 Project Readme

RequireEnv

Check environment variable presence easily.

Install

bundle install require_env

Usage

Create a YAML file, e.g. config/environment.yml, where you define the environment variables required by your application, for the different environment it will run (test, development, production by default)

  • You can use ERB code to provide computed values
  • You can use YAML defaults.
  • An optional default value can be given for any key
  • Keys ending with _IF_OTHER_VARIABLE will only be checked if OTHER_VARIABLE is defined and not equal 'false'
  • Default value have precedence over the _IF_OTHER_VARIABLE rule

Here is an example presenting the different possibilities:

DEFAULTS: &DEFAULTS
  BEANSTALK_URL: 'beanstalk://127.0.0.1'

  SLACK_TOKEN:

  # Use the environment variable TOKEN_SCALINGO as a default value if provided
  SCALINGO_TOKEN: "<%= ENV['TOKEN_SCALINGO'] %>"

  # Declare STRIPE_ENABLED to check for STRIPE_PUBLIC_KEY and STRIPE_SECRET_KEY
  STRIPE_PUBLIC_KEY_IF_STRIPE_ENABLED:
  STRIPE_SECRET_KEY_IF_STRIPE_ENABLED:

test:
  <<: *DEFAULTS
  IGNORE_EVENT_DELAY: 'true'

development:
  <<: *DEFAULTS
  IGNORE_EVENT_DELAY: 'true'

production:
  <<: *DEFAULTS

When your application starts, simply call RequireEnv.check(Rails.root.join('config', 'environment.yml')) to check if the required environment variables are provided.

If any variable is missing, RequireEnv::Missing will be raised, detailing the missing environment variable.