database_url 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.
@@ -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
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
@@ -0,0 +1,3 @@
1
+ 0.0.1 / 2013-09-19
2
+
3
+ Initial release!
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in database_url.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Seamus Abshere
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.
@@ -0,0 +1,32 @@
1
+ # DatabaseUrl
2
+
3
+ Convert back and forth between Heroku-style `ENV['DATABASE_URL']` and Rails/ActiveRecord-style `config/database.yml` config hashes.
4
+
5
+ ## Usage
6
+
7
+ ### Get `DATABASE_URL` from config hash
8
+
9
+ You pass it a hash (string or symbol keys, doesn't matter) and it gives you a URL:
10
+
11
+ >> c = YAML.load_file('/path/to/config/database.yml')
12
+ => {"development"=>{"adapter"=>"mysql2", "database"=>"myapp_development", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "test"=>{"adapter"=>"mysql2", "database"=>"myapp_test", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}, "production"=>{"adapter"=>"mysql2", "database"=>"myapp", "username"=>"myapp", "password"=>"XXX", "encoding"=>"utf8"}, "cucumber"=>{"adapter"=>"mysql2", "database"=>"myapp_cucumber", "username"=>"root", "password"=>"password", "encoding"=>"utf8"}}
13
+ >> DatabaseUrl.database_url c['development']
14
+ => "mysql2://root:password@127.0.0.1/myapp_development?encoding=utf8"
15
+
16
+ ### Get config hash from `DATABASE_URL`
17
+
18
+ Let's assume `DATABASE_URL` is set to `postgres://uuu:xxx@127.0.0.1:1234/abc`...
19
+
20
+ >> DatabaseUrl.active_record_config
21
+ => {:adapter=>"postgres", :host=>"127.0.0.1", :port=>1234, :database=>"abc", :user=>"uuu", :password=>"xxx"}
22
+
23
+ You can also pass a URL as the first argument to `DatabaseUrl.active_record_config`.
24
+
25
+ ## Query parameters supported
26
+
27
+ * `encoding`
28
+ * `pool`
29
+
30
+ ## Copyright
31
+
32
+ Copyright 2013 Seamus Abshere
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ 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 'database_url/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "database_url"
8
+ spec.version = DatabaseUrl::VERSION
9
+ spec.authors = ["Seamus Abshere"]
10
+ spec.email = ["seamus@abshere.net"]
11
+ spec.description = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style config/database.yml hashes.}
12
+ spec.summary = %q{Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style config/database.yml hashes.}
13
+ spec.homepage = "https://github.com/seamusabshere/database_url"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,72 @@
1
+ require "database_url/version"
2
+
3
+ require 'uri'
4
+ require 'cgi'
5
+
6
+ module DatabaseUrl
7
+ class << self
8
+ def active_record_config(url = nil)
9
+ if url.nil?
10
+ url = ENV.fetch 'DATABASE_URL'
11
+ end
12
+ uri = URI.parse url
13
+ retval = {
14
+ adapter: uri.scheme,
15
+ host: uri.host,
16
+ database: File.basename(uri.path),
17
+ }
18
+ if uri.port
19
+ retval[:port] = uri.port
20
+ end
21
+ if uri.user
22
+ retval[:user] = uri.user
23
+ end
24
+ if uri.password
25
+ retval[:password] = uri.password
26
+ end
27
+ query = CGI.parse uri.query
28
+ if query.has_key?('encoding')
29
+ retval[:encoding] = query['encoding'][0]
30
+ end
31
+ if query.has_key?('pool')
32
+ retval[:pool] = query['pool'][0].to_i
33
+ end
34
+ retval
35
+ end
36
+
37
+ def database_url(active_record_config)
38
+ # stringify keys
39
+ c = active_record_config.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo }
40
+ userinfo = if c.has_key?('user') or c.has_key?('username') or c.has_key?('password')
41
+ username = c.values_at('user', 'username').compact.first
42
+ [ username, c['password'] ].join ':'
43
+ end
44
+ query = {}
45
+ if c.has_key?('encoding')
46
+ query['encoding'] = c['encoding']
47
+ end
48
+ if c.has_key?('pool')
49
+ query['pool'] = c['pool'].to_i
50
+ end
51
+ query = if query.length > 0
52
+ URI.encode_www_form query
53
+ end
54
+
55
+ # URI.new(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false)
56
+ uri = URI::Generic.new(
57
+ c.fetch('adapter'),
58
+ userinfo,
59
+ c.fetch('host', DEFAULT_HOST),
60
+ c['port'],
61
+ nil, # registry
62
+ "/#{c.fetch('database')}", # path
63
+ nil, # opaque
64
+ query, # query
65
+ nil, # fragment
66
+ )
67
+ uri.to_s
68
+ end
69
+ end
70
+
71
+ DEFAULT_HOST = '127.0.0.1'
72
+ end
@@ -0,0 +1,3 @@
1
+ module DatabaseUrl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatabaseUrl do
4
+ before do
5
+ ENV.delete 'DATABASE_URL'
6
+ end
7
+
8
+ it "should build Rails/ActiveRecord-style config hashes using ENV['DATABASE_URL']" do
9
+ ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc?encoding=latin1&pool=9"
10
+ DatabaseUrl.active_record_config.should == {
11
+ adapter: 'postgres',
12
+ host: '127.0.0.1',
13
+ port: 1234,
14
+ database: 'abc',
15
+ user: 'uuu',
16
+ password: 'xxx',
17
+ encoding: 'latin1',
18
+ pool: 9,
19
+ }
20
+ end
21
+
22
+ it "should build DATABASE_URL-style URL from Rails/ActiveRecord-style config hash" do
23
+ DatabaseUrl.database_url({
24
+ adapter: 'postgreq',
25
+ host: '127.0.0.2',
26
+ port: 1235,
27
+ database: 'abd',
28
+ user: 'uu1',
29
+ password: 'xx1',
30
+ encoding: 'utf8',
31
+ pool: 15,
32
+ }).should == "postgreq://uu1:xx1@127.0.0.2:1235/abd?encoding=utf8&pool=15"
33
+ end
34
+
35
+ it "should build Rails/ActiveRecord-style config hashes given a URL" do
36
+ ENV['DATABASE_URL'] = "postgres://uuu:xxx@127.0.0.1:1234/abc" # should be ignored!
37
+ DatabaseUrl.active_record_config("postgres://uu2:xxx@127.0.0.3:1236/abc?encoding=latin1").should == {
38
+ adapter: 'postgres',
39
+ host: '127.0.0.3',
40
+ port: 1236,
41
+ database: 'abc',
42
+ user: 'uu2',
43
+ password: 'xxx',
44
+ encoding: 'latin1',
45
+ }
46
+ end
47
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'database_url'
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Seamus Abshere
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style
63
+ config/database.yml hashes.
64
+ email:
65
+ - seamus@abshere.net
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - .travis.yml
73
+ - CHANGELOG
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - database_url.gemspec
79
+ - lib/database_url.rb
80
+ - lib/database_url/version.rb
81
+ - spec/database_url_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: https://github.com/seamusabshere/database_url
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Convert back and forth between Heroku-style ENV['DATABASE_URL'] and Rails/ActiveRecord-style
108
+ config/database.yml hashes.
109
+ test_files:
110
+ - spec/database_url_spec.rb
111
+ - spec/spec_helper.rb
112
+ has_rdoc: