service_config 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/lib/service_config/provider.rb +31 -0
- data/lib/service_config/version.rb +3 -0
- data/lib/service_config.rb +5 -0
- data/service_config.gemspec +22 -0
- data/spec/provider_spec.rb +47 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mag+ AB
|
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,35 @@
|
|
1
|
+
# ServiceConfig
|
2
|
+
|
3
|
+
Use this gem to expose environment variables to your code, with clear
|
4
|
+
defaults and error handling for when the environment variables are
|
5
|
+
unset.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'service_config'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
ENV['INTERNAL_SERVER'] = 'http://localhost:3000/'
|
20
|
+
|
21
|
+
provider = ServiceConfig::Provider.new(:raise_if_nil => false, :use_env => true) do |config|
|
22
|
+
config.provides :internal_server
|
23
|
+
config.provides :soundcloud_server, 'http://api.soundcloud.com/'
|
24
|
+
end
|
25
|
+
|
26
|
+
provider.internal_server # => 'http://localhost:3000/'
|
27
|
+
provider.soundcloud_server # => 'http://api.soundcloud.com/'
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ServiceConfig
|
2
|
+
class Provider
|
3
|
+
def initialize(args)
|
4
|
+
@raise_if_nil = args[:raise_if_nil]
|
5
|
+
@use_env = args[:use_env]
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
def provides(name, default_value = '')
|
10
|
+
guard_against_unset_env(name) if @raise_if_nil
|
11
|
+
|
12
|
+
self.class.send(:define_method, name) do
|
13
|
+
lookup_value(name) || default_value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def guard_against_unset_env(name)
|
20
|
+
env_variable_name = name.to_s.upcase
|
21
|
+
|
22
|
+
unless ENV[env_variable_name]
|
23
|
+
raise "must set #{env_variable_name}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def lookup_value(name)
|
28
|
+
ENV[name.to_s.upcase] if @use_env
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'service_config/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "service_config"
|
8
|
+
gem.version = ServiceConfig::VERSION
|
9
|
+
gem.authors = ["Karl Eklund", "Mikael Amborn", "Mike Burns"]
|
10
|
+
gem.email = ["info@magplus.com"]
|
11
|
+
gem.description = %q{Configure your values using the environment, with fallbacks}
|
12
|
+
gem.summary = %q{Configure your values using the environment, with fallbacks}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'service_config/provider'
|
2
|
+
|
3
|
+
describe ServiceConfig::Provider do
|
4
|
+
it 'provides the services as configured' do
|
5
|
+
ENV['FOO'] = 'yo'
|
6
|
+
service_config = build_service_config(:use_env => true) do |c|
|
7
|
+
c.provides :foo
|
8
|
+
end
|
9
|
+
|
10
|
+
service_config.should respond_to(:foo)
|
11
|
+
service_config.method(:foo).should_not be_nil
|
12
|
+
service_config.foo.should == 'yo'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'raises if an environment variable is not set, if configured' do
|
16
|
+
expect {
|
17
|
+
build_service_config(:raise_if_nil => true) { |c| c.provides :unset }
|
18
|
+
}.to raise_error('must set UNSET')
|
19
|
+
|
20
|
+
expect {
|
21
|
+
build_service_config(:raise_if_nil => false) { |c| c.provides :unset }
|
22
|
+
}.not_to raise_error
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'defines the value as "" if the environment variable is unset' do
|
26
|
+
service_config = build_service_config(:raise_if_nil => false, :use_env => true) { |c| c.provides :unknown }
|
27
|
+
service_config.unknown.should == ''
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'provides the optional value if given and environment variable is unset' do
|
31
|
+
service_config = build_service_config(:raise_if_nil => false, :use_env => true) { |c| c.provides :not_set, 'optional value' }
|
32
|
+
service_config.not_set.should == 'optional value'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'does not look at the environment variable in the test environment' do
|
36
|
+
ENV['NOT_SET'] = 'value'
|
37
|
+
service_config = build_service_config(:use_env => false) { |c| c.provides :not_set }
|
38
|
+
service_config.not_set.should == ''
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_service_config(opts = {}, &block)
|
42
|
+
default_opts = { :raise_if_nil => true, :use_env => true }
|
43
|
+
ServiceConfig::Provider.new(default_opts.merge(opts)) do |config|
|
44
|
+
block.call(config)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: service_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Karl Eklund
|
9
|
+
- Mikael Amborn
|
10
|
+
- Mike Burns
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
requirement: &70118902353820 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *70118902353820
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: &70118902353320 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *70118902353320
|
38
|
+
description: Configure your values using the environment, with fallbacks
|
39
|
+
email:
|
40
|
+
- info@magplus.com
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/service_config.rb
|
51
|
+
- lib/service_config/provider.rb
|
52
|
+
- lib/service_config/version.rb
|
53
|
+
- service_config.gemspec
|
54
|
+
- spec/provider_spec.rb
|
55
|
+
homepage: ''
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
hash: 1473975662623168250
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: 1473975662623168250
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.10
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Configure your values using the environment, with fallbacks
|
85
|
+
test_files:
|
86
|
+
- spec/provider_spec.rb
|