envee 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +7 -0
- data/envee.gemspec +23 -0
- data/lib/envee.rb +47 -0
- data/lib/envee/version.rb +3 -0
- data/spec/envee_spec.rb +198 -0
- data/spec/spec_helper.rb +2 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f94f269e8d206155b0b450605a6d6c8583b691e
|
4
|
+
data.tar.gz: 7c00b33fbdabc979917bff9252082061953b4b66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40bb0f11403e0a96e5ad0fcdbb3c7d528b849854c202ffa0d6402635e5dc69dbd833acaec456fa247b2f8a4c57146b33d896e51b3291df7a6a66cd1830b8731b
|
7
|
+
data.tar.gz: 283fb912b6021af77e8511d5cf5cc5ddd9261fc71d0e1e9564830c222d1d241dd94958dc187560a16934c95f0f15eb1bb313206268e46b4865e6849761d29faf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Allen Madsen
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Envee
|
2
|
+
|
3
|
+
Provides casting wrappers around fetch for environment variables.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'envee'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install envee
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
ENV['NUM'] = '1'
|
25
|
+
ENV.int('NUM') #=> 1
|
26
|
+
|
27
|
+
ENV['NAME'] = 'bob'
|
28
|
+
ENV.str('NAME') #=> 'bob'
|
29
|
+
|
30
|
+
ENV['TIME'] = '1970-01-01 00:00:00 UTC'
|
31
|
+
ENV.time('TIME') #=> 1970-01-01 00:00:00 UTC
|
32
|
+
|
33
|
+
ENV['ITIME'] = 0
|
34
|
+
ENV.int_time('ITIME') #=> 1970-01-01 00:00:00 UTC
|
35
|
+
|
36
|
+
ENV['START'] = 'false' # or no, off, 0. Everything else is true.
|
37
|
+
ENV.bool(START) #=> falsey
|
38
|
+
|
39
|
+
# Specify defaults like you do with fetch
|
40
|
+
ENV.int('MISSING', '1') #=> 1, also casts default
|
41
|
+
ENV.time('TIME2'){Time.at(0)} #=> 1970-01-01 00:00:00 UTC
|
42
|
+
|
43
|
+
ENV['MISSING'] = 'CHANGEME'
|
44
|
+
ENV.validate!(missing_value: 'CHANGEME') #=> Envee::MissingValuesError,
|
45
|
+
# The following environment variables are not set, but should be:
|
46
|
+
# MISSING
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/secondrotation/envee/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/envee.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'envee/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "envee"
|
8
|
+
spec.version = Envee::VERSION
|
9
|
+
spec.authors = ["Allen Madsen"]
|
10
|
+
spec.email = ["blatyo@gmail.com"]
|
11
|
+
spec.summary = 'Provides casting wrappers around fetch for environment variables.'
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
data/lib/envee.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'envee/version'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module Envee
|
5
|
+
class MissingValuesError < StandardError
|
6
|
+
def initialize(missing_keys)
|
7
|
+
super(
|
8
|
+
"The following environment variables are not set, but should be:\n"\
|
9
|
+
"#{missing_keys.join("\n")}\n"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def int(*args, &block)
|
15
|
+
Integer(fetch(*args, &block))
|
16
|
+
end
|
17
|
+
|
18
|
+
def str(*args, &block)
|
19
|
+
String(fetch(*args, &block))
|
20
|
+
end
|
21
|
+
|
22
|
+
def bool(*args, &block)
|
23
|
+
value = fetch(*args, &block)
|
24
|
+
|
25
|
+
value && value !~ /^(0|no|false|off|)$/i
|
26
|
+
end
|
27
|
+
|
28
|
+
def time(*args, &block)
|
29
|
+
value = fetch(*args, &block)
|
30
|
+
return value if value.is_a?(Time)
|
31
|
+
Time.parse(value).utc
|
32
|
+
end
|
33
|
+
|
34
|
+
def int_time(*args, &block)
|
35
|
+
value = Integer(fetch(*args, &block))
|
36
|
+
return value if value.is_a?(Time)
|
37
|
+
Time.at(value).utc
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate!(options)
|
41
|
+
missing = options[:missing_value]
|
42
|
+
missing_keys = select{|k, v| v.include?(missing)}.map(&:first)
|
43
|
+
raise MissingValuesError.new(missing_keys) unless missing_keys.empty?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
ENV.extend(Envee)
|
data/spec/envee_spec.rb
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Envee do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(Envee::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:env){{}.extend(described_class)}
|
9
|
+
|
10
|
+
describe '#int' do
|
11
|
+
context 'when requesting a value in env without default' do
|
12
|
+
it 'returns env value casted as integer' do
|
13
|
+
env['NUM'] = '1'
|
14
|
+
expect(env.int('NUM')).to eq(1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when requesting a value not in env with a default value' do
|
19
|
+
it 'returns the default value casted as integer' do
|
20
|
+
expect(env.int('NUM', '2')).to eq(2)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when requesting a value not in env with a default block' do
|
25
|
+
it 'returns the default block value casted as integer' do
|
26
|
+
expect(env.int('NUM'){'2'}).to eq(2)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when requesting a value not in env with no default' do
|
31
|
+
it 'raises an KeyError' do
|
32
|
+
expect{env.int('NUM')}.to raise_error(KeyError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when requesting a value in env that cannot be casted to integer' do
|
37
|
+
it 'raises an ArgumentError' do
|
38
|
+
env['NUM'] = 'a'
|
39
|
+
expect{env.int('NUM')}.to raise_error(ArgumentError)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#str' do
|
45
|
+
context 'when requesting a value in env without default' do
|
46
|
+
it 'returns env value casted as string' do
|
47
|
+
env['NUM'] = '1'
|
48
|
+
expect(env.str('NUM')).to eq('1')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when requesting a value not in env with a default value' do
|
53
|
+
it 'returns the default value casted as string' do
|
54
|
+
expect(env.str('NUM', 2)).to eq('2')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when requesting a value not in env with a default block' do
|
59
|
+
it 'returns the default block value casted as string' do
|
60
|
+
expect(env.str('NUM'){2}).to eq('2')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when requesting a value not in env with no default' do
|
65
|
+
it 'raises an KeyError' do
|
66
|
+
expect{env.str('NUM')}.to raise_error(KeyError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#bool' do
|
72
|
+
context 'when requesting a value in env without default' do
|
73
|
+
it 'returns env value casted as truthy' do
|
74
|
+
env['START'] = 'true'
|
75
|
+
expect(env.bool('START')).to be_truthy
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when requesting a value not in env with a default value' do
|
80
|
+
it 'returns the default value casted as truthy' do
|
81
|
+
expect(env.bool('START', '2')).to be_truthy
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when requesting a value not in env with a default block' do
|
86
|
+
it 'returns the default block value casted as integer' do
|
87
|
+
expect(env.bool('START'){'2'}).to be_truthy
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
%w(0 false no off).each do |false_value|
|
92
|
+
context "when the env value is #{false_value}" do
|
93
|
+
it 'returns a false value' do
|
94
|
+
env['START'] = false_value
|
95
|
+
expect(env.bool('START')).to be_falsy
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when requesting a value not in env with no default' do
|
101
|
+
it 'raises an KeyError' do
|
102
|
+
expect{env.bool('START')}.to raise_error(KeyError)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#time' do
|
108
|
+
context 'when requesting a value in env without default' do
|
109
|
+
it 'returns env value casted as time' do
|
110
|
+
env['TIME'] = '1970-01-01 00:00:00 UTC'
|
111
|
+
expect(env.time('TIME')).to eq(Time.at(0))
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'when requesting a value not in env with a default value' do
|
116
|
+
it 'returns the default value casted as time' do
|
117
|
+
expect(env.time('TIME', '1970-01-01 00:00:01 UTC')).to eq(Time.at(1))
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when requesting a value not in env with a default block' do
|
122
|
+
it 'returns the default block value casted as time' do
|
123
|
+
expect(env.time('TIME'){'1970-01-01 00:00:01 UTC'}).to eq(Time.at(1))
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when requesting a value not in env with a default block and default already is a time' do
|
128
|
+
it 'returns the default block value casted as time' do
|
129
|
+
expect(env.time('TIME'){Time.at(1)}).to eq(Time.at(1))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when requesting a value not in env with no default' do
|
134
|
+
it 'raises an KeyError' do
|
135
|
+
expect{env.time('TIME')}.to raise_error(KeyError)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when requesting a value in env that cannot be casted to time' do
|
140
|
+
it 'raises an ArgumentError' do
|
141
|
+
env['TIME'] = 'a'
|
142
|
+
expect{env.time('TIME')}.to raise_error(ArgumentError)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#int_time' do
|
148
|
+
context 'when requesting a value in env without default' do
|
149
|
+
it 'returns env value casted as time' do
|
150
|
+
env['TIME'] = '0'
|
151
|
+
expect(env.int_time('TIME')).to eq(Time.at(0))
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'when requesting a value not in env with a default value' do
|
156
|
+
it 'returns the default value casted as time' do
|
157
|
+
expect(env.int_time('TIME', '1')).to eq(Time.at(1))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when requesting a value not in env with a default block' do
|
162
|
+
it 'returns the default block value casted as time' do
|
163
|
+
expect(env.int_time('TIME'){'1'}).to eq(Time.at(1))
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'when requesting a value not in env with no default' do
|
168
|
+
it 'raises an KeyError' do
|
169
|
+
expect{env.int_time('TIME')}.to raise_error(KeyError)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when requesting a value in env that cannot be casted to time' do
|
174
|
+
it 'raises an ArgumentError' do
|
175
|
+
env['TIME'] = 'a'
|
176
|
+
expect{env.int_time('TIME')}.to raise_error(ArgumentError)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe '#validate!' do
|
182
|
+
context 'when no missing values are found' do
|
183
|
+
it 'does not raise an error' do
|
184
|
+
expect{env.validate!(missing_value: 'CHANGEME')}.to_not raise_error
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context 'when there are missing values' do
|
189
|
+
it 'raises an error with all the missing values keys' do
|
190
|
+
env['A'] = env['B'] = 'CHANGEME'
|
191
|
+
expect{env.validate!(missing_value: 'CHANGEME')}.to raise_error(
|
192
|
+
Envee::MissingValuesError,
|
193
|
+
"The following environment variables are not set, but should be:\nB\nA\n"
|
194
|
+
)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: envee
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Allen Madsen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-18 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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
|
+
description:
|
56
|
+
email:
|
57
|
+
- blatyo@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- envee.gemspec
|
69
|
+
- lib/envee.rb
|
70
|
+
- lib/envee/version.rb
|
71
|
+
- spec/envee_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Provides casting wrappers around fetch for environment variables.
|
97
|
+
test_files:
|
98
|
+
- spec/envee_spec.rb
|
99
|
+
- spec/spec_helper.rb
|