Module: Envy

Includes:
Deploy, Dotenv
Defined in:
lib/envy.rb,
lib/envy/version.rb

Overview

Module for loading environment variables and failing programs when variables are not set.

Constant Summary collapse

EnvMissing =
Class.new(StandardError)
VERSION =
'1.1.0'

Constants included from Deploy

Deploy::MissingHerokuToken, Deploy::TokenName

Class Method Summary collapse

Methods included from Deploy

#config_vars, #deploy_heroku, #heroku_client, heroku_token

Class Method Details

.deploy_heroku(app_name) ⇒ Object



48
49
50
# File 'lib/envy.rb', line 48

def self.deploy_heroku(app_name)
  Deploy.deploy_heroku(app_name)
end

.env_name?(data) ⇒ Boolean

Entity check for EnvName parameter

Parameters:

  • data (Object)

    object under test

Returns:

  • (Boolean)

    true if data is a valid EnvName, false otherwise



33
34
35
36
# File 'lib/envy.rb', line 33

def self.env_name?(data)
  data.is_a?(String) &&
    (data == data.upcase)
end

.load_env(env_name) ⇒ String?

Returns the value of the specified environment variable or raises an EnvMissing exception

Parameters:

  • env_name (String)

    name of the environment variable, which should match an entry in .env* files

Returns:

  • (String, nil)

    value of envrionment variable when env_name is a valid name and nil otherwise

Raises:

  • (EnvMissing)

    when the environment variable does not exist



24
25
26
27
28
# File 'lib/envy.rb', line 24

def self.load_env(env_name)
  if env_name?(env_name)
    ENV.fetch(env_name){|name| raise EnvMissing, load_env_error_message(name)}
  end
end

.load_env_error_message(env_name) ⇒ String

Exception message for EnvMissing in #load_env

Parameters:

  • env_name (String)

    name of the environment variable, which should match an entry in .env* files

Returns:

  • (String)

    error message



42
43
44
45
46
# File 'lib/envy.rb', line 42

def self.load_env_error_message(env_name)
  "#{env_name} is missing from the environment. "\
  "Please ensure that the entry #{env_name}=value-of-var is present in " \
  "your .env or .env.* file."
end

.test_envsObject

Creates environment variables for ‘.env’ and overwrites them with variables present in ‘.env.test`



13
14
15
# File 'lib/envy.rb', line 13

def self.test_envs
  Dotenv.overload('.env.test')
end