envoker 1.0.0
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 +1 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +32 -0
- data/envoker.gemspec +17 -0
- data/lib/envoker.rb +28 -0
- data/lib/envoker/version.rb +3 -0
- data/makefile +5 -0
- data/test/.env.example +4 -0
- data/test/envoker_test.rb +56 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67b033c2bd3051d5cb80c453f5b30a94622cab9b
|
4
|
+
data.tar.gz: b942adccacf02a36daa334496e7a595a2c12df20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83fe89f611d068de214c9b6185235205a6a779ee24023db4dc4036ce4402a5067e46b1572aee49e8affc6f281bddd07098baabddb7756707a672587a134e2c5a
|
7
|
+
data.tar.gz: 9fae925702a80a405ea2d972bd2864f983971cb3675dc3887a57e16c8d8891d45b7d2fb223fdb2a55f9460def729156277b09c8ebd3e16b14135a020999abb97
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Francesco Rodríguez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
envoker
|
2
|
+
=======
|
3
|
+
|
4
|
+
Load environment variables from .env file.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
This is a common pattern we use in our applications:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require "envoker"
|
13
|
+
|
14
|
+
module Settings
|
15
|
+
Envoker.load
|
16
|
+
|
17
|
+
DATABASE_URL = Envoker.fetch("DATABASE_URL")
|
18
|
+
RACK_ENV = Envoker.fetch("RACK_ENV", "development")
|
19
|
+
|
20
|
+
# ...
|
21
|
+
end
|
22
|
+
|
23
|
+
puts(Settings::RACK_ENV)
|
24
|
+
# => "development"
|
25
|
+
```
|
26
|
+
|
27
|
+
Installation
|
28
|
+
------------
|
29
|
+
|
30
|
+
```
|
31
|
+
$ gem install envoker
|
32
|
+
```
|
data/envoker.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "lib/envoker/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "envoker"
|
5
|
+
s.version = Envoker::VERSION
|
6
|
+
s.summary = "Load environment variables from `.env`"
|
7
|
+
s.description = s.summary
|
8
|
+
s.authors = ["Francesco Rodríguez"]
|
9
|
+
s.email = ["frodsan@protonmail.ch"]
|
10
|
+
s.homepage = "https://github.com/harmoni/envoker"
|
11
|
+
s.license = "MIT"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler", "~> 1.10"
|
16
|
+
s.add_development_dependency "cutest", "~> 1.2"
|
17
|
+
end
|
data/lib/envoker.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative "envoker/version"
|
2
|
+
|
3
|
+
module Envoker
|
4
|
+
DEFAULT = ".env"
|
5
|
+
SETTINGS = /(.+)=(.*)$/
|
6
|
+
|
7
|
+
class KeyNotFound < StandardError
|
8
|
+
MESSAGE = "%s not found in .env file or environment settings."
|
9
|
+
|
10
|
+
def initialize(key)
|
11
|
+
super(sprintf(MESSAGE, key.inspect))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load(file = DEFAULT)
|
16
|
+
return unless File.exists?(file)
|
17
|
+
|
18
|
+
parse(file).each { |k, v| ENV[k] ||= k }
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.parse(file)
|
22
|
+
return File.read(file).scan(SETTINGS)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.fetch(key, default = nil)
|
26
|
+
return ENV.fetch(key, default) || raise(KeyNotFound, key)
|
27
|
+
end
|
28
|
+
end
|
data/makefile
ADDED
data/test/.env.example
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "cutest"
|
3
|
+
require_relative "../lib/envoker"
|
4
|
+
|
5
|
+
FILE = File.expand_path(".env.example", __dir__)
|
6
|
+
|
7
|
+
def test(*)
|
8
|
+
defaults = ENV.to_hash
|
9
|
+
super
|
10
|
+
ensure
|
11
|
+
ENV.replace(defaults)
|
12
|
+
end
|
13
|
+
|
14
|
+
scope "parse" do
|
15
|
+
test "ignore wrong environment lines" do
|
16
|
+
assert_equal 1, Envoker.parse(FILE).count
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
scope "load" do
|
21
|
+
test "don't raise if file doesn't exist" do
|
22
|
+
assert !Envoker.load(".notexists")
|
23
|
+
end
|
24
|
+
|
25
|
+
test "load environment vars" do
|
26
|
+
Envoker.load(FILE)
|
27
|
+
|
28
|
+
assert_equal "secret", ENV["secret"]
|
29
|
+
end
|
30
|
+
|
31
|
+
test "don't override environment vars" do
|
32
|
+
ENV["secret"] = "supersecret"
|
33
|
+
|
34
|
+
Envoker.load(FILE)
|
35
|
+
|
36
|
+
assert_equal "supersecret", ENV["secret"]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
scope "fetch" do
|
41
|
+
test "return value from ENV" do
|
42
|
+
Envoker.load(FILE)
|
43
|
+
|
44
|
+
assert_equal "secret", Envoker.fetch("secret")
|
45
|
+
end
|
46
|
+
|
47
|
+
test "return default if not exists" do
|
48
|
+
assert_equal "secret", Envoker.fetch("notexists", "secret")
|
49
|
+
end
|
50
|
+
|
51
|
+
test "raise error if key not exists" do
|
52
|
+
assert_raise do
|
53
|
+
Envoker.fetch("notexists")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: envoker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Rodríguez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-06 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cutest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
description: Load environment variables from `.env`
|
42
|
+
email:
|
43
|
+
- frodsan@protonmail.ch
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- envoker.gemspec
|
53
|
+
- lib/envoker.rb
|
54
|
+
- lib/envoker/version.rb
|
55
|
+
- makefile
|
56
|
+
- test/.env.example
|
57
|
+
- test/envoker_test.rb
|
58
|
+
homepage: https://github.com/harmoni/envoker
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Load environment variables from `.env`
|
82
|
+
test_files: []
|