Sha256: 87cc4d7a5bfa859ccede8ff2c8debf47a6524876c0014e4eb8fd0b47010bdd58

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require "uri"

class ConnectionUrlResolver
  class URIDecorator
    def initialize(url)
      @uri = parser.parse(url)
    end

    def to_hash
      connection_hash
    end

    private

      attr_reader :uri

      def parser
        @_parser ||= URI::Parser.new
      end

      def connection_hash
        unescape_values(connection_hash_with_values)
      end

      def connection_hash_with_values
        query_hash.merge(
          "adapter" => adapter,
          "username" => user,
          "password" => password,
          "port" => port,
          "database" => database,
          "host" => host
        ).reject { |_key, value| value.nil? || value.empty? }
      end

      def query_hash
        query.split("&").map { |pair| pair.split("=") }.to_h
      end

      def query
        uri.query || ""
      end

      def adapter
        @_adapter ||= begin
          adapter = uri.scheme && uri.scheme.tr("-", "_")

          if adapter == "postgres"
            "postgresql"
          else
            adapter
          end
        end
      end

      def user
        uri.user
      end

      def password
        uri.password
      end

      def port
        uri.port.to_s
      end

      def database
        if adapter == "sqlite3"
          uri.path
        else
          uri.path.sub %r(^/), ""
        end
      end

      def host
        uri.hostname
      end

      def unescape_values(hash)
        hash.transform_values { |value| unescape(value) }
      end

      def unescape(value)
        if value.is_a?(String)
          parser.unescape(value)
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
connection_url_resolver-1.0.0 lib/connection_url_resolver/uri_decorator.rb