rails_db_config_resolver 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 886200010d0a03d6d08bc7bc9952494ef6353f1b
4
+ data.tar.gz: 530d0e66d11c381cf0142c6ea3b2a21cbd3ec92e
5
+ SHA512:
6
+ metadata.gz: bb0c27eda5bc7fa74f403a244ecfce8f8c0fd1b2b8a368a4af6a0439cb795ace2691cef7e4e08aedfd59cff73396b45f2e566cf516d26382b20ec5e9f2bf4858
7
+ data.tar.gz: 5cb4070e87474a13266b7802ca1a9055fae706077e526ec7e9423742761cac859f75e8d7f49cdfa34617f913ccc60d176dc290a5ef9f297e85dea45ae059b140
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_db_config_resolver.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cameron Martin
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,38 @@
1
+ # RailsDbConfigResolver
2
+
3
+ Resolves database configuration in the same way that rails does.
4
+ This is useful when wanting to connect to rail's database without loading the rails environment or
5
+ when wanting to use [em-pg-client][1] for a helper process in a rails app.
6
+
7
+ Values from `ENV['DATABASE_URL']` and `config/database.yml` are merged. `ENV['DATABASE_URL']` trumps `config/database.yml`.
8
+ By looking at [the rails guide][2], I _think_ the url field in `database.yml` trumps both of them, so I'm not sure, so I left it out.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'rails_db_config_resolver'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rails_db_config_resolver
23
+
24
+ ## Usage
25
+
26
+ For now, check out `spec/rails_db_config_resolver_spec.rb`.
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/cameron-martin/rails_db_config_resolver/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
35
+
36
+
37
+ [1]: https://github.com/royaltm/ruby-em-pg-client
38
+ [2]: http://guides.rubyonrails.org/configuring.html#configuring-a-database
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,79 @@
1
+ require 'uri'
2
+ require 'rattributes'
3
+
4
+ class RailsDbConfigResolver
5
+ class DatabaseConfig
6
+
7
+ include Rattributes.new(:adapter, :host, :port, :username, :password, :database, :pool)
8
+
9
+ def self.from_url(url_string)
10
+ url = URI.parse(url_string)
11
+
12
+ pool = (url.query.match(/pool=(\d+)/)[1] if url.query)
13
+ database = url.path[1..-1]
14
+
15
+ new(
16
+ adapter: url.scheme,
17
+ host: url.host,
18
+ port: url.port,
19
+ username: url.user,
20
+ password: url.password,
21
+ database: database,
22
+ pool: pool
23
+ )
24
+ end
25
+
26
+ class << self
27
+ alias_method :from_hash, :new
28
+ end
29
+
30
+ def initialize(*)
31
+ super
32
+
33
+ @pool = cast_to_integer_or_nil(@pool)
34
+ end
35
+
36
+ def to_hash
37
+ {
38
+ adapter: @adapter,
39
+ host: @host,
40
+ port: @port,
41
+ username: @username,
42
+ password: @password,
43
+ database: @database,
44
+ pool: @pool
45
+ }
46
+ end
47
+
48
+ def to_url
49
+ URI::Generic.build(build_url_hash).to_s
50
+ end
51
+
52
+ def merge(other)
53
+ my_hash = to_hash
54
+ other_hash = other.to_hash.reject { |_, value| value.nil? }
55
+ self.class.from_hash(my_hash.merge(other_hash))
56
+ end
57
+
58
+ private
59
+
60
+ def build_url_hash
61
+ {
62
+ scheme: @adapter,
63
+ host: @host,
64
+ port: @port,
65
+ path: "/#{@database}"
66
+ }.tap do |hash|
67
+ hash[:query] = "pool=#{@pool}" if @pool
68
+ hash[:userinfo] = [@username, @password].compact.join(':') if @username || @password
69
+ end
70
+
71
+ end
72
+
73
+ def cast_to_integer_or_nil(object)
74
+ return nil unless object.is_a?(Numeric) || /^\d+$/.match(object)
75
+ object.to_i
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ class RailsDbConfigResolver
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'rails_db_config_resolver/version'
2
+ require 'rails_db_config_resolver/database_config'
3
+
4
+ require 'erb'
5
+ require 'yaml'
6
+ require 'uri'
7
+
8
+ class RailsDbConfigResolver
9
+
10
+ def initialize(file, env_url, rails_env)
11
+ @file = file
12
+ @env_url = env_url
13
+ @rails_env = rails_env
14
+ end
15
+
16
+ def parse
17
+ merged_config.to_hash
18
+ end
19
+
20
+ private
21
+
22
+ def merged_config
23
+ if @env_url
24
+ file_config.merge(env_url_config)
25
+ else
26
+ file_config
27
+ end
28
+ end
29
+
30
+ def env_url_config
31
+ DatabaseConfig.from_url(@env_url)
32
+ end
33
+
34
+ def file_config
35
+ DatabaseConfig.from_hash(file_config_hash)
36
+ end
37
+
38
+ def file_config_hash
39
+ symbolize_keys(YAML.load(rendered_yaml)[@rails_env])
40
+ end
41
+
42
+ def raw_yaml
43
+ File.read(@file)
44
+ end
45
+
46
+ def rendered_yaml
47
+ ERB.new(raw_yaml).result
48
+ end
49
+
50
+ def symbolize_keys(hash)
51
+ Hash[hash.map { |k, v| [k.to_sym, v] }]
52
+ end
53
+
54
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_db_config_resolver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rails_db_config_resolver'
8
+ spec.version = RailsDbConfigResolver::VERSION
9
+ spec.authors = ['Cameron Martin']
10
+ spec.email = ['cameronmartin123@gmail.com']
11
+ spec.summary = %q{Rails db configuration, outside of rails}
12
+ spec.description = %q{Use the database configuration from a rails app, without using rails}
13
+ spec.homepage = 'https://github.com/cameron-martin/rails_db_config_resolver'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'rspec-its', '~> 1.0'
25
+
26
+ spec.add_dependency 'rattributes'
27
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsDbConfigResolver::DatabaseConfig do
4
+
5
+ describe '.from_url' do
6
+
7
+ context 'with all fields' do
8
+
9
+ subject { RailsDbConfigResolver::DatabaseConfig.from_url('postgres://username:password@host.com:1234/database?pool=5') }
10
+
11
+ its(:adapter) { should eq('postgres') }
12
+ its(:username) { should eq('username') }
13
+ its(:password) { should eq('password') }
14
+ its(:host) { should eq('host.com') }
15
+ its(:port) { should eq(1234) }
16
+ its(:database) { should eq('database') }
17
+ its(:pool) { should eq(5) }
18
+
19
+ end
20
+
21
+ context 'without pool' do
22
+
23
+ subject { RailsDbConfigResolver::DatabaseConfig.from_url('postgres://username:password@host.com:1234/database') }
24
+
25
+ its(:adapter) { should eq('postgres') }
26
+ its(:username) { should eq('username') }
27
+ its(:password) { should eq('password') }
28
+ its(:host) { should eq('host.com') }
29
+ its(:port) { should eq(1234) }
30
+ its(:database) { should eq('database') }
31
+ its(:pool) { should be_nil }
32
+
33
+ end
34
+
35
+ end
36
+
37
+ describe '#initialize' do
38
+
39
+ context 'pool' do
40
+
41
+ def create_with_pool(pool)
42
+ RailsDbConfigResolver::DatabaseConfig.new(pool: pool)
43
+ end
44
+
45
+ it 'leaves integer pool as is' do
46
+ expect(create_with_pool(5).pool).to eq(5)
47
+ end
48
+
49
+ it 'converts pool to integer' do
50
+ expect(create_with_pool('5').pool).to eq(5)
51
+ end
52
+
53
+ it 'converts empty pool to nil' do
54
+ expect(create_with_pool('').pool).to eq(nil)
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ describe '#merge' do
62
+
63
+ context 'with two urls' do
64
+
65
+ let(:config1) { RailsDbConfigResolver::DatabaseConfig.from_url('postgres://username:password@host.com/database?pool=5') }
66
+ let(:config2) { RailsDbConfigResolver::DatabaseConfig.from_url('postgres://other-host.com/other-database?pool=10') }
67
+
68
+ subject { config1.merge(config2) }
69
+
70
+ its(:adapter) { should eq('postgres') }
71
+ its(:username) { should eq('username') }
72
+ its(:password) { should eq('password') }
73
+ its(:host) { should eq('other-host.com') }
74
+ its(:database) { should eq('other-database') }
75
+ its(:pool) { should eq(10) }
76
+
77
+ end
78
+
79
+ end
80
+
81
+ describe '#to_url' do
82
+
83
+ # Here we test if DatabaseConfig.from_url(url).to_url == url
84
+
85
+ {
86
+ 'with every field' => 'postgres://username:password@host.com:1234/database?pool=5',
87
+ 'without port' => 'postgres://username:password@host.com/database?pool=5',
88
+ 'without pool' => 'postgres://username:password@host.com:1234/database',
89
+ 'without password' => 'postgres://username@host.com:1234/database?pool=5',
90
+ 'without username and password' => 'postgres://host.com:1234/database?pool=5',
91
+ }.each do |description, url|
92
+ context description do
93
+ subject { RailsDbConfigResolver::DatabaseConfig.from_url(url) }
94
+ its(:to_url) { should eq(url) }
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ SPEC_ROOT = __dir__
4
+ DB_FILE = File.join(SPEC_ROOT, 'support', 'database.yml')
5
+
6
+ describe RailsDbConfigResolver do
7
+
8
+ context 'with full database file, and partial env variable' do
9
+
10
+ subject { RailsDbConfigResolver.new(DB_FILE, 'sqlite://env-host.com/env_database', 'production') }
11
+
12
+ its(:parse) { should eq(
13
+ adapter: 'sqlite',
14
+ database: 'env_database',
15
+ pool: 5,
16
+ username: 'file_username',
17
+ password: 'file_password',
18
+ host: 'env-host.com',
19
+ port: 1234
20
+ ) }
21
+
22
+ end
23
+
24
+ context 'with full database file, and no env file' do
25
+
26
+ subject { RailsDbConfigResolver.new(DB_FILE, nil, 'production') }
27
+
28
+ its(:parse) { should eq(
29
+ adapter: 'postgresql',
30
+ database: 'file_database',
31
+ pool: 5,
32
+ username: 'file_username',
33
+ password: 'file_password',
34
+ host: 'file-host.com',
35
+ port: 1234
36
+ ) }
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,4 @@
1
+ require 'rails_db_config_resolver'
2
+
3
+ require 'rspec'
4
+ require 'rspec/its'
@@ -0,0 +1,9 @@
1
+ production:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: file_database
5
+ pool: 5
6
+ username: file_username
7
+ password: file_password
8
+ host: file-host.com
9
+ port: 1234
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_db_config_resolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-08 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rattributes
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Use the database configuration from a rails app, without using rails
84
+ email:
85
+ - cameronmartin123@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/rails_db_config_resolver.rb
96
+ - lib/rails_db_config_resolver/database_config.rb
97
+ - lib/rails_db_config_resolver/version.rb
98
+ - rails_db_config_resolver.gemspec
99
+ - spec/database_config_spec.rb
100
+ - spec/rails_db_config_resolver_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/support/database.yml
103
+ homepage: https://github.com/cameron-martin/rails_db_config_resolver
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rails db configuration, outside of rails
127
+ test_files:
128
+ - spec/database_config_spec.rb
129
+ - spec/rails_db_config_resolver_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/database.yml