Class: Makara::ConfigParser::ConnectionUrlResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/makara/config_parser.rb

Overview

ConnectionUrlResolver is borrowed from Rails 4-2 since its location and implementation vary slightly among Rails versions, but the behavior is the same. Thus, borrowing the class should be the most future-safe way to parse a database url.

Expands a connection string into a hash.

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ConnectionUrlResolver

Example

url = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000"
ConnectionUrlResolver.new(url).to_hash
# => {
  "adapter"  => "postgresql",
  "host"     => "localhost",
  "port"     => 9000,
  "database" => "foo_test",
  "username" => "foo",
  "password" => "bar",
  "pool"     => "5",
  "timeout"  => "3000"
}


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/makara/config_parser.rb', line 50

def initialize(url)
  raise "Database URL cannot be empty" if url.blank?
  @uri     = URI.parse(url)
  @adapter = @uri.scheme.tr('-', '_')
  @adapter = "postgresql" if @adapter == "postgres"

  if @uri.opaque
    @uri.opaque, @query = @uri.opaque.split('?', 2)
  else
    @query = @uri.query
  end
end

Instance Method Details

#to_hashObject

Converts the given URL to a full connection hash.



64
65
66
67
68
# File 'lib/makara/config_parser.rb', line 64

def to_hash
  config = raw_config.reject { |_,value| value.blank? }
  config.map { |key,value| config[key] = URI.unescape(value) if value.is_a? String }
  config
end