Project

early

0.01
No commit activity in last 3 years
No release in over 3 years
Checks for environment variables early in your program.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

Early

Early checks for environment variables availability, so you don't have to. Hook it early in your program to require or default a variable and then work with ENV like you normally would. Extremely useful for Twelve-Factor apps.

Usage

Add this line to your application's Gemfile:

gem 'early', require: false

Afterwards, make sure to call Early as early as possible in your application, to check the ENV variables, before you use them in your configuration layer:

require 'early'

Early do
  require :DATABASE_URL
  require :REDIS_URL

  default :PROVIDER, :generic
end

The configuration will require the presence of DATABASE_URL and REDIS_URL and will raise Early::Error if any of them is missing. It will also set a default value to the env PROVIDER.

Rails

If you want to use early with Rails, you can store the early configuration in config/early.rb:

require 'early'

Early do
  require :ADMIN_NAME, :ADMIN_PASSWORD
  require :MEETUP_API_KEY
end

More importantly, require it in config/boot.rb, which is executed before the config/application.rb and config/initializers files:

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup'

require_relative 'early' # 👈

This will make sure, that the rules you wanted early to enforce have been applied before any code in config has been run.

Travis

If you are using Travis CI, you can auto-load the environment variables specified in .travis.yml with:

require 'early'

Early :development do
  travis
end

License

The gem is available as open source under the terms of the MIT License.