class Vanity::Connection::Specification

Public Class Methods

new(spec) click to toggle source
# File lib/vanity/connection.rb, line 66
def initialize(spec)
  case spec
  when String
    @spec = build_specification_hash_from_url(spec)
  when Hash
    if spec[:redis]
      @spec = {
        adapter: :redis,
        redis: spec[:redis]
      }
    else
      @spec = spec
    end
  else
    raise InvalidSpecification.new("Unsupported connection specification: #{spec.inspect}")
  end

  validate_specification_hash(@spec)
end

Public Instance Methods

to_h() click to toggle source
# File lib/vanity/connection.rb, line 86
def to_h
  @spec
end

Private Instance Methods

build_specification_hash_from_url(connection_url) click to toggle source
# File lib/vanity/connection.rb, line 97
def build_specification_hash_from_url(connection_url)
  uri = URI.parse(connection_url)
  params = CGI.parse(uri.query) if uri.query
  {
    adapter: uri.scheme,
    username: uri.user,
    password: uri.password,
    host: uri.host,
    port: uri.port,
    path: uri.path,
    params: params
  }
end
validate_specification_hash(spec) click to toggle source
# File lib/vanity/connection.rb, line 92
def validate_specification_hash(spec)
  all_symbol_keys = spec.keys.all? { |key| key.is_a?(Symbol) }
  raise InvalidSpecification unless all_symbol_keys
end