prius 0.2.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4efd54f9144bd36ec263a5cb4207ee0adc8b80c
4
- data.tar.gz: 4a1cfedd0b63a10146b9164ca3cbc78f276afbfd
3
+ metadata.gz: 2f204cfe4500248f7ae5ec942f91aa3e367b5507
4
+ data.tar.gz: 375c5ede8265db74665bdfa8d5a512e6c2be84d0
5
5
  SHA512:
6
- metadata.gz: f5f916b7b56ae454bed8bdcc82f37e7bad980a6003af01aa669776d725131e0fe74e479d8ad06be9be34e760d1cc4047debb538e94e4c8e471c64d37144687e5
7
- data.tar.gz: 6d6788a1500a1efc21c7620fc09e902c3f4606feba3ce6211feb493eec44211248a5ba8da7f94dec700572e16a1e306d524b1370fde375f34114c5f43351bdff
6
+ metadata.gz: 1c8edc7fcae0e3b43d8bb2385228a8c4667511407724d8348b52e93e5a35bbee2f60e9626291b705a21f39fb21f802be17e750d9ce6e9e998817e9fc24e8eb2d
7
+ data.tar.gz: 8514bdb66909f5b16f7dd9173e0478503453fecd6bb0fcf41bab11b37bdc62de96b92107e08d6309a988481d7fa0521f3b4e3402c0957c4f023f8b1fa6e8b804
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  Gemfile.lock
2
+ .bundle
3
+ *.gem
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ Exclude:
3
+ - prius.gemspec
4
+
5
+ # Limit lines to 80 characters.
6
+ LineLength:
7
+ Max: 80
8
+
9
+ ClassLength:
10
+ Max: 300
11
+
12
+ StringLiterals:
13
+ Enabled: false
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Style/SignalException:
19
+ EnforcedStyle: 'only_raise'
20
+
21
+ Style/DotPosition:
22
+ EnforcedStyle: 'trailing'
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ matrix:
4
+ allow_failures:
5
+ - rvm: jruby
6
+ - rvm: rbx-2
7
+
8
+ rvm:
9
+ - 1.9.3
10
+ - 2.0
11
+ - 2.1
12
+ - 2.2
13
+ - jruby
14
+ - rbx-2
15
+
16
+ script:
17
+ - bundle exec rubocop
18
+ - bundle exec rspec spec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 GoCardless
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,80 @@
1
1
  # Prius
2
-
3
2
  Environmentally-friendly application config for Ruby.
4
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/prius.svg)](http://badge.fury.io/rb/prius)
5
+ [![Build Status](https://travis-ci.org/gocardless/prius.svg?branch=master)](https://travis-ci.org/gocardless/prius)
6
+
7
+ Prius helps you guarantee that your environment variables are:
8
+
9
+ - **Present** - an exception is raised if an environment variable is missing,
10
+ so you can hear about it as soon as your app boots.
11
+ - **Valid** - an environment variable can be coerced to a desired type
12
+ (integer, boolean or string), and an exception will be raised if the value
13
+ doesn't match the type.
14
+
15
+ ## Usage
16
+
17
+ #### Installing
18
+
19
+ ```
20
+ $ gem install prius
21
+ ```
22
+
23
+ #### Quick Start
24
+
25
+ ```ruby
26
+ # Load a required environment variable into the Prius registry:
27
+ Prius.load(:github_token)
28
+
29
+ # Use the environment variable:
30
+ Prius.get(:github_token)
31
+
32
+ # Load an optional environment variable:
33
+ Prius.load(:might_be_here_or_not, required: false)
34
+
35
+ # Load and alias an environment variable:
36
+ Prius.load(:alias_name, env_var: "HORRENDOUS_SYSTEM_VAR_NAME")
37
+
38
+ # Load and coerce an environment variable (or raise):
39
+ Prius.load(:my_flag, type: :bool)
40
+ ```
41
+
42
+ You probably want to `load` all your environment variables as your app starts,
43
+ so you catch config issues at boot time.
44
+
45
+ #### Loading Environment Variables
46
+
47
+ Environment variables need to be loaded into the Prius registry before being
48
+ used. Typically this is done in an initialiser.
49
+
50
+ ```ruby
51
+ Prius.load(name, options = {})
52
+ ```
53
+
54
+ If an environment variable can't be loaded, Prius will raise one of:
55
+ - `MissingValueError` if the environment variable was expected to be set but couldn't be found.
56
+ - `TypeMismatchError` if the environment variable wasn't of the expected type (see below).
57
+
58
+ `Prius.load` accepts the following options:
59
+
60
+ | Param | Default | Description |
61
+ |-------------------|---------------|-------------------------------------------------------------------------------------------|
62
+ | `required` | `true` | Flag to require the environment variable to have been set. |
63
+ | `type` | `:string` | Type to coerce the environment variable to. Allowed values are `:string`, `:int` and `:bool`. |
64
+ | `env_var` | `name.upcase` | Name of the environment variable name (if different from the upcased `name`). |
65
+
66
+ #### Reading Environment Variables
67
+
68
+ Once a variable has been loaded into the registry it can be read using:
69
+
70
+ ```ruby
71
+ Prius.get(name)
72
+ ```
73
+
74
+ If the environment variable hasn't been loaded, Prius will raise an `UndeclaredNameError`.
75
+
76
+ #### Test and development environments
77
+
78
+ To make running your app in test and development environments easier we
79
+ recommend using [Dotenv](https://github.com/bkeepers/dotenv) to automatically
80
+ load a file of dummy config values.
@@ -2,15 +2,45 @@ require "prius/registry"
2
2
  require "prius/railtie" if defined?(Rails)
3
3
 
4
4
  module Prius
5
- def self.load(name, type: :string, allow_nil: false)
6
- registry.load(name, type: type, allow_nil: allow_nil)
5
+ # Load an environment variable into the registry.
6
+ #
7
+ # name - The Symbol name of the item in the registry. Use this when
8
+ # looking up the item via `#get`. If `env_var` is not provided
9
+ # this will be uppercased and used as the environment variable name.
10
+ # options - An optional Hash of options (default {}):
11
+ # :env_var - The String name of the environment variable. If
12
+ # omitted the uppercased form of `name` will be used.
13
+ # :type - The Symbol type of the environment variable's value.
14
+ # The value will be coerced to this type. Must be one
15
+ # of :string, :int, or :bool (default :string).
16
+ # :required - A Boolean indicating whether the value must be
17
+ # present in the environment. If true, a
18
+ # MissingValueError exception will be raised if the
19
+ # value isn't present in the environment. Otherwise,
20
+ # the value will be set to `nil` if the environment
21
+ # variable isn't present (default true).
22
+ #
23
+ # Raises a MissingValueError for required values that are missing.
24
+ # Raises a TypeMismatchError if a value can't be coerced to the given `type`.
25
+ # Raises an ArgumentError if an invalid `type` is provided.
26
+ def self.load(name, options = {})
27
+ registry.load(name, options)
7
28
  end
8
29
 
30
+ # Fetch a value from the registry.
31
+ #
32
+ # name - The Symbol name of the variable to look up. The name must have
33
+ # previously been `#load`ed into the registry.
34
+ #
35
+ # Raises UndeclaredNameError if the name provided has not been loaded into
36
+ # the registry.
9
37
  def self.get(name)
10
38
  registry.get(name)
11
39
  end
12
40
 
41
+ # Internal: accessor for the shared registry.
13
42
  def self.registry
14
43
  @registry ||= Registry.new(ENV)
15
44
  end
45
+ private_class_method :registry
16
46
  end
@@ -2,23 +2,29 @@ require "prius/errors"
2
2
 
3
3
  module Prius
4
4
  class Registry
5
- TYPES = [:string, :int, :bool]
6
-
5
+ # Initialise a Registry.
6
+ #
7
+ # env - A Hash used as a source for environment variables. Usually, ENV
8
+ # will be used.
7
9
  def initialize(env)
8
10
  @env = env
9
11
  @registry = {}
10
12
  end
11
13
 
12
- def load(name, env_var: nil, type: :string, allow_nil: false)
13
- env_var ||= name.to_s.upcase
14
+ # See Prius.load for documentation.
15
+ def load(name, options = {})
16
+ env_var = options.fetch(:env_var, name.to_s.upcase)
17
+ type = options.fetch(:type, :string)
18
+ required = options.fetch(:required, true)
14
19
  @registry[name] = case type
15
- when :string then load_string(env_var, allow_nil)
16
- when :int then load_int(env_var, allow_nil)
17
- when :bool then load_bool(env_var, allow_nil)
18
- else raise ArgumentError
20
+ when :string then load_string(env_var, required)
21
+ when :int then load_int(env_var, required)
22
+ when :bool then load_bool(env_var, required)
23
+ else raise ArgumentError, "Invalid type #{type}"
19
24
  end
20
25
  end
21
26
 
27
+ # See Prius.get for documentation.
22
28
  def get(name)
23
29
  @registry.fetch(name)
24
30
  rescue KeyError
@@ -27,21 +33,15 @@ module Prius
27
33
 
28
34
  private
29
35
 
30
- def check_valid_type(type)
31
- unless TYPES.include?(type)
32
- raise ArgumentError, "invalid type '#{type}'"
33
- end
34
- end
35
-
36
- def load_string(name, allow_nil)
36
+ def load_string(name, required)
37
37
  @env.fetch(name)
38
38
  rescue KeyError
39
- return nil if allow_nil
39
+ return nil unless required
40
40
  raise MissingValueError, "config value '#{name}' not present"
41
41
  end
42
42
 
43
- def load_int(name, allow_nil)
44
- value = load_string(name, allow_nil)
43
+ def load_int(name, required)
44
+ value = load_string(name, required)
45
45
  return value if value.nil?
46
46
 
47
47
  unless /\A[0-9]+\z/.match(value)
@@ -50,8 +50,8 @@ module Prius
50
50
  value.to_i
51
51
  end
52
52
 
53
- def load_bool(name, allow_nil)
54
- value = load_string(name, allow_nil)
53
+ def load_bool(name, required)
54
+ value = load_string(name, required)
55
55
  return nil if value.nil?
56
56
 
57
57
  if /\A(yes|y|true|t|1)\z/i.match(value)
@@ -1,3 +1,3 @@
1
1
  module Prius
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
@@ -19,4 +19,5 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "rspec", "~> 3.1"
22
+ spec.add_development_dependency "rubocop", "~> 0.31.0"
22
23
  end
@@ -17,9 +17,9 @@ describe Prius::Registry do
17
17
  to raise_error(Prius::MissingValueError)
18
18
  end
19
19
 
20
- context "when allow_nil is true" do
20
+ context "when required is false" do
21
21
  it "doesn't blow up" do
22
- expect { registry.load(:slogan, allow_nil: true) }.to_not raise_error
22
+ expect { registry.load(:slogan, required: false) }.to_not raise_error
23
23
  end
24
24
  end
25
25
  end
@@ -88,7 +88,7 @@ describe Prius::Registry do
88
88
  end
89
89
 
90
90
  context "given a nillable name that has been loaded" do
91
- before { registry.load(:lightsabre, allow_nil: true) }
91
+ before { registry.load(:lightsabre, required: false) }
92
92
  it "returns nil" do
93
93
  expect(registry.get(:lightsabre)).to be_nil
94
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prius
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Marr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.31.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.31.0
27
41
  description: Environmentally-friendly config
28
42
  email:
29
43
  - engineering@gocardless.com
@@ -32,7 +46,10 @@ extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
48
  - .gitignore
49
+ - .rubocop.yml
50
+ - .travis.yml
35
51
  - Gemfile
52
+ - LICENSE
36
53
  - README.md
37
54
  - lib/prius.rb
38
55
  - lib/prius/errors.rb
@@ -67,3 +84,4 @@ specification_version: 4
67
84
  summary: Environmentally-friendly config
68
85
  test_files:
69
86
  - spec/prius/registry_spec.rb
87
+ has_rdoc: