module DatabaseUrl

Constants

ACTIVE_RECORD_FIELD_MAP
DEFAULT_HOST
SEQUEL_FIELD_MAP
VERSION

Public Class Methods

to_active_record_hash(url = nil) click to toggle source
# File lib/database_url.rb, line 8
def to_active_record_hash(url = nil)
  result_hash = to_hash ACTIVE_RECORD_FIELD_MAP, url

  if result_hash[:adapter] == 'mysql2'
    result_hash[:username] = result_hash.delete(:user)
  end

  result_hash
end
to_active_record_url(hash) click to toggle source
# File lib/database_url.rb, line 18
def to_active_record_url(hash)
  to_url ACTIVE_RECORD_FIELD_MAP, hash
end
to_sequel_hash(url = nil) click to toggle source
# File lib/database_url.rb, line 22
def to_sequel_hash(url = nil)
  to_hash SEQUEL_FIELD_MAP, url
end
to_sequel_url(hash) click to toggle source
# File lib/database_url.rb, line 26
def to_sequel_url(hash)
  to_url SEQUEL_FIELD_MAP, hash
end

Private Class Methods

to_hash(field_map, url = nil) click to toggle source
# File lib/database_url.rb, line 32
def to_hash(field_map, url = nil)
  if url.nil?
    url = ENV.fetch 'DATABASE_URL'
  end
  uri = URI.parse url
  memo = {
    adapter: uri.scheme,
    host: uri.host,
    database: File.basename(uri.path),
  }
  if uri.port
    memo[:port] = uri.port
  end
  if uri.user
    memo[:user] = uri.user
  end
  if uri.password
    memo[:password] = uri.password
  end
  query = CGI.parse uri.query.to_s
  if query.has_key?('encoding')
    memo[:encoding] = query['encoding'][0]
  end
  if (pool = query['pool'] || query['max_connections']) and pool.length > 0 # CGI.parse creates a Hash that defaults to []
    memo[field_map.fetch('pool').to_sym] = pool[0].to_i
  end
  memo
end
to_url(field_map, hash) click to toggle source
# File lib/database_url.rb, line 61
def to_url(field_map, hash)
  # stringify keys
  c = hash.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo }
  userinfo = if c.has_key?('user') or c.has_key?('username') or c.has_key?('password')
    username = c.values_at('user', 'username').compact.first
    [ username, c['password'] ].join ':'
  end
  query = {}
  if c.has_key?('encoding')
    query['encoding'] = c['encoding']
  end
  if pool = c['pool'] || c['max_connections']
    query[field_map.fetch('pool')] = pool.to_i
  end
  query = if query.length > 0
    URI.encode_www_form query
  end

  # URI.new(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false)
  uri = URI::Generic.new(
    c.fetch('adapter'),
    userinfo,
    c.fetch('host', DEFAULT_HOST),
    c['port'],
    nil, # registry
    "/#{c.fetch('database')}", # path
    nil, # opaque
    query, # query
    nil, # fragment
  )
  uri.to_s
end