Module: Config

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

Constant Summary collapse

VERSION =
'0.0.2'
@@defaults =
{}

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.



28
29
30
31
32
# File 'lib/env-conf.rb', line 28

def self.[](name)
  var_name = name.to_s.upcase
  default_name = name.to_s.downcase.to_sym
  ENV[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



108
109
110
# File 'lib/env-conf.rb', line 108

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



115
116
117
# File 'lib/env-conf.rb', line 115

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



99
100
101
# File 'lib/env-conf.rb', line 99

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.



152
153
154
# File 'lib/env-conf.rb', line 152

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.



161
162
163
# File 'lib/env-conf.rb', line 161

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.



131
132
133
134
# File 'lib/env-conf.rb', line 131

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



54
55
56
# File 'lib/env-conf.rb', line 54

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



60
61
62
# File 'lib/env-conf.rb', line 60

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.



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

def self.development?
  self['RACK_ENV'] == 'development'
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.



45
46
47
# File 'lib/env-conf.rb', line 45

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.



70
71
72
# File 'lib/env-conf.rb', line 70

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.



142
143
144
# File 'lib/env-conf.rb', line 142

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

.portFixnum

The port to listen on for web requests.

Returns:

  • (Fixnum)

    The port to listen on for web requests.



122
123
124
# File 'lib/env-conf.rb', line 122

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.



78
79
80
# File 'lib/env-conf.rb', line 78

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

.reset!Object

Reset defaults values



35
36
37
# File 'lib/env-conf.rb', line 35

def self.reset!
  @@defaults = {}
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.



86
87
88
# File 'lib/env-conf.rb', line 86

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.



170
171
172
# File 'lib/env-conf.rb', line 170

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.



178
179
180
# File 'lib/env-conf.rb', line 178

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