envconfig 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +97 -7
- data/lib/envconfig/database.rb +32 -0
- data/lib/envconfig/memcached.rb +30 -0
- data/lib/envconfig/provider.rb +6 -1
- data/lib/envconfig/redis.rb +61 -0
- data/lib/envconfig/smtp.rb +15 -2
- data/lib/envconfig/url_parser.rb +50 -0
- data/lib/envconfig/version.rb +1 -1
- data/lib/envconfig.rb +56 -4
- data/spec/database_spec.rb +36 -0
- data/spec/envconfig_spec.rb +17 -0
- data/spec/memcached_spec.rb +31 -0
- data/spec/redis_spec.rb +21 -0
- data/spec/root_spec.rb +104 -11
- data/spec/shared_examples.rb +37 -0
- data/spec/smtp_spec.rb +19 -9
- data/spec/spec_helper.rb +2 -0
- data/spec/url_parser_spec.rb +44 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ae15014bb2a42caac46541701ad0eeedecf4945
|
4
|
+
data.tar.gz: 291af5950eae465a0ebe2a411374ddcde9ddff16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d227e3a0307aad586efe1dc3c81ee7f089920fbea191ff6e0f00a9659f7c6c8089745e1fda711d56316e299446b4c307d8eb7db809d1077a6033deaaf02265b
|
7
|
+
data.tar.gz: 9ef22e14457b7fc0297061d0a9a97fb1f1cf1bf45701d8dbb4c5135772cc3082cf1aa2ede4163a99699e9c0e0fc544c3fd76eaff034bc462a7f4b558c8cf5fc4
|
data/README.md
CHANGED
@@ -17,12 +17,13 @@ POSTMARK_API_KEY="bcca0a78abbaed6533f3c8017b804bda"
|
|
17
17
|
Then envconfig's SMTP configuration will look like this:
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
Envconfig.load
|
20
|
+
Envconfig.load.smtp.to_h # =>
|
21
21
|
{
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
port: "25",
|
23
|
+
authentication: :plain,
|
24
|
+
address: "smtp.example.org",
|
25
|
+
user_name: "bcca0a78abbaed6533f3c8017b804bda",
|
26
|
+
password: "bcca0a78abbaed6533f3c8017b804bda"
|
26
27
|
}
|
27
28
|
```
|
28
29
|
|
@@ -41,17 +42,106 @@ or `gem install envconfig`.
|
|
41
42
|
Add `envconfig-rails` to your `Gemfile` and go.
|
42
43
|
|
43
44
|
If you're not using Rails, add the base `envconfig` gem, and access the
|
44
|
-
|
45
|
+
configuration like this:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
Envconfig.load.smtp[:address] => "example.org"
|
49
|
+
Envconfig.load.smtp.to_h # => {address: "example.org", ...}
|
50
|
+
```
|
45
51
|
|
46
52
|
|
47
53
|
## Supported Add-ons
|
48
54
|
|
55
|
+
### Database
|
56
|
+
|
57
|
+
* Generic (`DATABASE_URL` in `ENV`)
|
58
|
+
|
59
|
+
Database example:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
ENV["DATABASE_URL"] = "postgres://ab:secret@example.org:1234/db?encoding=utf-8"
|
63
|
+
|
64
|
+
Envconfig.load.database.to_h # =>
|
65
|
+
{
|
66
|
+
url: "postgres://ab:secret@example.org:1234/db?encoding=utf-8",
|
67
|
+
adapter: "postgresql",
|
68
|
+
database: "db",
|
69
|
+
username: "ab",
|
70
|
+
password: "secret",
|
71
|
+
host: "example.org",
|
72
|
+
port: 1234,
|
73
|
+
encoding: "utf-8"
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
|
49
80
|
### SMTP
|
50
81
|
|
51
82
|
* Postmark ([Broadstack](https://broadstack.com/addons/postmark), [Heroku](https://addons.heroku.com/postmark))
|
52
83
|
* Mandrill ([Heroku](https://addons.heroku.com/mandrill))
|
53
84
|
* SendGrid ([Heroku](https://addons.heroku.com/sendgrid))
|
54
|
-
*
|
85
|
+
* Mailgun ([Heroku](https://address.Heroku.com/mailgun))
|
86
|
+
* Generic (`SMTP_HOST`, `SMTP_PORT`, `SMTP_USERNAME`, `SMTP_PASSWORD` in `ENV`)
|
87
|
+
|
88
|
+
SMTP example:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
ENV["MANDRILL_USERNAME"] = "mandrilluser"
|
92
|
+
ENV["MANDRILL_APIKEY"] = SecureRandom.hex
|
93
|
+
|
94
|
+
Envconfig.load.smtp.to_h # =>
|
95
|
+
{
|
96
|
+
address: "smtp.mandrillapp.com",
|
97
|
+
port: "587",
|
98
|
+
user_name: "mandrilluser",
|
99
|
+
password: "14941007dfdc2af76e2fafe1383cf33b"
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
### memcached
|
104
|
+
|
105
|
+
* MemCachier ([Broadstack](https://broadstack.com/addons/memcachier), [Heroku](https://addons.heroku.com/memcachier))
|
106
|
+
|
107
|
+
memcached example:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
ENV["MEMCACHIER_SERVERS"] = "a.example.org:11211,b.example.com:11211"
|
111
|
+
ENV["MEMCACHIER_USERNAME"] = "memcachieruser"
|
112
|
+
ENV["MEMCACHIER_PASSWORD"] = SecureRandom.hex
|
113
|
+
|
114
|
+
Envconfig.load.memcached.to_h # =>
|
115
|
+
{
|
116
|
+
servers: "a.example.org:11211,b.example.com:11211",
|
117
|
+
username: "memcachieruser",
|
118
|
+
password: "9d1d72d6828ed59bab3d1496da71ba59",
|
119
|
+
server_strings: ["a.example.org:11211", "b.example.com:11211"]
|
120
|
+
}
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
### Redis
|
125
|
+
|
126
|
+
* openredis ([Heroku](https://addons.heroku.com/openredis))
|
127
|
+
* RedisCloud ([Heroku](https://addons.heroku.com/rediscloud))
|
128
|
+
* RedisGreen ([Heroku](https://addons.heroku.com/redisgreen))
|
129
|
+
* Redis To Go ([Heroku](https://addons.heroku.com/redistogo))
|
130
|
+
|
131
|
+
Redis example:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
ENV["OPENREDIS_URL"] = "redis://:secrettoken@127.0.0.1:6379"
|
135
|
+
|
136
|
+
Envconfig.load.redis.to_h # =>
|
137
|
+
{
|
138
|
+
url: "redis://:secrettoken@127.0.0.1:6379",
|
139
|
+
host: "127.0.0.1",
|
140
|
+
port: 6379,
|
141
|
+
user: "",
|
142
|
+
password: "secrettoken",
|
143
|
+
}
|
144
|
+
```
|
55
145
|
|
56
146
|
|
57
147
|
## Contributing
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Envconfig
|
2
|
+
class Database
|
3
|
+
|
4
|
+
include Service
|
5
|
+
|
6
|
+
def self.providers
|
7
|
+
[
|
8
|
+
Generic,
|
9
|
+
]
|
10
|
+
end
|
11
|
+
|
12
|
+
class Generic
|
13
|
+
include Provider
|
14
|
+
def mapping
|
15
|
+
{url: "DATABASE_URL"}
|
16
|
+
end
|
17
|
+
def filter_config(config)
|
18
|
+
url = UrlParser.new(config[:url])
|
19
|
+
parts = url.extract_as(
|
20
|
+
adapter: ->(u){ (u.scheme || "").sub(/\Apostgres\z/, "postgresql") },
|
21
|
+
database: ->(u){ (u.path || "").split("/")[1] },
|
22
|
+
username: :user,
|
23
|
+
password: :password,
|
24
|
+
host: :host,
|
25
|
+
port: :port,
|
26
|
+
)
|
27
|
+
config.merge!(parts).merge!(url.query_values)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Envconfig
|
2
|
+
class Memcached
|
3
|
+
|
4
|
+
include Service
|
5
|
+
|
6
|
+
def self.providers
|
7
|
+
[
|
8
|
+
Memcachier,
|
9
|
+
]
|
10
|
+
end
|
11
|
+
|
12
|
+
class Memcachier
|
13
|
+
include Provider
|
14
|
+
def name; "MemCachier" end
|
15
|
+
def mapping
|
16
|
+
{
|
17
|
+
servers: "MEMCACHIER_SERVERS",
|
18
|
+
username: "MEMCACHIER_USERNAME",
|
19
|
+
password: "MEMCACHIER_PASSWORD",
|
20
|
+
}
|
21
|
+
end
|
22
|
+
def filter_config(hash)
|
23
|
+
hash.merge!(
|
24
|
+
server_strings: hash[:servers].split(",")
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/envconfig/provider.rb
CHANGED
@@ -42,7 +42,12 @@ module Envconfig
|
|
42
42
|
|
43
43
|
# The configuration derived from the environment for this provider.
|
44
44
|
def config
|
45
|
-
static.merge(dynamic)
|
45
|
+
filter_config(static.merge(dynamic))
|
46
|
+
end
|
47
|
+
|
48
|
+
# A hook for arbitrary changes to the config hash.
|
49
|
+
def filter_config(config)
|
50
|
+
config
|
46
51
|
end
|
47
52
|
|
48
53
|
private
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module Envconfig
|
4
|
+
class Redis
|
5
|
+
|
6
|
+
include Service
|
7
|
+
|
8
|
+
def self.providers
|
9
|
+
[
|
10
|
+
Openredis,
|
11
|
+
Rediscloud,
|
12
|
+
Redisgreen,
|
13
|
+
Redistogo,
|
14
|
+
]
|
15
|
+
end
|
16
|
+
|
17
|
+
module ConfigFilter
|
18
|
+
def filter_config(config)
|
19
|
+
url = UrlParser.new(config[:url])
|
20
|
+
config.merge!(url.extract(:host, :port, :user, :password))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Openredis
|
25
|
+
include Provider
|
26
|
+
include ConfigFilter
|
27
|
+
def name; "openredis" end
|
28
|
+
def mapping
|
29
|
+
{url: "OPENREDIS_URL"}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Rediscloud
|
34
|
+
include Provider
|
35
|
+
include ConfigFilter
|
36
|
+
def name; "Redis Cloud" end
|
37
|
+
def mapping
|
38
|
+
{url: "REDISCLOUD_URL"}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Redisgreen
|
43
|
+
include Provider
|
44
|
+
include ConfigFilter
|
45
|
+
def name; "RedisGreen" end
|
46
|
+
def mapping
|
47
|
+
{url: "REDISGREEN_URL"}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Redistogo
|
52
|
+
include Provider
|
53
|
+
include ConfigFilter
|
54
|
+
def name; "Redis To Go" end
|
55
|
+
def mapping
|
56
|
+
{url: "REDISTOGO_URL"}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
data/lib/envconfig/smtp.rb
CHANGED
@@ -5,7 +5,8 @@ module Envconfig
|
|
5
5
|
|
6
6
|
def self.providers
|
7
7
|
[
|
8
|
-
|
8
|
+
Generic,
|
9
|
+
Mailgun,
|
9
10
|
Mandrill,
|
10
11
|
Postmark,
|
11
12
|
Sendgrid,
|
@@ -13,7 +14,7 @@ module Envconfig
|
|
13
14
|
end
|
14
15
|
|
15
16
|
# A custom configuration, for local or self-managed SMTP servers.
|
16
|
-
class
|
17
|
+
class Generic
|
17
18
|
include Provider
|
18
19
|
def valid?
|
19
20
|
mapping.values.any? { |k| env.key?(k) } #any? instead of #all?
|
@@ -28,6 +29,18 @@ module Envconfig
|
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
32
|
+
class Mailgun
|
33
|
+
include Provider
|
34
|
+
def mapping
|
35
|
+
{
|
36
|
+
port: "MAILGUN_SMTP_PORT",
|
37
|
+
address: "MAILGUN_SMTP_SERVER",
|
38
|
+
user_name: "MAILGUN_SMTP_LOGIN",
|
39
|
+
password: "MAILGUN_SMTP_PASSWORD",
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
31
44
|
class Mandrill
|
32
45
|
include Provider
|
33
46
|
def mapping
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "cgi"
|
3
|
+
|
4
|
+
module Envconfig
|
5
|
+
class UrlParser
|
6
|
+
|
7
|
+
def initialize(url)
|
8
|
+
@url = url
|
9
|
+
end
|
10
|
+
|
11
|
+
# Extract the given keys verbatim from the URL.
|
12
|
+
# These map to public instance methods on URI.
|
13
|
+
def extract(*keys)
|
14
|
+
keys.inject({}) do |hash, key|
|
15
|
+
hash.merge!(key => parsed_url.public_send(key))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Extract and rename the given keys.
|
20
|
+
# The mapping keys are the wanted keys, the mapping values
|
21
|
+
# are public instance methods on URI.
|
22
|
+
def extract_as(mapping)
|
23
|
+
mapping.inject({}) do |hash, (key, method)|
|
24
|
+
hash.merge!(key => extract_value(method))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Query string as hash with symbol keys.
|
29
|
+
def query_values
|
30
|
+
CGI.parse(parsed_url.query || "").inject({}) do |hash, (key, values)|
|
31
|
+
hash.merge!(key.to_sym => values.first)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def parsed_url
|
38
|
+
@_parsed_url ||= URI.parse(@url)
|
39
|
+
end
|
40
|
+
|
41
|
+
def extract_value(method)
|
42
|
+
if method.respond_to?(:call)
|
43
|
+
method.call(parsed_url)
|
44
|
+
else
|
45
|
+
parsed_url.public_send(method)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
data/lib/envconfig/version.rb
CHANGED
data/lib/envconfig.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
|
-
require "envconfig/provider"
|
2
1
|
require "envconfig/service"
|
2
|
+
require "envconfig/provider"
|
3
|
+
require "envconfig/url_parser"
|
4
|
+
|
5
|
+
require "envconfig/database"
|
6
|
+
require "envconfig/memcached"
|
7
|
+
require "envconfig/redis"
|
3
8
|
require "envconfig/smtp"
|
9
|
+
|
4
10
|
require "envconfig/version"
|
5
11
|
|
6
12
|
module Envconfig
|
7
13
|
|
8
|
-
def self.load(env)
|
14
|
+
def self.load(env = ENV)
|
9
15
|
Root.new(env)
|
10
16
|
end
|
11
17
|
|
@@ -13,14 +19,60 @@ module Envconfig
|
|
13
19
|
|
14
20
|
def initialize(env)
|
15
21
|
@env = env
|
22
|
+
@_services = {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_h
|
26
|
+
[
|
27
|
+
:database,
|
28
|
+
:memcached,
|
29
|
+
:redis,
|
30
|
+
:smtp,
|
31
|
+
].inject({}) do |hash, service|
|
32
|
+
hash.merge!(service => public_send(service).to_h)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def database
|
37
|
+
service_for(:database, Database)
|
38
|
+
end
|
39
|
+
|
40
|
+
def database?
|
41
|
+
predicate_for(:database)
|
42
|
+
end
|
43
|
+
|
44
|
+
def memcached
|
45
|
+
service_for(:memcached, Memcached)
|
46
|
+
end
|
47
|
+
|
48
|
+
def memcached?
|
49
|
+
predicate_for(:memcached)
|
50
|
+
end
|
51
|
+
|
52
|
+
def redis
|
53
|
+
service_for(:redis, Redis)
|
54
|
+
end
|
55
|
+
|
56
|
+
def redis?
|
57
|
+
predicate_for(:redis)
|
16
58
|
end
|
17
59
|
|
18
60
|
def smtp
|
19
|
-
|
61
|
+
service_for(:smtp, Smtp)
|
20
62
|
end
|
21
63
|
|
22
64
|
def smtp?
|
23
|
-
smtp
|
65
|
+
predicate_for(:smtp)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def service_for(name, klass)
|
71
|
+
@_services[name] || klass.new(@env)
|
72
|
+
end
|
73
|
+
|
74
|
+
def predicate_for(name)
|
75
|
+
public_send(name).to_h.any?
|
24
76
|
end
|
25
77
|
|
26
78
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "database configuration" do
|
4
|
+
|
5
|
+
database_url = "postgres://user:pass@example.org:5432/db?a=b&c=d"
|
6
|
+
|
7
|
+
let(:env) { {} }
|
8
|
+
let(:config) { Envconfig.load(env).database }
|
9
|
+
|
10
|
+
context "with nothing relevant in ENV" do
|
11
|
+
it_behaves_like "empty configuration"
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with DATABASE_URL='#{database_url}' in ENV" do
|
15
|
+
before do
|
16
|
+
env["DATABASE_URL"] = database_url
|
17
|
+
end
|
18
|
+
|
19
|
+
{
|
20
|
+
url: database_url,
|
21
|
+
adapter: "postgresql", # transformed like heroku-buildpack-ruby
|
22
|
+
database: "db",
|
23
|
+
username: "user",
|
24
|
+
password: "pass",
|
25
|
+
host: "example.org",
|
26
|
+
port: 5432,
|
27
|
+
a: "b",
|
28
|
+
c: "d",
|
29
|
+
}.each do |key, value|
|
30
|
+
it "sets :#{key} to #{value.inspect}" do
|
31
|
+
expect(config[key]).to eq(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Envconfig do
|
4
|
+
|
5
|
+
it "loads from an ENV-like hash" do
|
6
|
+
result = Envconfig.load({"KEY" => "value"})
|
7
|
+
expect(result.class).to eq(Envconfig::Root)
|
8
|
+
end
|
9
|
+
|
10
|
+
# I don't want to modify or make assumptions about ENV,
|
11
|
+
# but at least test that Envconfig.load() handles zero arguments.
|
12
|
+
it "loads without an argument (defaulting to ENV)" do
|
13
|
+
result = Envconfig.load()
|
14
|
+
expect(result.class).to eq(Envconfig::Root)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "memcached configuration" do
|
4
|
+
|
5
|
+
let(:env) { {} }
|
6
|
+
subject(:config) { Envconfig.load(env).memcached }
|
7
|
+
|
8
|
+
context "with nothing relevant in ENV" do
|
9
|
+
it_behaves_like "empty configuration"
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with MemCachier in ENV" do
|
13
|
+
before do
|
14
|
+
env["MEMCACHIER_SERVERS"] = "m1.example.org:11211,m2.example.org:11211"
|
15
|
+
env["MEMCACHIER_USERNAME"] = "memcachieruser"
|
16
|
+
env["MEMCACHIER_PASSWORD"] = "memcachierpass"
|
17
|
+
end
|
18
|
+
|
19
|
+
{
|
20
|
+
servers: "m1.example.org:11211,m2.example.org:11211",
|
21
|
+
username: "memcachieruser",
|
22
|
+
password: "memcachierpass",
|
23
|
+
server_strings: ["m1.example.org:11211", "m2.example.org:11211"],
|
24
|
+
}.each do |key, value|
|
25
|
+
it "sets :#{key} to #{value.inspect}" do
|
26
|
+
expect(config[key]).to eq(value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/spec/redis_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "redis configuration" do
|
4
|
+
|
5
|
+
let(:env) { {} }
|
6
|
+
subject(:config) { Envconfig.load(env).redis }
|
7
|
+
|
8
|
+
context "with nothing relevant in ENV" do
|
9
|
+
it_behaves_like "empty configuration"
|
10
|
+
end
|
11
|
+
|
12
|
+
%w{
|
13
|
+
OPENREDIS_URL
|
14
|
+
REDISCLOUD_URL
|
15
|
+
REDISGREEN_URL
|
16
|
+
REDISTOGO_URL
|
17
|
+
}.each do |env_key|
|
18
|
+
it_behaves_like "redis configuration", env_key
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/root_spec.rb
CHANGED
@@ -3,27 +3,120 @@ require "spec_helper"
|
|
3
3
|
describe Envconfig::Root do
|
4
4
|
|
5
5
|
let(:env) { {} }
|
6
|
-
subject(:root) { Envconfig.
|
6
|
+
subject(:root) { Envconfig::Root.new(env) }
|
7
7
|
|
8
8
|
context "with nothing in ENV" do
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
service_methods = [
|
11
|
+
:database,
|
12
|
+
:memcached,
|
13
|
+
:redis,
|
14
|
+
:smtp,
|
15
|
+
]
|
16
|
+
|
17
|
+
service_methods.each do |service_method|
|
18
|
+
predicate_method = :"#{service_method}?"
|
19
|
+
|
20
|
+
describe "##{predicate_method}" do
|
21
|
+
it "is false" do
|
22
|
+
expect(root.public_send(predicate_method)).to eq(false)
|
23
|
+
end
|
13
24
|
end
|
14
|
-
end
|
15
25
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
26
|
+
describe "##{service_method}" do
|
27
|
+
[:to_h, :provider, :[]].each do |m|
|
28
|
+
it "claims to respond to :#{m}" do
|
29
|
+
expect(root.smtp.respond_to?(m)).to eq(true)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
it "has an empty #to_h result" do
|
33
|
+
expect(root.smtp.to_h).to eq({})
|
34
|
+
end
|
35
|
+
it "responds to #provider with an object with no name" do
|
36
|
+
expect(root.smtp.provider.name).to eq(nil)
|
20
37
|
end
|
21
38
|
end
|
22
|
-
|
23
|
-
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#to_h" do
|
42
|
+
it "returns a nested hash of all configuration" do
|
43
|
+
expect(root.to_h).to eq(
|
44
|
+
database: {},
|
45
|
+
memcached: {},
|
46
|
+
redis: {},
|
47
|
+
smtp: {},
|
48
|
+
)
|
24
49
|
end
|
25
50
|
end
|
26
51
|
|
27
52
|
end
|
28
53
|
|
54
|
+
context "with all of the things in ENV" do
|
55
|
+
let(:env) do
|
56
|
+
# Note: actual precedence is defined in the service classes,
|
57
|
+
# generally based on alphabetical sorting.
|
58
|
+
# This example ENV intentionally mixes up the precedence ordering.
|
59
|
+
# e.g. the Redis services match their actual precedence,
|
60
|
+
# but the SMTP services reverse their actual precedence.
|
61
|
+
{
|
62
|
+
"DATABASE_URL" => "postgres://u:p@db.example.org:1234/db",
|
63
|
+
"MEMCACHIER_SERVERS" => "mc.example.org:11211",
|
64
|
+
"MEMCACHIER_USERNAME" => "mcuser",
|
65
|
+
"MEMCACHIER_PASSWORD" => "mcpass",
|
66
|
+
"OPENREDIS_URL" => "redis://:orpass@or.example.org:2345",
|
67
|
+
"REDISTOGO_URL" => "redis://:rtgpass@rtg.example.org:3456",
|
68
|
+
"SENDGRID_USERNAME" => "sguser",
|
69
|
+
"SENDGRID_PASSWORD" => "sgpass",
|
70
|
+
"POSTMARK_SMTP_SERVER" => "pm.example.org",
|
71
|
+
"POSTMARK_API_KEY" => "pmkey",
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
{
|
76
|
+
database: "Generic",
|
77
|
+
memcached: "MemCachier",
|
78
|
+
redis: "openredis",
|
79
|
+
smtp: "Postmark"
|
80
|
+
}.each do |service, name|
|
81
|
+
it "configures #{service} with #{name}" do
|
82
|
+
expect(root.public_send(service).provider.name).to eq(name)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it "has a giant #to_h" do
|
87
|
+
expect(root.to_h).to eq(
|
88
|
+
database: {
|
89
|
+
url: "postgres://u:p@db.example.org:1234/db",
|
90
|
+
adapter: "postgresql",
|
91
|
+
database: "db",
|
92
|
+
username: "u",
|
93
|
+
password: "p",
|
94
|
+
host: "db.example.org",
|
95
|
+
port: 1234
|
96
|
+
},
|
97
|
+
memcached: {
|
98
|
+
servers: "mc.example.org:11211",
|
99
|
+
username: "mcuser",
|
100
|
+
password: "mcpass",
|
101
|
+
server_strings: ["mc.example.org:11211"]
|
102
|
+
},
|
103
|
+
redis: {
|
104
|
+
url: "redis://:orpass@or.example.org:2345",
|
105
|
+
host: "or.example.org",
|
106
|
+
port: 2345,
|
107
|
+
user: "",
|
108
|
+
password: "orpass"
|
109
|
+
},
|
110
|
+
smtp: {
|
111
|
+
port: "25",
|
112
|
+
authentication: :plain,
|
113
|
+
address: "pm.example.org",
|
114
|
+
user_name: "pmkey",
|
115
|
+
password: "pmkey"
|
116
|
+
}
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
29
122
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
shared_examples "empty configuration" do
|
2
|
+
|
3
|
+
it "returns nil for keys" do
|
4
|
+
[:url, :test].each do |k|
|
5
|
+
expect(config[k]).to eq(nil)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
it "responds to #to_h with empty hash" do
|
10
|
+
expect(config.to_h).to eq({})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has nil provider name" do
|
14
|
+
expect(config.provider.name).to eq(nil)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
shared_examples "redis configuration" do |env_key|
|
20
|
+
context "with openredis in ENV" do
|
21
|
+
before do
|
22
|
+
env[env_key] = "redis://:secrettoken@127.0.0.1:1234"
|
23
|
+
end
|
24
|
+
|
25
|
+
{
|
26
|
+
url: "redis://:secrettoken@127.0.0.1:1234",
|
27
|
+
host: "127.0.0.1",
|
28
|
+
port: 1234,
|
29
|
+
user: "",
|
30
|
+
password: "secrettoken",
|
31
|
+
}.each do |key, value|
|
32
|
+
it "sets :#{key} to #{value.inspect}" do
|
33
|
+
expect(config[key]).to eq(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/smtp_spec.rb
CHANGED
@@ -6,16 +6,26 @@ describe "SMTP configuration" do
|
|
6
6
|
subject(:config) { Envconfig.load(env).smtp }
|
7
7
|
|
8
8
|
context "with nothing relevant in ENV" do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
it_behaves_like "empty configuration"
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with Mailgun in ENV" do
|
13
|
+
before do
|
14
|
+
env['MAILGUN_SMTP_PORT'] = 2468
|
15
|
+
env['MAILGUN_SMTP_SERVER'] = "mailgun.example.org"
|
16
|
+
env['MAILGUN_SMTP_LOGIN'] = "mailgunuser"
|
17
|
+
env['MAILGUN_SMTP_PASSWORD'] = "a493216c9ced751d6bc38fd6f5c3930b"
|
13
18
|
end
|
14
|
-
it "
|
15
|
-
expect(config.
|
19
|
+
it "identifies as Mailgun" do
|
20
|
+
expect(config.provider.name).to eq("Mailgun")
|
16
21
|
end
|
17
|
-
it "
|
18
|
-
expect(config.
|
22
|
+
it "sets port, address, user_name, password" do
|
23
|
+
expect(config.to_h).to eq(
|
24
|
+
address: "mailgun.example.org",
|
25
|
+
port: 2468,
|
26
|
+
user_name: "mailgunuser",
|
27
|
+
password: "a493216c9ced751d6bc38fd6f5c3930b",
|
28
|
+
)
|
19
29
|
end
|
20
30
|
end
|
21
31
|
|
@@ -27,7 +37,7 @@ describe "SMTP configuration" do
|
|
27
37
|
it "sets address, port, user_name, password" do
|
28
38
|
expect(config.to_h).to eq(
|
29
39
|
address: "smtp.mandrillapp.com",
|
30
|
-
port: "
|
40
|
+
port: "587",
|
31
41
|
user_name: "mandrilluser",
|
32
42
|
password: "mandrillkey",
|
33
43
|
)
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Envconfig
|
4
|
+
describe UrlParser do
|
5
|
+
|
6
|
+
let(:url) { "postgres://john:pass@a.example.org:1234/db?a=b&c=d" }
|
7
|
+
subject(:parser) { UrlParser.new(url) }
|
8
|
+
|
9
|
+
describe "#extract" do
|
10
|
+
it "returns hash of requested keys" do
|
11
|
+
expect(parser.extract(:password, :host, :port)).to eq(
|
12
|
+
password: "pass",
|
13
|
+
host: "a.example.org",
|
14
|
+
port: 1234
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#extract_as" do
|
20
|
+
it "returns hash with renamed keys" do
|
21
|
+
result = parser.extract_as(
|
22
|
+
adapter: ->(u){ u.scheme.sub(/\Apostgres\z/, "postgresql") },
|
23
|
+
username: :user,
|
24
|
+
port: :port,
|
25
|
+
)
|
26
|
+
expect(result).to eq(
|
27
|
+
adapter: "postgresql",
|
28
|
+
username: "john",
|
29
|
+
port: 1234,
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#query_values" do
|
35
|
+
it "returns query string as a hash with symbol keys" do
|
36
|
+
expect(parser.query_values).to eq(
|
37
|
+
a: "b",
|
38
|
+
c: "d"
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: envconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Annesley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,15 +70,25 @@ files:
|
|
70
70
|
- envconfig.gemspec
|
71
71
|
- lib/envconfig-rails.rb
|
72
72
|
- lib/envconfig.rb
|
73
|
+
- lib/envconfig/database.rb
|
74
|
+
- lib/envconfig/memcached.rb
|
73
75
|
- lib/envconfig/provider.rb
|
74
76
|
- lib/envconfig/railtie.rb
|
77
|
+
- lib/envconfig/redis.rb
|
75
78
|
- lib/envconfig/service.rb
|
76
79
|
- lib/envconfig/smtp.rb
|
80
|
+
- lib/envconfig/url_parser.rb
|
77
81
|
- lib/envconfig/version.rb
|
82
|
+
- spec/database_spec.rb
|
83
|
+
- spec/envconfig_spec.rb
|
84
|
+
- spec/memcached_spec.rb
|
78
85
|
- spec/provider_resolver_spec.rb
|
86
|
+
- spec/redis_spec.rb
|
79
87
|
- spec/root_spec.rb
|
88
|
+
- spec/shared_examples.rb
|
80
89
|
- spec/smtp_spec.rb
|
81
90
|
- spec/spec_helper.rb
|
91
|
+
- spec/url_parser_spec.rb
|
82
92
|
homepage: https://github.com/broadstack/envconfig
|
83
93
|
licenses:
|
84
94
|
- MIT
|
@@ -104,8 +114,14 @@ signing_key:
|
|
104
114
|
specification_version: 4
|
105
115
|
summary: Connect your app to backing services via Broadstack/Heroku-style ENV vars.
|
106
116
|
test_files:
|
117
|
+
- spec/database_spec.rb
|
118
|
+
- spec/envconfig_spec.rb
|
119
|
+
- spec/memcached_spec.rb
|
107
120
|
- spec/provider_resolver_spec.rb
|
121
|
+
- spec/redis_spec.rb
|
108
122
|
- spec/root_spec.rb
|
123
|
+
- spec/shared_examples.rb
|
109
124
|
- spec/smtp_spec.rb
|
110
125
|
- spec/spec_helper.rb
|
126
|
+
- spec/url_parser_spec.rb
|
111
127
|
has_rdoc:
|