app_env 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +20 -0
- data/app_env.gemspec +17 -0
- data/lib/app_env.rb +49 -0
- data/lib/app_env/version.rb +3 -0
- data/spec/app_env_spec.rb +114 -0
- data/spec/env +2 -0
- metadata +58 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Francesc Esplugas
|
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,50 @@
|
|
1
|
+
# Ruby - AppEnv
|
2
|
+
|
3
|
+
Write `.env` files from Ruby.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'app_env'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install app_env
|
19
|
+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Read or initialize `.env` file:
|
24
|
+
|
25
|
+
@app_env = AppEnv.new('.env')
|
26
|
+
|
27
|
+
Set a key:
|
28
|
+
|
29
|
+
@app_env.set('foo', 'bar')
|
30
|
+
|
31
|
+
Get a key:
|
32
|
+
|
33
|
+
@app_env.get('foo')
|
34
|
+
|
35
|
+
Delete a key:
|
36
|
+
|
37
|
+
@app_env.delete('foo')
|
38
|
+
|
39
|
+
Save `env` file:
|
40
|
+
|
41
|
+
@app_env.save
|
42
|
+
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Default: run specs.'
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc "Run specs"
|
10
|
+
RSpec::Core::RakeTask.new do |t|
|
11
|
+
# t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
12
|
+
# Put spec opts in a file named .rspec in root
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Generate code coverage"
|
16
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
17
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
18
|
+
t.rcov = true
|
19
|
+
t.rcov_opts = ['--exclude', 'spec']
|
20
|
+
end
|
data/app_env.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/app_env/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Francesc Esplugas"]
|
6
|
+
gem.email = ["contact@francescesplugas.com"]
|
7
|
+
gem.description = %q{Write env files from Ruby.}
|
8
|
+
gem.summary = %q{Write env files from Ruby.}
|
9
|
+
gem.homepage = "http://github.com/fesplugas/ruby-app_env"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "app_env"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AppEnv::VERSION
|
17
|
+
end
|
data/lib/app_env.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
class AppEnv
|
2
|
+
|
3
|
+
attr_accessor :env
|
4
|
+
attr_accessor :file
|
5
|
+
|
6
|
+
def initialize(file)
|
7
|
+
@file = file
|
8
|
+
@env = {}
|
9
|
+
|
10
|
+
if File.exist?(file)
|
11
|
+
data = File.read(file)
|
12
|
+
|
13
|
+
data.each_line do |line|
|
14
|
+
env = line.strip.split('=')
|
15
|
+
key = env.first.downcase
|
16
|
+
value = env.last
|
17
|
+
@env[key] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return true/false
|
23
|
+
def set(key, value)
|
24
|
+
env[key.downcase] = value
|
25
|
+
end
|
26
|
+
|
27
|
+
# Return the value.
|
28
|
+
def get(key)
|
29
|
+
env[key.downcase]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return true/false
|
33
|
+
def delete(key)
|
34
|
+
env.delete(key)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Prepare data to dump.
|
38
|
+
def data
|
39
|
+
env.map { |k, v| "#{k.upcase}=#{v}\n" }.join
|
40
|
+
end
|
41
|
+
|
42
|
+
# Write to disk.
|
43
|
+
def save
|
44
|
+
path = File.dirname(file)
|
45
|
+
FileUtils.mkdir_p(path) unless Dir.exist?(path)
|
46
|
+
File.open(file, 'w') { |f| f.write(data) }
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require './lib/app_env'
|
3
|
+
|
4
|
+
describe AppEnv do
|
5
|
+
|
6
|
+
describe 'initialize' do
|
7
|
+
|
8
|
+
it 'should raise an error if file is not provided' do
|
9
|
+
expect { AppEnv.new }.to raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'file' do
|
15
|
+
|
16
|
+
before do
|
17
|
+
@env = AppEnv.new('spec/env')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return the file containing env vars' do
|
21
|
+
@env.file.should == 'spec/env'
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'set' do
|
27
|
+
|
28
|
+
before do
|
29
|
+
@env = AppEnv.new('spec/env')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should set a key' do
|
33
|
+
@env.set('foo', 'bar').should == 'bar'
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'get' do
|
39
|
+
|
40
|
+
before do
|
41
|
+
@env = AppEnv.new('spec/env')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should get a key' do
|
45
|
+
@env.get('SMTP_HOSTNAME').should == 'localhost'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return false when key not found' do
|
49
|
+
@env.get('UNEXISTING').should be_nil
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'delete' do
|
55
|
+
|
56
|
+
before do
|
57
|
+
@env = AppEnv.new('spec/env')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should delete a key' do
|
61
|
+
@env.env.keys.include?('smtp_hostname').should be_true
|
62
|
+
@env.delete('smtp_hostname')
|
63
|
+
@env.env.keys.include?('smtp_hostname').should be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should return nil when trying to delete a key which does not exist' do
|
67
|
+
@env.delete('foo').should be_nil
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'data' do
|
73
|
+
|
74
|
+
before do
|
75
|
+
@env = AppEnv.new('spec/env')
|
76
|
+
@env.set('foo', 'bar')
|
77
|
+
@env.delete('smtp_hostname')
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should generate the data which is going to be saved' do
|
81
|
+
expected = <<-DATA
|
82
|
+
APP_DOMAIN=example.com
|
83
|
+
FOO=bar
|
84
|
+
DATA
|
85
|
+
|
86
|
+
@env.data.should == expected
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'save' do
|
92
|
+
|
93
|
+
before do
|
94
|
+
@env_test = 'spec/env_test'
|
95
|
+
@env = AppEnv.new(@env_test)
|
96
|
+
@env.set('FOO', 'bar')
|
97
|
+
end
|
98
|
+
|
99
|
+
after do
|
100
|
+
File.delete(@env_test)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should dump env content to disk' do
|
104
|
+
expected = <<-DATA
|
105
|
+
FOO=bar
|
106
|
+
DATA
|
107
|
+
|
108
|
+
@env.save
|
109
|
+
File.read(@env_test).should == expected
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
data/spec/env
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app_env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Francesc Esplugas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Write env files from Ruby.
|
15
|
+
email:
|
16
|
+
- contact@francescesplugas.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- app_env.gemspec
|
28
|
+
- lib/app_env.rb
|
29
|
+
- lib/app_env/version.rb
|
30
|
+
- spec/app_env_spec.rb
|
31
|
+
- spec/env
|
32
|
+
homepage: http://github.com/fesplugas/ruby-app_env
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.23
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Write env files from Ruby.
|
56
|
+
test_files:
|
57
|
+
- spec/app_env_spec.rb
|
58
|
+
- spec/env
|