envconfig 0.0.3 → 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 +4 -4
- data/README.md +77 -14
- data/lib/envconfig.rb +10 -0
- data/lib/envconfig/mongodb.rb +44 -0
- data/lib/envconfig/version.rb +1 -1
- data/spec/mongodb_spec.rb +19 -0
- data/spec/root_spec.rb +4 -0
- data/spec/shared_examples.rb +24 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ab09a9d9d16863e549d99b9e6d4e98185e66ed2
|
4
|
+
data.tar.gz: b679e231ef2f5c3287ab1673210bd982d921145b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98ddb30963a62b3f0b0a6f45e400843df574675b6dedaf228e3fa710efb648182f34b28672eef8246bd5f9d172bae526efb31df220c33053aa0888ef6ece6ac5
|
7
|
+
data.tar.gz: a2fca7e29c93c0302c62aa4d2d98e25a448bedce4088f72357a722c930fa918972791913a3377b5b1f5ef2f526a5e43fc9fab6fd5439990e230f81ab0142d0d5
|
data/README.md
CHANGED
@@ -1,33 +1,65 @@
|
|
1
1
|
# envconfig
|
2
2
|
|
3
|
-
|
4
|
-
provided by [Broadstack][broadstack] or
|
3
|
+
Easily configure your Ruby / Rails app to use the backing services specified in
|
4
|
+
environment variables such as those provided by [Broadstack][broadstack] or
|
5
|
+
[Heroku][heroku_addons] add-ons.
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
via initializers.
|
7
|
+
This lets you easily switch between providers, or between development and production,
|
8
|
+
without having configuration conditionals in your code.
|
9
9
|
|
10
|
-
For
|
10
|
+
For more on why this makes a lot of sense, read the [Config][12config] and
|
11
|
+
[Backing Services][12bs] sections of the excellent [Twelve-Factor App][12factor]
|
12
|
+
written by Heroku co-founder Adam Wiggins.
|
11
13
|
|
12
|
-
|
14
|
+
|
15
|
+
## Services and Providers
|
16
|
+
|
17
|
+
* [Database](#database): generic `DATABASE_URL`.
|
18
|
+
* [memcached](#memcached): MemCachier.
|
19
|
+
* [MongoDB](#mongodb): MongoHQ, MongoLab.
|
20
|
+
* [Redis](#redis): openredis, RedisCloud, RedisGreen, Redis To Go.
|
21
|
+
* [SMTP](#smtp): generic, Mailgun, Mandrill, Postmark, Sendgrid.
|
22
|
+
|
23
|
+
|
24
|
+
## Examples
|
25
|
+
|
26
|
+
Given the following ENV vars:
|
27
|
+
|
28
|
+
```sh
|
13
29
|
POSTMARK_SMTP_SERVER="smtp.example.org"
|
14
30
|
POSTMARK_API_KEY="bcca0a78abbaed6533f3c8017b804bda"
|
31
|
+
MONGOHQ_URL="mongodb://user:pass@mongo.example.com:2468/stack618"
|
15
32
|
```
|
16
33
|
|
17
|
-
|
34
|
+
Envconfig will look like this:
|
18
35
|
|
19
36
|
```ruby
|
20
|
-
Envconfig.load
|
21
|
-
|
37
|
+
config = Envconfig.load
|
38
|
+
|
39
|
+
# Individual keys:
|
40
|
+
config.smtp[:address] # => "smtp.example.org"
|
41
|
+
config.mongodb[:host] # => "mongo.example.com"
|
42
|
+
|
43
|
+
# Service configuration:
|
44
|
+
config.smtp.to_h # => {
|
22
45
|
port: "25",
|
23
46
|
authentication: :plain,
|
24
47
|
address: "smtp.example.org",
|
25
48
|
user_name: "bcca0a78abbaed6533f3c8017b804bda",
|
26
49
|
password: "bcca0a78abbaed6533f3c8017b804bda"
|
27
50
|
}
|
51
|
+
config.mongodb.to_h # => {
|
52
|
+
url: "mongodb://user:pass@mongo.example.com:2468/stack618",
|
53
|
+
database: "stack618",
|
54
|
+
username: "user",
|
55
|
+
password: "pass",
|
56
|
+
host: "mongo.example.com",
|
57
|
+
port: 2468
|
58
|
+
}
|
28
59
|
```
|
29
60
|
|
30
|
-
And
|
61
|
+
And if you're using Rails, ActionMailer will be automatically configured
|
62
|
+
to use those SMTP details via `Rails.application.config.action_mailer.smtp_settings`.
|
31
63
|
|
32
64
|
|
33
65
|
## Installation
|
@@ -39,16 +71,22 @@ or `gem install envconfig`.
|
|
39
71
|
|
40
72
|
## Usage
|
41
73
|
|
42
|
-
|
74
|
+
### envconfig
|
43
75
|
|
44
|
-
|
45
|
-
|
76
|
+
The `envconfig` gem exposes your `ENV` configuration as a consistent interface,
|
77
|
+
using its knowledge of many add-on providers.
|
46
78
|
|
47
79
|
```ruby
|
48
80
|
Envconfig.load.smtp[:address] => "example.org"
|
49
81
|
Envconfig.load.smtp.to_h # => {address: "example.org", ...}
|
50
82
|
```
|
51
83
|
|
84
|
+
### envconfig-rails
|
85
|
+
|
86
|
+
The `envconfig-rails` gem adds Rails integration, pushing certain configuration
|
87
|
+
into the Rails application where it makes sense. Currently this is limited to
|
88
|
+
configuring ActionMailer for SMTP; feel free to open issues discussing others.
|
89
|
+
|
52
90
|
|
53
91
|
## Supported Add-ons
|
54
92
|
|
@@ -121,6 +159,28 @@ Envconfig.load.memcached.to_h # =>
|
|
121
159
|
```
|
122
160
|
|
123
161
|
|
162
|
+
### MongoDB
|
163
|
+
|
164
|
+
* MongoHQ ([Broadstack](https://broadstack.com/addons/mongohq), [Heroku](https://addons.heroku.com/mongohq))
|
165
|
+
* MongoLab ([Heroku](https://addons.heroku.com/mongolab))
|
166
|
+
|
167
|
+
MongoDB example:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
ENV["MONGOHQ_URL"] = "mongodb://user:pass@mhq.example.org:2468/stack618"
|
171
|
+
|
172
|
+
Envconfig.load.mongodb.to_h # =>
|
173
|
+
{
|
174
|
+
url: "mongodb://user:secret@mhq.example.org:2468/stack618",
|
175
|
+
username: "user",
|
176
|
+
password: "secret",
|
177
|
+
host: "mhq.example.org",
|
178
|
+
port: 2468,
|
179
|
+
database: "stack618"
|
180
|
+
}
|
181
|
+
```
|
182
|
+
|
183
|
+
|
124
184
|
### Redis
|
125
185
|
|
126
186
|
* openredis ([Heroku](https://addons.heroku.com/openredis))
|
@@ -156,3 +216,6 @@ Envconfig.load.redis.to_h # =>
|
|
156
216
|
[backing_services]: http://12factor.net/backing-services
|
157
217
|
[broadstack]: https://broadstack.com
|
158
218
|
[heroku_addons]: https://addons.heroku.com/
|
219
|
+
[12factor]: http://12factor.net/
|
220
|
+
[12config]: http://12factor.net/config
|
221
|
+
[12bs]: http://12factor.net/backing-services
|
data/lib/envconfig.rb
CHANGED
@@ -4,6 +4,7 @@ require "envconfig/url_parser"
|
|
4
4
|
|
5
5
|
require "envconfig/database"
|
6
6
|
require "envconfig/memcached"
|
7
|
+
require "envconfig/mongodb"
|
7
8
|
require "envconfig/redis"
|
8
9
|
require "envconfig/smtp"
|
9
10
|
|
@@ -26,6 +27,7 @@ module Envconfig
|
|
26
27
|
[
|
27
28
|
:database,
|
28
29
|
:memcached,
|
30
|
+
:mongodb,
|
29
31
|
:redis,
|
30
32
|
:smtp,
|
31
33
|
].inject({}) do |hash, service|
|
@@ -49,6 +51,14 @@ module Envconfig
|
|
49
51
|
predicate_for(:memcached)
|
50
52
|
end
|
51
53
|
|
54
|
+
def mongodb
|
55
|
+
service_for(:mongodb, Mongodb)
|
56
|
+
end
|
57
|
+
|
58
|
+
def mongodb?
|
59
|
+
predicate_for(:mongodb)
|
60
|
+
end
|
61
|
+
|
52
62
|
def redis
|
53
63
|
service_for(:redis, Redis)
|
54
64
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Envconfig
|
2
|
+
class Mongodb
|
3
|
+
|
4
|
+
include Service
|
5
|
+
|
6
|
+
def self.providers
|
7
|
+
[
|
8
|
+
Mongohq,
|
9
|
+
Mongolab,
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
module ConfigFilter
|
14
|
+
def filter_config(config)
|
15
|
+
url = UrlParser.new(config[:url])
|
16
|
+
parts = url.extract_as(
|
17
|
+
database: ->(u){ (u.path || "").split("/")[1] },
|
18
|
+
username: :user,
|
19
|
+
password: :password,
|
20
|
+
host: :host,
|
21
|
+
port: :port,
|
22
|
+
)
|
23
|
+
config.merge!(parts).merge!(url.query_values)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Mongohq
|
28
|
+
include Provider
|
29
|
+
include ConfigFilter
|
30
|
+
def mapping
|
31
|
+
{url: "MONGOHQ_URL"}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Mongolab
|
36
|
+
include Provider
|
37
|
+
include ConfigFilter
|
38
|
+
def mapping
|
39
|
+
{url: "MONGOLAB_URI"}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/lib/envconfig/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "MongoDB configuration" do
|
4
|
+
|
5
|
+
let(:env) { {} }
|
6
|
+
let(:config) { Envconfig.load(env).mongodb }
|
7
|
+
|
8
|
+
context "with nothing relevant in ENV" do
|
9
|
+
it_behaves_like "empty configuration"
|
10
|
+
end
|
11
|
+
|
12
|
+
%w{
|
13
|
+
MONGOHQ_URL
|
14
|
+
MONGOLAB_URI
|
15
|
+
}.each do |env_key|
|
16
|
+
it_behaves_like "mongodb configuration", env_key
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/root_spec.rb
CHANGED
@@ -10,6 +10,7 @@ describe Envconfig::Root do
|
|
10
10
|
service_methods = [
|
11
11
|
:database,
|
12
12
|
:memcached,
|
13
|
+
:mongodb,
|
13
14
|
:redis,
|
14
15
|
:smtp,
|
15
16
|
]
|
@@ -43,6 +44,7 @@ describe Envconfig::Root do
|
|
43
44
|
expect(root.to_h).to eq(
|
44
45
|
database: {},
|
45
46
|
memcached: {},
|
47
|
+
mongodb: {},
|
46
48
|
redis: {},
|
47
49
|
smtp: {},
|
48
50
|
)
|
@@ -100,6 +102,8 @@ describe Envconfig::Root do
|
|
100
102
|
password: "mcpass",
|
101
103
|
server_strings: ["mc.example.org:11211"]
|
102
104
|
},
|
105
|
+
mongodb: {
|
106
|
+
},
|
103
107
|
redis: {
|
104
108
|
url: "redis://:orpass@or.example.org:2345",
|
105
109
|
host: "or.example.org",
|
data/spec/shared_examples.rb
CHANGED
@@ -17,7 +17,7 @@ shared_examples "empty configuration" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
shared_examples "redis configuration" do |env_key|
|
20
|
-
context "with
|
20
|
+
context "with #{env_key} in ENV" do
|
21
21
|
before do
|
22
22
|
env[env_key] = "redis://:secrettoken@127.0.0.1:1234"
|
23
23
|
end
|
@@ -35,3 +35,26 @@ shared_examples "redis configuration" do |env_key|
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
shared_examples "mongodb configuration" do |env_key|
|
40
|
+
context "with #{env_key} in ENV" do
|
41
|
+
before do
|
42
|
+
env[env_key] = "mongodb://user:pass@example.org:2468/db?a=b&c=d"
|
43
|
+
end
|
44
|
+
|
45
|
+
{
|
46
|
+
url: "mongodb://user:pass@example.org:2468/db?a=b&c=d",
|
47
|
+
database: "db",
|
48
|
+
username: "user",
|
49
|
+
password: "pass",
|
50
|
+
host: "example.org",
|
51
|
+
port: 2468,
|
52
|
+
a: "b",
|
53
|
+
c: "d",
|
54
|
+
}.each do |key, value|
|
55
|
+
it "sets :#{key} to #{value.inspect}" do
|
56
|
+
expect(config[key]).to eq(value)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
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: 1.0.0
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/envconfig.rb
|
73
73
|
- lib/envconfig/database.rb
|
74
74
|
- lib/envconfig/memcached.rb
|
75
|
+
- lib/envconfig/mongodb.rb
|
75
76
|
- lib/envconfig/provider.rb
|
76
77
|
- lib/envconfig/railtie.rb
|
77
78
|
- lib/envconfig/redis.rb
|
@@ -82,6 +83,7 @@ files:
|
|
82
83
|
- spec/database_spec.rb
|
83
84
|
- spec/envconfig_spec.rb
|
84
85
|
- spec/memcached_spec.rb
|
86
|
+
- spec/mongodb_spec.rb
|
85
87
|
- spec/provider_resolver_spec.rb
|
86
88
|
- spec/redis_spec.rb
|
87
89
|
- spec/root_spec.rb
|
@@ -109,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
111
|
version: '0'
|
110
112
|
requirements: []
|
111
113
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
114
|
+
rubygems_version: 2.0.3
|
113
115
|
signing_key:
|
114
116
|
specification_version: 4
|
115
117
|
summary: Connect your app to backing services via Broadstack/Heroku-style ENV vars.
|
@@ -117,6 +119,7 @@ test_files:
|
|
117
119
|
- spec/database_spec.rb
|
118
120
|
- spec/envconfig_spec.rb
|
119
121
|
- spec/memcached_spec.rb
|
122
|
+
- spec/mongodb_spec.rb
|
120
123
|
- spec/provider_resolver_spec.rb
|
121
124
|
- spec/redis_spec.rb
|
122
125
|
- spec/root_spec.rb
|
@@ -124,4 +127,3 @@ test_files:
|
|
124
127
|
- spec/smtp_spec.rb
|
125
128
|
- spec/spec_helper.rb
|
126
129
|
- spec/url_parser_spec.rb
|
127
|
-
has_rdoc:
|