early 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: afe1d324298d4e3b6ee90653d46d3f12aa82fb03faf80863a43a2b0d6e70d219
4
+ data.tar.gz: 2c9e6c181582728c3161e98ab33685f2246f9259a7ba74bdb8e29b62ddfc4144
5
+ SHA512:
6
+ metadata.gz: 57a93b949787e7d0126dec845d65da1dcc663c1d1bb4abcacdf4b90fdceb2cc3d15b72e15d2a80afcc324859bc606d1e7efde0c437d74b671ba7b1bbdaa1b2cf
7
+ data.tar.gz: bb08b31946e9f6fa2e172029082bd3228106b04f4c9506283aa2973fc287e59d585cf597c3b0915b284321ff0e21b342091baa433125944581043acf17d98f74
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at gsamokovarov@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in early.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Genadi Samokovarov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Early
2
+
3
+ Early checks for environment variables availability, so you don't have to. Hook
4
+ it early in your program to require or default a variable and then work with
5
+ `ENV` like you normally would. Extremely useful for [Twelve-Factor apps][12f].
6
+
7
+ ## Usage
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'early'
13
+ ```
14
+
15
+ Afterwards make sure to call `Early` as early as possible in your application,
16
+ to check the `ENV` variables, before you use them in your configuration layer:
17
+
18
+ ```ruby
19
+ Early do
20
+ require :DATABASE_URL
21
+ require :REDIS_URL
22
+
23
+ default :PROVIDER, :generic
24
+ end
25
+ ```
26
+
27
+ The configuration will require the presence of `DATABASE_URL` and `REDIS_URL`
28
+ and will raise `Early::Error` if any of them is missing. It will also set a
29
+ default value to the env `PROVIDER`.
30
+
31
+ ### Rails
32
+
33
+ If you want to use early with Rails, you can store the early configuration in
34
+ `config/early.rb`:
35
+
36
+
37
+ ```ruby
38
+ Early do
39
+ require :ADMIN_NAME, :ADMIN_PASSWORD
40
+ require :MEETUP_API_KEY
41
+ end
42
+ ```
43
+
44
+ More importantly, require it in `config/boot.rb`, which is executed before the
45
+ `config/application.rb` and `config/initializers` files:
46
+
47
+ ```ruby
48
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
49
+
50
+ require 'bundler/setup' # Set up gems listed in the Gemfile.
51
+ require 'bootsnap/setup'
52
+
53
+ require_relative 'early' # 👈
54
+ ```
55
+
56
+ This will make sure, that the rules you wanted early to enforce have been
57
+ applied before any code in `config` has been run.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT
62
+ License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "early"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/early.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'early'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'early'
7
+ spec.version = Early::VERSION
8
+ spec.authors = ['Genadi Samokovarov']
9
+ spec.email = ['gsamokovarov@gmail.com']
10
+
11
+ spec.summary = 'Checks for environment variables early in your program.'
12
+ spec.description = 'Checks for environment variables early in your program.'
13
+ spec.homepage = 'https://github.com/gsamokovarov'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'minitest', '~> 5.0'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
data/lib/early.rb ADDED
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Early checks for environment variables availability, so you don't have to.
4
+ #
5
+ # Hook it early in your program and work with `ENV` like you normally would.
6
+ # This time, however, the errors would be thrown early and not when a critical
7
+ # piece of code is hit, which may happen late in the program runtime an be easy
8
+ # to miss.
9
+ module Early
10
+ VERSION = '0.1.0'
11
+
12
+ class Configuration # :nodoc:
13
+ attr_reader :variables
14
+
15
+ def initialize(&block)
16
+ @variables = []
17
+
18
+ instance_eval(&block)
19
+ end
20
+
21
+ private
22
+
23
+ def require(*names)
24
+ names.each do |name|
25
+ @variables << RequiredVariable.new(name)
26
+ end
27
+ end
28
+
29
+ def default(name, value)
30
+ @variables << DefaultVariable.new(name, value)
31
+ end
32
+ end
33
+
34
+ class DefaultVariable # :nodoc:
35
+ attr_reader :name, :value
36
+
37
+ def initialize(name, value)
38
+ @name = String(name)
39
+ @value = String(value)
40
+ end
41
+
42
+ def apply
43
+ ENV[name] ||= value
44
+ end
45
+ end
46
+
47
+ class RequiredVariable # :nodoc:
48
+ attr_reader :name
49
+
50
+ def initialize(name)
51
+ @name = String(name)
52
+ end
53
+
54
+ def apply
55
+ ENV.fetch(name) { raise Error, self }
56
+ end
57
+ end
58
+
59
+ # Error is raised when an environment variable is missing.
60
+ class Error < KeyError
61
+ attr_reader :variable
62
+
63
+ def initialize(variable)
64
+ @variable = variable
65
+
66
+ super("Variable ENV[#{variable.name.inspect}] is missing")
67
+ end
68
+ end
69
+
70
+ # Env returns the early environment. This is either the value of RAILS_ENV,
71
+ # RACK_ENV (in order) or the string <tt>'development'</tt> if neither of the
72
+ # aforementioned environment variables are present.
73
+ def self.env
74
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
75
+ end
76
+
77
+ # Applies a configuration, which means every variable is either defaulted or
78
+ # checked for existence.
79
+ def self.apply(config)
80
+ config.variables.each(&:apply)
81
+ end
82
+ end
83
+
84
+ module Kernel
85
+ # Early checks for environment variables availability. It provides a DSL that
86
+ # can require or set a default value for an environment variable. This will
87
+ # help you catch configuration errors earlier in your application runtime.
88
+ # Here is an example:
89
+ #
90
+ # Early do
91
+ # require :REDIS_URL
92
+ # default :PROVIDER, :generic
93
+ # end
94
+ #
95
+ # Calling <tt>require :REDIS_URL</tt> will fail if <tt>ENV['REDIS_URL']</tt> is
96
+ # <tt>nil</tt>, which means that it wasn't provided, before running the current
97
+ # application. The error will give you information about the missing variable:
98
+ #
99
+ # Early::Error (Variable ENV["REDIS_URL"] is missing)
100
+ #
101
+ # Setting a default calls <tt>ENV['PROVIDER'] ||= 'generic'</tt> under the
102
+ # hood. Every time you use <tt>ENV['PROVIDER']</tt> will give you
103
+ # <tt>'generic'</tt>. No need to set it explicitly prior to the application
104
+ # run.
105
+ #
106
+ # A quick note about how <tt>ENV</tt> works in Ruby. It is a plain
107
+ # <tt>Object</tt> that is monkey patched to behave a bit like a hash. You can
108
+ # get an variable with <tt>ENV['NAME']</tt> and you can set an environment
109
+ # variable with <tt>ENV['NAME'] = 'val'</tt>.
110
+ #
111
+ # Both of the operations explicitly require strings for the variable name and
112
+ # value. Passing a symbol to <tt>ENV[:NAME]</tt> will result in an error. The
113
+ # same will happen if you try to set a variable to any non-string value, like
114
+ # <tt>ENV['NAME'] = :val</tt>. Early converts both the name and the value to
115
+ # strings when it eventually deals with <tt>ENV</tt>, so you don't have to
116
+ # worry about it.
117
+ def Early(*envs, &block)
118
+ config = Early::Configuration.new(&block)
119
+
120
+ if envs.empty? || envs.find { |e| Early.env == e.to_s }
121
+ Early.apply(config)
122
+ end
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: early
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genadi Samokovarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Checks for environment variables early in your program.
56
+ email:
57
+ - gsamokovarov@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - early.gemspec
72
+ - lib/early.rb
73
+ homepage: https://github.com/gsamokovarov
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Checks for environment variables early in your program.
97
+ test_files: []