Module: Conker
- Defined in:
- lib/conker.rb
Overview
Example that uses the process’s environment:
module Conker
setup_config!(Rails.env, :A_SECRET => api_credential)
end
Example that uses a supplied hash of values (e.g. read from some file or database):
config_values = {:A_SECRET => 'very_secret'}
module Conker
setup_config!(Rails.env, config_values, :A_SECRET => api_credential)
end
For convenience, if your config file is YAML, you can supply the path directly and Conker will load and parse the file:
module Conker
setup_config!(Rails.env, 'config_values.yml', :A_SECRET => api_credential)
end
Defined Under Namespace
Classes: Error, MissingDefault, MustBeDefined, UnknownType, VariableDeclaration
Constant Summary collapse
- ENVIRONMENTS =
%w(production development test)
- DUMMY_API_KEY =
'dummy_api_key'.freeze
- DUMMY_CRYPTO_SECRET =
'dummysecretdummysecretdummysecretdummysecretdummysecretdummysecretdummysecre'
Class Method Summary collapse
-
.api_credential(declaration_opts = {}) ⇒ Object
Declare an environment variable to be used as a credential for accessing an external API (e.g. username, password, API key, access token): shorthand for required_in_production(:type => :string, :default => ‘dummy_api_key’).
-
.crypto_secret(declaration_opts = {}) ⇒ Object
Declare an environment variable to be used as a secret key by some encryption algorithm used in our code.
-
.optional(declaration_opts = {}) ⇒ Object
Declare an environment variable, defaulting to other values if not defined.
-
.redis_url(opts = {}) ⇒ Object
A redis url is required_in_production with development and test defaulting to localhost.
-
.required_in_production(declaration_opts = {}) ⇒ Object
Declare an environment variable that is required to be defined in the production environment, and defaults to other values in the test or development environments.
-
.setup_config!(current_env, *args) ⇒ Object
Parse a multi-key hash into globals and raise an informative error message on failure.
-
.setup_rack_environment!(*args) ⇒ Object
Like setup_config! but uses ENV || ‘development’ as the environment.
Class Method Details
.api_credential(declaration_opts = {}) ⇒ Object
Declare an environment variable to be used as a credential for accessing an external API (e.g. username, password, API key, access token): shorthand for required_in_production(:type => :string, :default => ‘dummy_api_key’)
89 90 91 92 93 94 |
# File 'lib/conker.rb', line 89 def api_credential(declaration_opts={}) required_in_production({ :type => :string, :default => DUMMY_API_KEY, }.merge(declaration_opts)) end |
.crypto_secret(declaration_opts = {}) ⇒ Object
Declare an environment variable to be used as a secret key by some encryption algorithm used in our code.
To generate a secret suitable for production use, try:
openssl rand -hex 256
(which will generate 256 bytes = 2048 bits of randomness).
The distinction between this and api_credential is mainly for documentation purposes, but they also have different defaults.
105 106 107 108 109 110 |
# File 'lib/conker.rb', line 105 def crypto_secret(declaration_opts={}) required_in_production({ :type => :string, :default => DUMMY_CRYPTO_SECRET, }.merge(declaration_opts)) end |
.optional(declaration_opts = {}) ⇒ Object
Declare an environment variable, defaulting to other values if not defined.
You must either specify a :default, or specify defaults for each of :production, :test and :development.
124 125 126 |
# File 'lib/conker.rb', line 124 def optional(declaration_opts = {}) VariableDeclaration.new(declaration_opts) end |
.redis_url(opts = {}) ⇒ Object
A redis url is required_in_production with development and test defaulting to localhost.
113 114 115 116 117 118 |
# File 'lib/conker.rb', line 113 def redis_url(opts={}) required_in_production({ :development => "redis://localhost/1", :test => "redis://localhost/3" }.merge(opts)) end |
.required_in_production(declaration_opts = {}) ⇒ Object
Declare an environment variable that is required to be defined in the production environment, and defaults to other values in the test or development environments.
You must either specify a :default, or specify defaults for each of :test and :development.
81 82 83 |
# File 'lib/conker.rb', line 81 def required_in_production(declaration_opts={}) VariableDeclaration.new(declaration_opts.reverse_merge(:required_in => :production)) end |
.setup_config!(current_env, *args) ⇒ Object
Parse a multi-key hash into globals and raise an informative error message on failure.
43 44 45 46 47 48 |
# File 'lib/conker.rb', line 43 def setup_config!(current_env, *args) declarations = args. values = values_hash(args[0]) setup_constants(current_env, declarations, values) end |
.setup_rack_environment!(*args) ⇒ Object
Like setup_config! but uses ENV || ‘development’ as the environment. Also sets constant RACK_ENV.
N.B. if using this method, you don’t need to specify :RACK_ENV in your variable declarations, and it will complain if you do. This is partly to make clear that this method *won’t* read RACK_ENV from your config file, only from the environment variable, for compatibility with other code (e.g. Sinatra) that depends directly on the environment variable.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/conker.rb', line 58 def setup_rack_environment!(*args) ENV['RACK_ENV'] ||= 'development' set_constant(:RACK_ENV, ENV['RACK_ENV']) declarations = args. values = values_hash(args[0]) if declarations.key?('RACK_ENV') || declarations.key?(:RACK_ENV) raise Error, "No need to declare RACK_ENV; please remove it to avoid confusion!" end if ENV.key?('RACK_ENV') && values.key?('RACK_ENV') && (env = ENV['RACK_ENV']) != (conf = values['RACK_ENV']) raise "RACK_ENV differs between environment (#{env}) and config (#{conf})! Please remove it from your config." end setup_constants(ENV['RACK_ENV'], declarations, values) end |