Sha256: c466db4a24e27791489483f66d90a380128052eadb9d0e6e663644b0da218f53

Contents?: true

Size: 683 Bytes

Versions: 3

Compression:

Stored size: 683 Bytes

Contents

require 'uri'
require 'rack/utils'

module Dossier
  class ConnectionUrl

    attr_reader :uri

    def initialize(url = nil)
      @uri = URI.parse(url || ENV.fetch('DATABASE_URL'))
    end

    def to_hash
      {
        adapter:  adapter,
        username: uri.user,
        password: uri.password,
        host:     uri.host,
        port:     uri.port,
        database: File.basename(uri.path)
      }.merge(params).reject { |k,v| v.nil? }
    end

    private

    def adapter
      uri.scheme == "postgres" ? "postgresql" : uri.scheme
    end

    def params
      return {} unless uri.query
      Rack::Utils.parse_nested_query(uri.query).symbolize_keys
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dossier-3.0.0.pre lib/dossier/connection_url.rb
dossier-2.13.1 lib/dossier/connection_url.rb
dossier-2.12.2 lib/dossier/connection_url.rb