class Turboquery::DatabaseUrl

Public Class Methods

new(url = nil) click to toggle source
# File lib/turboquery/database_url.rb, line 5
def initialize(url = nil)
  @uri = URI.parse(url || ENV['DATABASE_URL'])
  @query = CGI.parse @uri.query.to_s
end

Public Instance Methods

to_hash() click to toggle source
# File lib/turboquery/database_url.rb, line 10
def to_hash
  build_hash
  @hash
end

Private Instance Methods

build_hash() click to toggle source
# File lib/turboquery/database_url.rb, line 17
def build_hash
  @hash = {
    adapter: @uri.scheme,
    host: @uri.host,
    database: File.basename(@uri.path)
  }

  set_optional :port, @uri.port
  set_optional :username, @uri.user
  set_optional :password, @uri.password
  set_optional :encoding, encoding
  set_optional :pool, pool
end
encoding() click to toggle source
# File lib/turboquery/database_url.rb, line 35
def encoding
  @query.key?('encoding') ? @query['encoding'][0] : nil
end
pool() click to toggle source
# File lib/turboquery/database_url.rb, line 39
def pool
  pool = @query['pool'] || @query['max_connections']
  pool.length > 0 ? pool[0].to_i : nil
end
set_optional(key, val) click to toggle source
# File lib/turboquery/database_url.rb, line 31
def set_optional(key, val)
  @hash[key] = val if val
end