Module: Config

Defined in:
lib/env-conf.rb,
lib/env-conf/version.rb

Constant Summary collapse

VERSION =
'0.0.5'
@@defaults =
{}
@@dotenv =
{}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ String

Get a Config value

This is the preferred and uniform way to access config vars because defaults are included in the lookup

Uses defaults if available. Converts upper-case ENV var names to lower-case default names.

Order of precedence is: 1) app’s local ENV 2) default values

Config == nil

Config.default(:foo, ‘bar’) Config == ‘bar’

ENV = ‘baz’ Config == ‘baz’

Parameters:

  • key (Symbol)

    The lower-case name of the ENV value

Returns:

  • (String)

    The value of the ENV value or default.



31
32
33
34
35
# File 'lib/env-conf.rb', line 31

def self.[](name)
  var_name = name.to_s.upcase
  default_name = name.to_s.downcase.to_sym
  ENV[var_name] || @@dotenv[var_name] || @@defaults[default_name]
end

.app_deployString

The ‘APP_DEPLOY` env var is used to identify which deploy of the codebase is running in librato. This usually matches the name of the environment such as local, production, staging, etc.

Returns:

  • (String)

    The deploy/environment of the app



112
113
114
# File 'lib/env-conf.rb', line 112

def self.app_deploy
  env("APP_DEPLOY")
end

.app_envSymbol

The ‘RACK_ENV` env var is used to identify the app mode

Returns:

  • (Symbol)

    The environment of the app



119
120
121
# File 'lib/env-conf.rb', line 119

def self.app_env
  env!("RACK_ENV").to_sym
end

.app_nameString

The ‘APP_NAME` env var is used to identify which codebase is running in librato. This usually matches the name of the repository.

Returns:

  • (String)

    The name of the app



103
104
105
# File 'lib/env-conf.rb', line 103

def self.app_name
  env("APP_NAME")
end

.array(name) ⇒ Array

Comma-separated words converted to an array.

Parameters:

  • name (String)

    The name of the environment variable to fetch an array for.

Returns:

  • (Array)

    An array of values.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



156
157
158
# File 'lib/env-conf.rb', line 156

def self.array(name)
  env(name).to_s.split(',')
end

.bool?(name) ⇒ bool

An environment variable converted to a bool.

Parameters:

  • name (String)

    The name of the environment variable to fetch a boolean for.

Returns:

  • (bool)

    True if the value is ‘true`, otherwise false.



165
166
167
# File 'lib/env-conf.rb', line 165

def self.bool?(name)
  self[name] == true || self[name] == 'true'
end

.database_url(kind = '') ⇒ Object

The database URL from the environment.

Parameters:

  • kind (String) (defaults to: '')

    Optionally, the leading name of ‘*_DATABASE_URL` environment variable. Defaults to `DATABASE_URL`.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



135
136
137
138
# File 'lib/env-conf.rb', line 135

def self.database_url(kind = '')
  kind = "#{kind}_".upcase unless kind.empty?
  env!("#{kind}DATABASE_URL")
end

.default(key, value) ⇒ String

Set a default Defaults are supplied when accessing via Config

Parameters:

  • key (Symbol/String)

    The lower-case name of the default

Returns:

  • (String)

    The value of the default



58
59
60
# File 'lib/env-conf.rb', line 58

def self.default(key, value)
  @@defaults[key.to_s.downcase.to_sym] = value
end

.defaultsHash

Get all the defaults

Returns:

  • (Hash)

    The current set of defaults



64
65
66
# File 'lib/env-conf.rb', line 64

def self.defaults
  @@defaults
end

.development?Bool

Returns True if the service is in development mode.

Returns:

  • (Bool)

    True if the service is in development mode.



95
96
97
# File 'lib/env-conf.rb', line 95

def self.development?
  self['RACK_ENV'] == 'development'
end

.dotenv!Object

Loads a “.env” file, using Dotenv to parse but not fuck up the ENV



187
188
189
190
191
192
193
194
195
# File 'lib/env-conf.rb', line 187

def self.dotenv!
  return if Config.production?
  require 'dotenv'
  ['.env','.env.local',".env.#{Config[:rack_env]}",".env.#{Config[:rack_env]}.local"].each do |filename|
    if File.exists?(filename)
      @@dotenv.update(Dotenv::Parser.call(File.read(filename)))
    end
  end
end

.env(name) ⇒ String

An environment variable.

Parameters:

  • name (String)

    The name of the environment variable to fetch a value for.

Returns:

  • (String)

    The value of the environment variable or nil if no match is available.



49
50
51
# File 'lib/env-conf.rb', line 49

def self.env(name)
  self[name]
end

.env!(name) ⇒ String

An environment variable.

Parameters:

  • name (String)

    The name of the environment variable to fetch a value for.

Returns:

  • (String)

    The value of the environment variable.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



74
75
76
# File 'lib/env-conf.rb', line 74

def self.env!(name)
  self[name] || raise("missing #{name}")
end

.int(name) ⇒ Fixnum

An environment variable converted to a Fixnum.

Parameters:

  • name (String)

    The name of the environment variable to fetch a Fixnum for.

Returns:

  • (Fixnum)

    The number or nil if the value couldn’t be coerced to a Fixnum.



146
147
148
# File 'lib/env-conf.rb', line 146

def self.int(name)
  self[name] && Integer(self[name])
end

.portFixnum

The port to listen on for web requests.

Returns:

  • (Fixnum)

    The port to listen on for web requests.



126
127
128
# File 'lib/env-conf.rb', line 126

def self.port
  env!("PORT").to_i
end

.production?Bool

The ‘RACK_ENV` environment variable is used to determine whether the service is in production mode or not.

Returns:

  • (Bool)

    True if the service is in production mode.



82
83
84
# File 'lib/env-conf.rb', line 82

def self.production?
  self['RACK_ENV'] == 'production'
end

.reset!Object

Reset defaults values



38
39
40
41
# File 'lib/env-conf.rb', line 38

def self.reset!
  @@defaults = {}
  @@dotenv   = {}
end

.test?Bool

The ‘RACK_ENV` environment variable is used to determine whether the service is in test mode or not.

Returns:

  • (Bool)

    True if the service is in test mode.



90
91
92
# File 'lib/env-conf.rb', line 90

def self.test?
  self['RACK_ENV'] == 'test'
end

.time(name) ⇒ Time

An environment variable converted to a time.

Parameters:

  • name (String|Symbol)

    The name of the environment variable to fetch a boolean for.

Returns:

  • (Time)

    Time if the value is parseable, otherwise false.



174
175
176
# File 'lib/env-conf.rb', line 174

def self.time(name)
  self[name] && Time.parse(self[name])
end

.uri(name) ⇒ URI

An environment variable converted to a URI.

Parameters:

  • name (String|Symbol)

    The name of the environment variable.

Returns:

  • (URI)

    URI if the value is parseable, otherwise false.



182
183
184
# File 'lib/env-conf.rb', line 182

def self.uri(name)
  self[name] && URI.parse(self[name])
end