dotenv-schema-rails 0.0.1

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
+ SHA1:
3
+ metadata.gz: 93df2b54e8b80293e76f260b9073801257c0be8d
4
+ data.tar.gz: acf240ce0ee51c2355a8d3cc2bf2d1eae9590482
5
+ SHA512:
6
+ metadata.gz: c3ced370670d19da2c563f0f006c2ef19fc4cf16a3eb4d2b164df21d96185f581c70ccbcaf81a02e609782124989b211f5a80bee959a21598ff5bdfa5986561f
7
+ data.tar.gz: 889bc0434c04d6cdac5ad153d67e3759b6227ac0119e3f8f81d3e5c7eb495780973886888432f281f624943c5e6334c20a7234700f4a98b622bd7c9ad60a461b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.8.7
6
+ - ree
data/Changelog.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 0.0.1
4
+ * First release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :name => 'dotenv-schema'
3
+
4
+ gem 'guard-rspec'
5
+ gem 'guard-bundler'
6
+ gem 'rb-fsevent'
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ end
4
+
5
+ guard :rspec do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Issei Naruta
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,103 @@
1
+ # dotenv-schema [![Build Status](https://travis-ci.org/mirakui/dotenv-schema.png?branch=master)](https://travis-ci.org/mirakui/dotenv-schema)
2
+
3
+ Dotenv-schema makes [dotenv](https://github.com/bkeepers/dotenv) schemaful.
4
+
5
+ ## Installation
6
+ Write `.env` and `.env_schema`:
7
+
8
+ ```shell
9
+ $ cat .env
10
+ DB_HOST=db.example.com
11
+ DB_PORT=3306
12
+
13
+ $ cat .env_schema
14
+ DB_HOST:
15
+ DB_PORT:
16
+ ```
17
+
18
+ ### Rails
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'dotenv-shcmea-rails'
23
+ # DO NOT load dotenv-rails gem! Use dotenv-schema-rails instead.
24
+ ```
25
+
26
+ And then execute:
27
+ ```
28
+ $ bundle
29
+ $ rails server
30
+ ```
31
+ It fails Dotenv::Schema::ValidationError if the validation failed.
32
+
33
+ ### Sinatra or Plain ol' Ruby
34
+ ```
35
+ $ gem install dotenv
36
+ ```
37
+
38
+ ```ruby
39
+ require 'dotenv-schema'
40
+ Dotenv.load # raises Dotenv::Schema::ValidationError if the validation failed.
41
+ ```
42
+
43
+ ## Schema validation rule and `.env_schema` format
44
+ `.env_schema` is based on YAML format.
45
+
46
+ ### Basic rule
47
+ It fails validation If any variables which are defined in `.env_schema` don't exist in `.env`.
48
+ ```shell
49
+ # .env_schema
50
+ DB_HOST:
51
+ DB_PORT:
52
+
53
+ # .env
54
+ DB_HOST=db.example.com
55
+ # => Dotenv::Schema::ValidationError: ENV['DB_PORT'] must exist
56
+ ```
57
+
58
+ It fails validation If any variables which aren't defined in `.env_schema` exist in `.env`.
59
+ ```shell
60
+ # .env_schema
61
+ DB_HOST:
62
+
63
+ # .env
64
+ DB_HOST=db.example.com
65
+ DB_PORT=3306
66
+ # => Dotenv::Schema::ValidationError: Undefined variable(s): DB_PORT; Please add them into .env_schema
67
+ ```
68
+
69
+ ### To allow empty string
70
+ In default, it fails if any environment variables in `.env` are empty string.
71
+ ```shell
72
+ # .env_schema
73
+ DB_HOST:
74
+
75
+ # .env
76
+ DB_HOST=
77
+ # => Dotenv::Schema::ValidationError: ENV['DB_HOST'] must not be empty string
78
+ ```
79
+
80
+ You can allow it by using `allow_empty_string` option.
81
+
82
+ ```shell
83
+ # .env_schema
84
+ DB_HOST:
85
+ allow_empty_string: true
86
+
87
+ # .env
88
+ DB_HOST=
89
+ # => Passes validation even though DB_HOST is empty
90
+ ```
91
+
92
+ ### To allow variables to don't exist
93
+ ```shell
94
+ # .env_schema
95
+ DB_HOST:
96
+ DB_PORT:
97
+ allow_not_exists: true
98
+
99
+ # .env
100
+ DB_HOST=
101
+ # => Passes validation even though DB_PORT doesn't exist
102
+ ```
103
+
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_helper'
4
+
5
+ namespace 'dotenv-schema' do
6
+ Bundler::GemHelper.install_tasks :name => 'dotenv-schema'
7
+ end
8
+
9
+ namespace 'dotenv-schema-rails' do
10
+ class DotenvSchemaRailsGemHelper < Bundler::GemHelper
11
+ def guard_already_tagged; end # noop
12
+ def tag_version; end # noop
13
+ end
14
+
15
+ DotenvSchemaRailsGemHelper.install_tasks :name => 'dotenv-schema-rails'
16
+ end
17
+
18
+ task :build => ["dotenv-schema:build", 'dotenv-schema-rails:build']
19
+ task :install => ["dotenv-schema:install", 'dotenv-schema-rails:install']
20
+ task :release => ["dotenv-schema:release", 'dotenv-schema-rails:release']
21
+
22
+ require 'rspec/core/rake_task'
23
+
24
+ desc "Run all specs"
25
+ RSpec::Core::RakeTask.new(:spec) do |t|
26
+ t.rspec_opts = %w[--color]
27
+ t.verbose = false
28
+ end
29
+
30
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dotenv-schema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dotenv-schema-rails"
8
+ spec.version = Dotenv::SCHEMA_VERSION
9
+ spec.authors = ["Issei Naruta"]
10
+ spec.email = ["naruta@cookpad.com"]
11
+ spec.description = %q{Loads environment variables with dotenv-schema in Rails}
12
+ spec.summary = %q{Loads environment variables with dotenv-schema in Rails}
13
+ spec.homepage = "https://github.com/mirakui/dotenv-schema"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dotenv-schema", Dotenv::SCHEMA_VERSION
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dotenv-schema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dotenv-schema"
8
+ spec.version = Dotenv::SCHEMA_VERSION
9
+ spec.authors = ["Issei Naruta"]
10
+ spec.email = ["naruta@cookpad.com"]
11
+ spec.description = %q{Defines schema for dotenv}
12
+ spec.summary = %q{Defines schema for dotenv}
13
+ spec.homepage = "https://github.com/mirakui/dotenv-schema"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dotenv"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,2 @@
1
+ require 'dotenv-schema'
2
+ require 'dotenv/railtie'
@@ -0,0 +1,25 @@
1
+ require 'dotenv'
2
+ require 'dotenv/schema'
3
+
4
+ module Dotenv
5
+ class << self
6
+ def load_with_validation(*filenames)
7
+ env_before = ENV.to_hash
8
+ load_without_validation *filenames
9
+ env = ENV.to_hash.reject {|k, v| env_before.has_key?(k) }
10
+
11
+ schema = Schema.load schema_path
12
+ schema.validate! env
13
+ end
14
+ alias_method :load_without_validation, :load
15
+ alias_method :load, :load_with_validation
16
+
17
+ def schema_path=(path)
18
+ @schema_path = path
19
+ end
20
+
21
+ def schema_path
22
+ @schema_path || '.env_schema'
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Dotenv
2
+ SCHEMA_VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'dotenv-schema'
2
+ require 'yaml'
3
+
4
+ module Dotenv
5
+ class Schema < Hash
6
+ class ValidationError < StandardError; end
7
+
8
+ def initialize(hash={})
9
+ replace hash if hash
10
+ end
11
+
12
+ def self.load(file)
13
+ new YAML.load(File.read(file))
14
+ end
15
+
16
+ def validate!(env)
17
+ undefined_keys = env.keys - keys
18
+ unless undefined_keys.empty?
19
+ raise ValidationError, "Undefined variable(s): #{undefined_keys.join(', ')}; Please add them into #{Dotenv.schema_path}"
20
+ end
21
+ each do |key, options|
22
+ if env[key] == '' && (options && !options['allow_empty_string'] || !options)
23
+ raise ValidationError, "ENV['#{key}'] must not be empty string"
24
+ end
25
+ if !env.has_key?(key) && (options && !options['allow_not_exists'] || !options)
26
+ raise ValidationError, "ENV['#{key}'] must exist"
27
+ end
28
+ end
29
+ true
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'dotenv'
3
+ require 'dotenv-schema'
4
+ require 'tempfile'
5
+
6
+ describe 'Dotenv Schema' do
7
+ before(:all) do
8
+ Dotenv.schema_path = file <<-END
9
+ KEY1:
10
+ allow_empty_string: true
11
+ allow_not_exists: true
12
+ KEY2:
13
+ KEY3:
14
+ allow_empty_string: false
15
+ END
16
+ end
17
+
18
+ before do
19
+ %w[KEY1 KEY2 KEY3].each {|k| ENV.delete(k) }
20
+ end
21
+
22
+ it do
23
+ expect {
24
+ Dotenv.load file <<-END
25
+ KEY1=value1
26
+ KEY2=value2
27
+ KEY3=value3
28
+ END
29
+ }.not_to raise_error
30
+ end
31
+
32
+ it do
33
+ expect {
34
+ Dotenv.load file <<-END
35
+ KEY1=value1
36
+ KEY3=value3
37
+ END
38
+ }.to raise_error(Dotenv::Schema::ValidationError)
39
+ end
40
+
41
+ it do
42
+ expect {
43
+ Dotenv.load file <<-END
44
+ KEY1=
45
+ KEY2=value2
46
+ KEY3=value3
47
+ END
48
+ }.not_to raise_error
49
+ end
50
+
51
+ it do
52
+ expect {
53
+ Dotenv.load file <<-END
54
+ KEY1=value1
55
+ KEY2=value2
56
+ KEY3=
57
+ END
58
+ }.to raise_error(Dotenv::Schema::ValidationError)
59
+ end
60
+
61
+ it do
62
+ expect {
63
+ Dotenv.load file <<-END
64
+ KEY1=value1
65
+ KEY2=
66
+ KEY3=value3
67
+ END
68
+ }.to raise_error(Dotenv::Schema::ValidationError)
69
+ end
70
+
71
+ it do
72
+ expect {
73
+ Dotenv.load file <<-END
74
+ KEY2=value2
75
+ KEY3=value3
76
+ END
77
+ }.not_to raise_error
78
+ end
79
+
80
+ it do
81
+ expect {
82
+ Dotenv.load file <<-END
83
+ KEY1=value1
84
+ KEY2=value2
85
+ KEY3=value3
86
+ KEY4=value4
87
+ END
88
+ }.to raise_error(Dotenv::Schema::ValidationError)
89
+ end
90
+
91
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'dotenv/schema'
3
+
4
+ describe Dotenv::Schema do
5
+ let(:schema) {
6
+ described_class.load file <<-END
7
+ KEY1:
8
+ allow_empty_string: true
9
+ allow_not_exists: true
10
+ KEY2:
11
+ KEY3:
12
+ allow_empty_string: false
13
+ END
14
+ }
15
+
16
+ it do
17
+ expect(schema).to eq({
18
+ 'KEY1' => { 'allow_empty_string' => true, 'allow_not_exists' => true },
19
+ 'KEY2' => nil,
20
+ 'KEY3' => { 'allow_empty_string' => false }
21
+ })
22
+ end
23
+
24
+ it do
25
+ expect {
26
+ schema.validate!({
27
+ 'KEY1' => 'VALUE1',
28
+ 'KEY2' => 'VALUE2',
29
+ 'KEY3' => 'VALUE3'
30
+ })
31
+ }.not_to raise_error
32
+ end
33
+
34
+ it do
35
+ expect {
36
+ schema.validate!({
37
+ 'KEY1' => 'VALUE1',
38
+ 'KEY3' => 'VALUE3'
39
+ })
40
+ }.to raise_error(Dotenv::Schema::ValidationError)
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'tempfile'
20
+ $tmp_files = []
21
+
22
+ def file(str)
23
+ tmp = Tempfile.new('dotenv-schema.')
24
+ tmp.puts str
25
+ tmp.flush
26
+ $tmp_files << tmp
27
+ tmp.path
28
+ end
data/test.env ADDED
@@ -0,0 +1,2 @@
1
+ TEST1=
2
+ TEST2=value
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotenv-schema-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Issei Naruta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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
+ description: Loads environment variables with dotenv-schema in Rails
56
+ email:
57
+ - naruta@cookpad.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Changelog.md
66
+ - Gemfile
67
+ - Guardfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - dotenv-schema-rails.gemspec
72
+ - dotenv-schema.gemspec
73
+ - lib/dotenv-schema-rails.rb
74
+ - lib/dotenv-schema.rb
75
+ - lib/dotenv-schema/version.rb
76
+ - lib/dotenv/schema.rb
77
+ - spec/dotenv_schema_spec.rb
78
+ - spec/lib/dotenv/schema_spec.rb
79
+ - spec/spec_helper.rb
80
+ - test.env
81
+ homepage: https://github.com/mirakui/dotenv-schema
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Loads environment variables with dotenv-schema in Rails
105
+ test_files:
106
+ - spec/dotenv_schema_spec.rb
107
+ - spec/lib/dotenv/schema_spec.rb
108
+ - spec/spec_helper.rb