envi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a5095eefa6698a31f090267fd358bd1a7cf7967
4
+ data.tar.gz: 6cfb3cbce23466ac9fd4be27e51a8de4ac1ffcd6
5
+ SHA512:
6
+ metadata.gz: 89d8e312da69554691dd9664650e06330e9d2b58b898790f79624f30fcbfca654562a08344bd4e5adcbc9e91211af3700af14ce001b5674001e5e91926e8aa03
7
+ data.tar.gz: da3ffbf021043da892281b952ce2516efd27f76892f461cd416e03890f7b335f36bbf2c7ae6fd064296b62b5a1b54e306cfa7995e02b99e5a0558d093af00dab
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --format progress
2
+ --color
3
+ --order rand
4
+ -I lib
5
+ -r envi
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - rbx-19mode
@@ -0,0 +1,9 @@
1
+ --no-private
2
+ --title "Envy, for configuring required environment variables"
3
+ --readme README.md
4
+ --markup markdown
5
+ --markup-provider kramdown
6
+ --output-dir doc/api
7
+ -
8
+ HISTORY.md
9
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in envy.gemspec
4
+ gemspec
File without changes
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Arjan van der Gaag
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # Envy [![Build Status](https://secure.travis-ci.org/avdgaag/envy.png?branch=master)](http://travis-ci.org/avdgaag/envy)
2
+
3
+ Envy is a simple module to define environment variable requirements, and to load
4
+ those environment variables into Ruby constants. This is ideal for managing
5
+ configuration settings using environment variables, as one would on [Heroku].
6
+
7
+ Read the [full API documentation][docs] for detailed API and usage instructions.
8
+
9
+ [docs]: http://rubydoc.info/github/avdgaag/envy
10
+ [Heroku]: http://heroku.com
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'envy'
17
+
18
+ And then execute:
19
+
20
+ % bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ % gem install envy
25
+
26
+ ## Usage
27
+
28
+ ### The `envars.yml` file
29
+
30
+ First, define your requirements in a YAML file. By default, Envy will look for
31
+ `./config/envars.yml`. For example:
32
+
33
+ ---
34
+ &defaults:
35
+ - name: AWS_ACCESS_KEY
36
+ message: Please provide your Amazon AWS credentials as environment variables.
37
+ - name: AWS_ACCESS_SECRET
38
+ message: Please provide your Amazon AWS credentials as environment variables.
39
+ - name: HASHING_SALT
40
+ production:
41
+ <<: *defaults
42
+ development:
43
+ <<: *defaults
44
+
45
+ You can define your required environment variables by name, and also provide
46
+ an optional custom exception message to be used when that variable is not
47
+ set.
48
+
49
+ ### Initializing Envy
50
+
51
+ To read the requirements and inspect the current environment variables,
52
+ simply call `Envy.init`. This will raise an exception if requirements are not
53
+ met. When working with Rails apps, you might want to use this is an
54
+ initializer.
55
+
56
+ ### Providing environment variables in a file
57
+
58
+ A common pattern is to define custom environment variables in a `.env` file
59
+ in your application's root directory. Other gems, such as [Foreman][] will
60
+ use such a file to augment the environment before running processes. When in
61
+ development mode, Envy can also do this for you. Simply tell it which file to
62
+ use:
63
+
64
+ Envy.init parse: '.env'
65
+
66
+ It is a good idea to not include such a file (which commonly contains
67
+ application secrets) in source control.
68
+
69
+ ### Extending your Rails application configuration
70
+
71
+ Envy will expose all the loaded environment variables as constants in the Envy
72
+ module, so you can access them as:
73
+
74
+ Envy::MY_VARIABLE
75
+
76
+ You could also opt to extend your Rails configuration object:
77
+
78
+ Envy.init use: MyApp::Application.config
79
+ MyApp::Application.config.my_variable # => ...
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'envi'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'envi'
8
+ spec.version = Envi::VERSION
9
+ spec.authors = ['Arjan van der Gaag']
10
+ spec.email = ['arjan@arjanvandergaag.nl']
11
+ spec.description = %q{Configure required environment variables in your Rails apps}
12
+ spec.summary = <<-EOS
13
+ Envi is a simple tool to make managing required environment variables for
14
+ your Rails application a little easier. It allows you to define the required
15
+ variables in a YAML file and provide sensible failure instructions.
16
+ EOS
17
+ spec.homepage = 'https://github.com/avdgaag/envi'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'yard'
28
+ spec.add_development_dependency 'rspec', '>= 2.13'
29
+ end
@@ -0,0 +1,179 @@
1
+ require 'yaml'
2
+
3
+ # Envi is a simple module to define environment variable requirements, and to load
4
+ # those environment variables into Ruby constants. This is ideal for managing configuration
5
+ # settings using environment variables, as one would on [Heroku].
6
+ #
7
+ # ## Setup
8
+ #
9
+ # ### The `envars.yml` file
10
+ #
11
+ # First, define your requirements in a YAML file. By default, Envi will look for
12
+ # `./config/envars.yml`. For example:
13
+ #
14
+ # ---
15
+ # &defaults:
16
+ # - name: AWS_ACCESS_KEY
17
+ # message: Please provide your Amazon AWS credentials as environment variables.
18
+ # - name: AWS_ACCESS_SECRET
19
+ # message: Please provide your Amazon AWS credentials as environment variables.
20
+ # - name: HASHING_SALT
21
+ # production:
22
+ # <<: *defaults
23
+ # development:
24
+ # <<: *defaults
25
+ # {: lang="yaml" }
26
+ #
27
+ # You can define your required environment variables by name, and also provide
28
+ # an optional custom exception message to be used when that variable is not
29
+ # set.
30
+ #
31
+ # ### Initializing Envi
32
+ #
33
+ # To read the requirements and inspect the current environment variables,
34
+ # simply call `Envi.init`. This will raise an exception if requirements are not
35
+ # met. When working with Rails apps, you might want to use this is an
36
+ # initializer.
37
+ #
38
+ # ### Providing environment variables in a file
39
+ #
40
+ # A common pattern is to define custom environment variables in a `.env` file
41
+ # in your application's root directory. Other gems, such as [Foreman][] will
42
+ # use such a file to augment the environment before running processes. When in
43
+ # development mode, Envi can also do this for you. Simply tell it which file to
44
+ # use:
45
+ #
46
+ # Envi.init parse: '.env'
47
+ # {: lang="ruby" }
48
+ #
49
+ # It is a good idea to not include such a file (which commonly contains
50
+ # application secrets) in source control.
51
+ #
52
+ # ### Extending your Rails application configuration
53
+ #
54
+ # Envi will expose all the loaded environment variables as constants in the Envi
55
+ # module, so you can access them as `Envi::MY_VARIABLE`. You could also opt to extend
56
+ # your Rails configuration object:
57
+ #
58
+ # Envi.init use: MyApp::Application.config
59
+ # MyApp::Application.config.my_variable # => ...
60
+ #
61
+ #@author Arjan van der Gaag <arjan@arjanvandergaag.nl>
62
+ #
63
+ # [Heroku]: http://heroku.com
64
+ module Envi
65
+ # Special exception raised when required files could not be found. This is a
66
+ # library-specific wrapper around Errno::ENOENT.
67
+ FileNotFound = Class.new(StandardError)
68
+
69
+ # Special exception raised when the currently configured environment name is
70
+ # not present in the configuration file.
71
+ UnknownEnvironment = Class.new(StandardError)
72
+
73
+ # @return [String] standard location to look for a configuration YAML file
74
+ ENVI_DEFAULT_CONFIG_FILE = 'config/envars.yml'.freeze
75
+
76
+ # @return [String] the default environment to load from the configuration file
77
+ ENVI_DEFAULT_ENVIRONMENT = 'production'.freeze
78
+
79
+ # @return [String] Envi gem version in format major.minor.patch
80
+ VERSION = '0.0.1'.freeze
81
+
82
+ module_function
83
+
84
+ # Define the required environment variables defined in the configuration YAML
85
+ # file (or the `:config` option) as constants on the {Envi} module. Any
86
+ # previously defined constants will be removed.
87
+ #
88
+ # @param [Hash] options
89
+ # @option options [String] :parse path to a file to read extra environment
90
+ # variables from.
91
+ # @option options [Object] :use an object to set downcased properties on
92
+ # @option options [String] :config path to the configuration file. Defaults
93
+ # to value of {ENVI_DEFAULT_CONFIG_FILE}
94
+ # @option options [String] :enviroment name of the application environment.
95
+ # Defaults to {environment}.
96
+ # @raise {NameError} when a required environment variable is not set.
97
+ # @raise {FileNotFound} when files in the `:config` or `:parse` options could
98
+ # not be found.
99
+ # @raise {UnknownEnvironment} when the current environment is not defined in
100
+ # the configuration file.
101
+ # @return [Hash] the parsed YAML configuration file
102
+ def init(options = {})
103
+ reset_consts
104
+ parse_envars_from(options[:parse]) if options[:parse]
105
+ config(options).each do |envar|
106
+ name = envar.fetch('name')
107
+ value = ENV.fetch(name) do
108
+ raise NameError, envar.fetch('message', "Required environment variable #{name} is undefined")
109
+ end
110
+ set name, value, options
111
+ end
112
+ end
113
+
114
+ # Read and parse a given `filename` and extract environment variables from it.
115
+ #
116
+ # @example
117
+ # # in .env
118
+ # FOO=bar
119
+ #
120
+ # parse_envars_from('.env')
121
+ # ENV['FOO'] # => 'bar'
122
+ # @raise {FileNotFound} when `filename` is not readable
123
+ def parse_envars_from(filename)
124
+ contents = File.readlines(filename).each do |line|
125
+ name, value = line.chomp.split('=', 2)
126
+ ENV[name] = value
127
+ end
128
+ rescue Errno::ENOENT => e
129
+ raise FileNotFound, e
130
+ end
131
+
132
+ # Store a constant by its `name` and `value` by defining it as a constant in
133
+ # the Envi module. Optionally, this will call a setter method on the object
134
+ # in `options[:use]`.
135
+ #
136
+ # @param [String] name of the constant to define
137
+ # @param [String] value of the constant to define
138
+ # @param [Hash] options
139
+ # @option options [Object] :use an object to set downcased properties on
140
+ def set(name, value, options)
141
+ if object = options[:use]
142
+ object.send("#{name.downcase}=", value)
143
+ end
144
+ const_set name, value
145
+ (@_envi_constants ||= []) << name
146
+ end
147
+
148
+ # Loads the YAML configuration file and returns the keys under the {environment}.
149
+ #
150
+ # @param [Hash] options
151
+ # @option options [String] :config path to the configuration file. Defaults
152
+ # to value of {ENVI_DEFAULT_CONFIG_FILE}
153
+ # @option options [String] :enviroment name of the application environment.
154
+ # Defaults to {environment}.
155
+ # @raise {UnknownEnvironment} when the current environment is not defined in
156
+ # the configuration file.
157
+ # @see environment
158
+ def config(options)
159
+ location = options.fetch(:config, ENVI_DEFAULT_CONFIG_FILE)
160
+ env = options.fetch(:environment, environment)
161
+ YAML.load_file(location).fetch(env) do
162
+ raise UnknownEnvironment, 'No configuration found for environment ' + environment
163
+ end
164
+ rescue Errno::ENOENT => e
165
+ raise FileNotFound, e
166
+ end
167
+
168
+ # @return [String] the value of RACK_ENV, RAILS_ENV or {ENVI_DEFAULT_ENVIRONMENT}
169
+ def environment
170
+ ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENVI_DEFAULT_ENVIRONMENT
171
+ end
172
+
173
+ # Remove any constants previously defined with {set}.
174
+ def reset_consts
175
+ (@_envi_constants || []).each do |name|
176
+ remove_const name if const_defined? name
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,107 @@
1
+ require 'ostruct'
2
+
3
+ describe Envi do
4
+ it 'has a version number' do
5
+ expect(Envi::VERSION).to_not be_nil
6
+ end
7
+
8
+ context 'when the configuration file exists' do
9
+ before do
10
+ YAML.should_receive(:load_file).with('config/envars.yml').and_return({
11
+ 'production' => [
12
+ { 'name' => 'FOO' },
13
+ { 'name' => 'BAR', 'message' => 'Bar must be set' }
14
+ ],
15
+ 'development' => [
16
+ { 'name' => 'BAZ' },
17
+ ]
18
+ })
19
+ end
20
+
21
+ context "and all keys are set" do
22
+ before do
23
+ stub_const('ENV', 'FOO' => 'bla', 'BAR' => 'bla')
24
+ end
25
+
26
+ it 'reads configuration from YAML file and values from ENV' do
27
+ Envi.init
28
+ expect(Envi::FOO).to eql('bla')
29
+ end
30
+
31
+ it 'raises error for unrequired keys' do
32
+ Envi.init
33
+ expect { Envi::BAZ }.to raise_error(NameError)
34
+ end
35
+ end
36
+
37
+ context "when keys are missing" do
38
+ it 'raises when required keys are missing' do
39
+ stub_const('ENV', {})
40
+ expect { Envi.init }.to raise_error(NameError, 'Required environment variable FOO is undefined')
41
+ end
42
+
43
+ it 'uses description from config file as error message' do
44
+ stub_const('ENV', 'FOO' => 'qux')
45
+ expect { Envi.init }.to raise_error(NameError, 'Bar must be set')
46
+ end
47
+ end
48
+
49
+ it 'defaults to RAILS_ENV for environment' do
50
+ stub_const('ENV', 'BAZ' => 'bla', 'RAILS_ENV' => 'development')
51
+ Envi.init
52
+ expect(Envi::BAZ).to eql('bla')
53
+ expect { Envi::FOO }.to raise_error(NameError)
54
+ end
55
+
56
+ it 'defaults to RACK_ENV for environment' do
57
+ stub_const('ENV', 'BAZ' => 'bla', 'RACK_ENV' => 'development')
58
+ Envi.init
59
+ expect(Envi::BAZ).to eql('bla')
60
+ expect { Envi::FOO }.to raise_error(NameError)
61
+ end
62
+
63
+ it 'raises when configured environment does not exist' do
64
+ stub_const('ENV', 'RACK_ENV' => 'hoeaap')
65
+ expect { Envi.init }.to raise_error(Envi::UnknownEnvironment)
66
+ end
67
+
68
+ it 'allows defining a custom environment' do
69
+ stub_const('ENV', 'BAZ' => 'bla')
70
+ Envi.init(environment: 'development')
71
+ expect(Envi::BAZ).to eql('bla')
72
+ expect { Envi::FOO }.to raise_error(NameError)
73
+ end
74
+
75
+ it 'can preload environment variables from a file' do
76
+ stub_const('ENV', 'BAR' => 'bla')
77
+ File.should_receive(:readlines).with('.env').and_return(["FOO=qux\n"])
78
+ Envi.init parse: '.env'
79
+ expect(ENV['FOO']).to eql('qux')
80
+ end
81
+ end
82
+
83
+ context "when using an existing object to call setters on" do
84
+ subject { OpenStruct.new }
85
+
86
+ before do
87
+ YAML.should_receive(:load_file).with('config/envars.yml').and_return({
88
+ 'production' => [{ 'name' => 'FOO' }]
89
+ })
90
+ stub_const('ENV', 'FOO' => 'bar')
91
+ Envi.init(use: subject)
92
+ end
93
+
94
+ its(:foo) { should eql('bar') }
95
+ end
96
+
97
+ it 'allows loading a custom configuration file' do
98
+ YAML.should_receive(:load_file).with('foobar.yml').and_return({})
99
+ expect { Envi.init(config: 'foobar.yml') }.to raise_error(Envi::UnknownEnvironment)
100
+ end
101
+
102
+ context 'when the configuration file cannot be found' do
103
+ it 'raises an error' do
104
+ expect { Envi.init }.to raise_error(Envi::FileNotFound)
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: envi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arjan van der Gaag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '2.13'
69
+ description: Configure required environment variables in your Rails apps
70
+ email:
71
+ - arjan@arjanvandergaag.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - .yardopts
80
+ - Gemfile
81
+ - HISTORY.md
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - envi.gemspec
86
+ - lib/envi.rb
87
+ - spec/envi_spec.rb
88
+ homepage: https://github.com/avdgaag/envi
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Envi is a simple tool to make managing required environment variables for
112
+ your Rails application a little easier. It allows you to define the required variables
113
+ in a YAML file and provide sensible failure instructions.
114
+ test_files:
115
+ - spec/envi_spec.rb
116
+ has_rdoc: