environment_helpers 0.0.1
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 +7 -0
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/README.md +55 -0
- data/environment_helpers.gemspec +35 -0
- data/lib/environment_helpers/access_helpers.rb +22 -0
- data/lib/environment_helpers/boolean_helpers.rb +33 -0
- data/lib/environment_helpers/numeric_helpers.rb +9 -0
- data/lib/environment_helpers/string_helpers.rb +12 -0
- data/lib/environment_helpers/version.rb +3 -0
- data/lib/environment_helpers.rb +21 -0
- metadata +114 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b923675b4ed251cc3ffe498b480923619622e9cb7a1ebbf2e3bde6bbae036e84
|
|
4
|
+
data.tar.gz: 63a23ba984f9f1e67c2e01651cc6d6d73538fc7e8eda26516b56e748209b2bd8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7113d4d1727c3ffe4a1311c588105f5ebec6a9d877da6341ee4d5e12b998fbbb03bfd0543736dbacbdcb3fe38ebd19c4eb3eae5efe5b4352a86086dfadf47e53
|
|
7
|
+
data.tar.gz: 35228c708df18bce2b56c6779f6047da0c3eaf62d922010136779e9e5c958ae66a5002006f2888ebc25be014448ff6515311811f692a498c95075941fadc298e
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# EnvironmentHelpers
|
|
2
|
+
This gem adds a set of convenience helpers to `ENV`, allowing you to access environment variables
|
|
3
|
+
in a more structures and consistent way. Have you seen any line like these?
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
enable_bar = ENV.fetch("ENABLE_BAR", "false") == "true"
|
|
7
|
+
foo_count = ENV.fetch("FOO_TOTAL", "100").to_i
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
This works okay! And it's not really that complicated, working out what's going on here isn't
|
|
11
|
+
that difficult. How about this?
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
enable_foo = ENV.fetch("ENABLE_FOO", "false") != "true"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
That.. looks very similar, but means the opposite thing. Also not that tricky. You don't start
|
|
18
|
+
to _wish_ for a tool like EnvironmentHelpers until these constructions get complex and compounded.
|
|
19
|
+
But.. if you're using a 12-factor style of configuration, you probably have these ENV-fetches
|
|
20
|
+
sprinkled _everywhere_, so making them moderately clearer or simpler can pay off fairly quickly.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
gem "environment_helper"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
There's not much to it - add the gem to your gemfile and when it's loaded it'll add some extra
|
|
29
|
+
methods onto `ENV` for your use.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
ENV.string("APP_NAME", default: "local")
|
|
35
|
+
ENV.symbol("BUSINESS_DOMAIN", default: :engineering, required: true)
|
|
36
|
+
ENV.boolean("ENABLE_FEATURE_FOO", default: false)
|
|
37
|
+
ENV.integer("MAX_THREAD_COUNT", default: 5)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Each of the supplied methods takes a positional parameter for the name of the environment variable,
|
|
41
|
+
and then two optional named parameters `default` and `required` (there's no point in supplying
|
|
42
|
+
both, but nothing stops you from doing so). The `default` value is the value used if the variable
|
|
43
|
+
isn't present in the environment. If `required` is set to a truthy value, then if the variable isn't
|
|
44
|
+
present in the environment, an `EnvironmentHelpers::MissingVariableError` is raised.
|
|
45
|
+
|
|
46
|
+
The available methods added to `ENV`:
|
|
47
|
+
* `string` - environment values are already strings, so this is the simplest of the methods.
|
|
48
|
+
* `symbol` - produces a symbol, and enforces that the default value is either `nil` or a Symbol.
|
|
49
|
+
* `boolean` - produces `nil`, `true`, or `false` (and only allows those as defaults). Supports..
|
|
50
|
+
a fair variety of strings to map onto those boolean value, though you should probably just use
|
|
51
|
+
"true" and "false" really. If you specify `required: true` and get a value like "maybe?", it'll
|
|
52
|
+
raise an `EnvironmentHelpers::InvalidBooleanText` exception.
|
|
53
|
+
* `integer` - produces an integer from the environment variable, by calling `to_i` on it (if it's
|
|
54
|
+
present). Note that this means that providing a value like "hello" means you'll get `0`, since
|
|
55
|
+
that's what ruby does when you call `"hello".to_i`.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require_relative "lib/environment_helpers/version"
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "environment_helpers"
|
|
5
|
+
spec.version = EnvironmentHelpers::VERSION
|
|
6
|
+
spec.authors = ["Eric Mueller"]
|
|
7
|
+
spec.email = ["nevinera@gmail.com"]
|
|
8
|
+
|
|
9
|
+
spec.summary = "A set of convenience methods for accessing environment data"
|
|
10
|
+
spec.description = <<~DESC
|
|
11
|
+
Convenience helpers for more simply accessing data passed to applications through the
|
|
12
|
+
environment that may have types and/or defaults
|
|
13
|
+
DESC
|
|
14
|
+
spec.homepage = "https://github.com/nevinera/environment_helpers"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
|
17
|
+
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
|
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
22
|
+
`git ls-files -z`
|
|
23
|
+
.split("\x0")
|
|
24
|
+
.reject { |f| f.start_with?("spec") }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
spec.bindir = "bin"
|
|
28
|
+
spec.executables = []
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_development_dependency "rspec", ">= 3.10"
|
|
32
|
+
spec.add_development_dependency "pry"
|
|
33
|
+
spec.add_development_dependency "standard"
|
|
34
|
+
spec.add_development_dependency "rubocop"
|
|
35
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module EnvironmentHelpers
|
|
2
|
+
module AccessHelpers
|
|
3
|
+
private
|
|
4
|
+
|
|
5
|
+
def fetch_value(name, required:)
|
|
6
|
+
required ? fetch(name) : fetch(name, nil)
|
|
7
|
+
rescue KeyError
|
|
8
|
+
fail(MissingVariableError, "The environment variable '#{name}' was required but not supplied")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def check_default_type(context, value, *types)
|
|
12
|
+
return if value.nil?
|
|
13
|
+
return if types.any? { |t| value.is_a?(t) }
|
|
14
|
+
fail(BadDefault, "Inappropriate default value for ENV.#{context}")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def check_default_value(context, value, allow:)
|
|
18
|
+
return if allow.include?(value)
|
|
19
|
+
fail(BadDefault, "Inappropriate default value for ENV.#{context}")
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require "set"
|
|
2
|
+
|
|
3
|
+
module EnvironmentHelpers
|
|
4
|
+
module BooleanHelpers
|
|
5
|
+
TRUTHY_STRINGS = %w[true yes on enabled enable allow t y 1 ok okay].to_set
|
|
6
|
+
FALSEY_STRINGS = %w[false no off disabled disable deny f n 0 nope].to_set
|
|
7
|
+
BOOLEAN_VALUES = [true, false, nil].to_set
|
|
8
|
+
|
|
9
|
+
def boolean(name, default: nil, required: false)
|
|
10
|
+
check_default_value(:boolean, default, allow: [nil, true, false])
|
|
11
|
+
text = fetch_value(name, required: required)
|
|
12
|
+
|
|
13
|
+
return default if text.nil?
|
|
14
|
+
return true if truthy_text?(text)
|
|
15
|
+
return false if falsey_text?(text)
|
|
16
|
+
|
|
17
|
+
return default unless required
|
|
18
|
+
fail(InvalidBooleanText, "Required boolean environment variable #{name} had inappropriate content '#{text}'")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def truthy_text?(text)
|
|
24
|
+
return false if text.nil?
|
|
25
|
+
TRUTHY_STRINGS.include?(text.strip.downcase)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def falsey_text?(text)
|
|
29
|
+
return false if text.nil?
|
|
30
|
+
FALSEY_STRINGS.include?(text.strip.downcase)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module EnvironmentHelpers
|
|
2
|
+
module StringHelpers
|
|
3
|
+
def string(name, default: nil, required: false)
|
|
4
|
+
fetch_value(name, required: required) || default
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def symbol(name, default: nil, required: false)
|
|
8
|
+
check_default_type(:symbol, default, Symbol)
|
|
9
|
+
string(name, default: default, required: required)&.to_sym
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require_relative "./environment_helpers/access_helpers"
|
|
2
|
+
require_relative "./environment_helpers/string_helpers"
|
|
3
|
+
require_relative "./environment_helpers/boolean_helpers"
|
|
4
|
+
require_relative "./environment_helpers/numeric_helpers"
|
|
5
|
+
|
|
6
|
+
module EnvironmentHelpers
|
|
7
|
+
Error = Class.new(::StandardError)
|
|
8
|
+
MissingVariableError = Class.new(Error)
|
|
9
|
+
BadDefault = Class.new(Error)
|
|
10
|
+
|
|
11
|
+
InvalidValue = Class.new(Error)
|
|
12
|
+
InvalidBooleanText = Class.new(InvalidValue)
|
|
13
|
+
InvalidIntegerText = Class.new(InvalidValue)
|
|
14
|
+
|
|
15
|
+
include AccessHelpers
|
|
16
|
+
include StringHelpers
|
|
17
|
+
include BooleanHelpers
|
|
18
|
+
include NumericHelpers
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
ENV.extend(EnvironmentHelpers)
|
metadata
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: environment_helpers
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eric Mueller
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-04-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.10'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: pry
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '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'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: standard
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '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'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rubocop
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
description: |
|
|
70
|
+
Convenience helpers for more simply accessing data passed to applications through the
|
|
71
|
+
environment that may have types and/or defaults
|
|
72
|
+
email:
|
|
73
|
+
- nevinera@gmail.com
|
|
74
|
+
executables: []
|
|
75
|
+
extensions: []
|
|
76
|
+
extra_rdoc_files: []
|
|
77
|
+
files:
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- ".rspec"
|
|
80
|
+
- Gemfile
|
|
81
|
+
- README.md
|
|
82
|
+
- environment_helpers.gemspec
|
|
83
|
+
- lib/environment_helpers.rb
|
|
84
|
+
- lib/environment_helpers/access_helpers.rb
|
|
85
|
+
- lib/environment_helpers/boolean_helpers.rb
|
|
86
|
+
- lib/environment_helpers/numeric_helpers.rb
|
|
87
|
+
- lib/environment_helpers/string_helpers.rb
|
|
88
|
+
- lib/environment_helpers/version.rb
|
|
89
|
+
homepage: https://github.com/nevinera/environment_helpers
|
|
90
|
+
licenses:
|
|
91
|
+
- MIT
|
|
92
|
+
metadata:
|
|
93
|
+
homepage_uri: https://github.com/nevinera/environment_helpers
|
|
94
|
+
source_code_uri: https://github.com/nevinera/environment_helpers
|
|
95
|
+
post_install_message:
|
|
96
|
+
rdoc_options: []
|
|
97
|
+
require_paths:
|
|
98
|
+
- lib
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 2.7.0
|
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0'
|
|
109
|
+
requirements: []
|
|
110
|
+
rubygems_version: 3.1.6
|
|
111
|
+
signing_key:
|
|
112
|
+
specification_version: 4
|
|
113
|
+
summary: A set of convenience methods for accessing environment data
|
|
114
|
+
test_files: []
|