prius 0.2.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b4efd54f9144bd36ec263a5cb4207ee0adc8b80c
4
- data.tar.gz: 4a1cfedd0b63a10146b9164ca3cbc78f276afbfd
2
+ SHA256:
3
+ metadata.gz: 82b6cf00671c57a486e613e87ccdbe34978560a9bb19fd0e285f534eb8ee0005
4
+ data.tar.gz: 77a611c64210e71ccc9b8ec60bd0d71ce74e14bf7b53ec3bb76f2977422476b0
5
5
  SHA512:
6
- metadata.gz: f5f916b7b56ae454bed8bdcc82f37e7bad980a6003af01aa669776d725131e0fe74e479d8ad06be9be34e760d1cc4047debb538e94e4c8e471c64d37144687e5
7
- data.tar.gz: 6d6788a1500a1efc21c7620fc09e902c3f4606feba3ce6211feb493eec44211248a5ba8da7f94dec700572e16a1e306d524b1370fde375f34114c5f43351bdff
6
+ metadata.gz: 45db2c76a6298dca7c750dfb4773f56400ccd119e4bdb1548da6f115d2c74cc13c309121e056a221b09e302fa575a0351144df0c85b7b0f2f1d345472f91d316
7
+ data.tar.gz: 86d7ec82467bdda55ad580e966ca814ff77c33481d43180fcfce229a45abf08bd5a775b6f4d88ae3cd43b635a3420273e3467b584468725ce563510823a9e3db
@@ -0,0 +1,65 @@
1
+ version: 2
2
+
3
+ references:
4
+ steps: &steps
5
+ - checkout
6
+
7
+ # CircleCI's Ruby images have Git installed, but the JRuby images lack it, so let's
8
+ # make sure it's installed
9
+ - run: apt-get update && apt-get install -y git
10
+
11
+ - type: cache-restore
12
+ key: prius-bundler-{{ checksum "prius.gemspec" }}
13
+
14
+ - run: gem install bundler -v 2.0.1
15
+
16
+ - run: bundle install --path vendor/bundle
17
+
18
+ - type: cache-save
19
+ key: prius-bundler-{{ checksum "prius.gemspec" }}
20
+ paths:
21
+ - vendor/bundle
22
+
23
+ - type: shell
24
+ command: |
25
+ bundle exec rspec --profile 10 \
26
+ --format RspecJunitFormatter \
27
+ --out /tmp/test-results/rspec.xml \
28
+ --format progress \
29
+ spec
30
+
31
+ - type: store_test_results
32
+ path: /tmp/test-results
33
+
34
+ - run: bundle exec rubocop
35
+ jobs:
36
+ build-ruby24:
37
+ docker:
38
+ - image: ruby:2.4
39
+ steps: *steps
40
+ build-ruby25:
41
+ docker:
42
+ - image: ruby:2.5
43
+ steps: *steps
44
+ build-ruby26:
45
+ docker:
46
+ - image: ruby:2.6
47
+ steps: *steps
48
+ build-ruby27:
49
+ docker:
50
+ - image: ruby:2.7
51
+ steps: *steps
52
+ build-jruby91:
53
+ docker:
54
+ - image: jruby:9.1
55
+ steps: *steps
56
+
57
+ workflows:
58
+ version: 2
59
+ tests:
60
+ jobs:
61
+ - build-ruby24
62
+ - build-ruby25
63
+ - build-ruby26
64
+ - build-ruby27
65
+ - build-jruby91
@@ -0,0 +1,7 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: weekly
7
+ open-pull-requests-limit: 10
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  Gemfile.lock
2
+ .bundle
3
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ AllCops:
2
+ Exclude:
3
+ - prius.gemspec
4
+ - vendor/**/*
5
+ TargetRubyVersion: 2.2
6
+
7
+ # Limit lines to 80 characters.
8
+ LineLength:
9
+ Max: 80
10
+
11
+ ClassLength:
12
+ Max: 300
13
+
14
+ StringLiterals:
15
+ Enabled: false
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Style/SignalException:
21
+ EnforcedStyle: 'only_raise'
22
+
23
+ Layout/DotPosition:
24
+ EnforcedStyle: 'trailing'
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - "spec/**/*_spec.rb"
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # 3.0.0
2
+
3
+ * Enforce Ruby 2.2 as minimum required version
4
+ * Use CircleCI instead of Travis for CI builds
5
+ * Add Dependabot support
6
+ * Some development dependency updates
7
+
8
+ # 2.0.0
9
+
10
+ * Drop support for Ruby versions 1.9, 2.0, 2.1, and add support for 2.3 and 2.4
11
+
12
+ # 1.0.0
13
+
14
+ Initial release
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,84 @@
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.
81
+
82
+ ---
83
+
84
+ GoCardless ♥ open source. If you do too, come [join us](https://gocardless.com/about/jobs/software-engineer).
@@ -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,39 +33,32 @@ 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
41
  raise MissingValueError, "config value '#{name}' not present"
41
42
  end
42
43
 
43
- def load_int(name, allow_nil)
44
- value = load_string(name, allow_nil)
44
+ def load_int(name, required)
45
+ value = load_string(name, required)
45
46
  return value if value.nil?
46
47
 
47
- unless /\A[0-9]+\z/.match(value)
48
+ unless value =~ /\A[0-9]+\z/
48
49
  raise TypeMismatchError, "'#{name}' value '#{value}' is not an integer"
49
50
  end
51
+
50
52
  value.to_i
51
53
  end
52
54
 
53
- def load_bool(name, allow_nil)
54
- value = load_string(name, allow_nil)
55
+ def load_bool(name, required)
56
+ value = load_string(name, required)
55
57
  return nil if value.nil?
58
+ return true if %w[yes y true t 1].include?(value)
59
+ return false if %w[no n false f 0].include?(value)
56
60
 
57
- if /\A(yes|y|true|t|1)\z/i.match(value)
58
- return true
59
- elsif /\A(no|n|false|f|0)\z/i.match(value)
60
- return false
61
- end
62
- raise TypeMismatchError, "'#{name}' value '#{value}' is not an boolean"
61
+ raise TypeMismatchError, "'#{name}' value '#{value}' is not a boolean"
63
62
  end
64
63
  end
65
64
  end
data/lib/prius/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Prius
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "3.0.0".freeze
3
3
  end
data/lib/prius.rb CHANGED
@@ -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
data/prius.gemspec CHANGED
@@ -18,5 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = ">= 2.2"
22
+
21
23
  spec.add_development_dependency "rspec", "~> 3.1"
24
+ spec.add_development_dependency "rubocop", "~> 0.68.1"
25
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.4.0"
22
26
  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
@@ -39,7 +39,7 @@ describe Prius::Registry do
39
39
 
40
40
  it "stores an int" do
41
41
  registry.load(:age, type: :int)
42
- expect(registry.get(:age)).to be_a(Fixnum)
42
+ expect(registry.get(:age)).to be_a(Integer)
43
43
  end
44
44
  end
45
45
 
@@ -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,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prius
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Marr
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
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.68.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.68.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec_junit_formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.0
27
55
  description: Environmentally-friendly config
28
56
  email:
29
57
  - engineering@gocardless.com
@@ -31,8 +59,13 @@ executables: []
31
59
  extensions: []
32
60
  extra_rdoc_files: []
33
61
  files:
34
- - .gitignore
62
+ - ".circleci/config.yml"
63
+ - ".github/dependabot.yml"
64
+ - ".gitignore"
65
+ - ".rubocop.yml"
66
+ - CHANGELOG.md
35
67
  - Gemfile
68
+ - LICENSE
36
69
  - README.md
37
70
  - lib/prius.rb
38
71
  - lib/prius/errors.rb
@@ -45,24 +78,23 @@ homepage: https://github.com/gocardless/prius
45
78
  licenses:
46
79
  - MIT
47
80
  metadata: {}
48
- post_install_message:
81
+ post_install_message:
49
82
  rdoc_options: []
50
83
  require_paths:
51
84
  - lib
52
85
  required_ruby_version: !ruby/object:Gem::Requirement
53
86
  requirements:
54
- - - '>='
87
+ - - ">="
55
88
  - !ruby/object:Gem::Version
56
- version: '0'
89
+ version: '2.2'
57
90
  required_rubygems_version: !ruby/object:Gem::Requirement
58
91
  requirements:
59
- - - '>='
92
+ - - ">="
60
93
  - !ruby/object:Gem::Version
61
94
  version: '0'
62
95
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 2.4.2
65
- signing_key:
96
+ rubygems_version: 3.2.15
97
+ signing_key:
66
98
  specification_version: 4
67
99
  summary: Environmentally-friendly config
68
100
  test_files: