module Rackup::Handler

def self.[](name)

def self.[](name)
  name = name.to_sym
  begin
    @handlers[name] || self.const_get(name, false)
  rescue NameError
    # Ignore.
  end
end

def self.default

def self.default
  if rack_handler = ENV[RACKUP_HANDLER]
    self.get(rack_handler)
  elsif rack_handler = ENV[RACK_HANDLER]
    warn "RACK_HANDLER is deprecated, use RACKUP_HANDLER."
    self.get(rack_handler)
  else
    pick SERVER_NAMES
  end
end

def self.get(name)

def self.get(name)
  return nil unless name
  name = name.to_sym
  if server = self[name]
    return server
  end
  begin
    require_handler("rackup/handler", name)
  rescue LoadError
    require_handler("rack/handler", name)
  end
  return self[name]
end

def self.pick(server_names)

=> Rackup::Handler::WEBrick
> pick ['puma', 'webrick']

Raises `LoadError` if no handler was found.
Select first available Rack handler given an `Array` of server names.
def self.pick(server_names)
  server_names = Array(server_names)
  server_names.each do |server_name|
    begin
      server = self.get(server_name)
     return server if server
    rescue LoadError
      # Ignore.
    end
  end
  raise LoadError, "Couldn't find handler for: #{server_names.join(', ')}."
end

def self.register(name, klass)

Register a named handler class.
def self.register(name, klass)
  if klass.is_a?(String)
    warn "Calling Rackup::Handler.register with a string is deprecated, use the class/module itself.", uplevel: 1
    klass = self.const_get(klass, false)
  end
  name = name.to_sym
  @handlers[name] = klass
end

def self.require_handler(prefix, const_name)

FooBarBaz # => 'foo_bar_baz.rb'
FOOBAR # => 'foobar.rb'
FOObar # => 'foobar.rb'
FooBAR # => 'foobar.rb'
FooBar # => 'foo_bar.rb'
Foo # => 'foo'

Naming convention:

then tries to require them but silences the LoadError if not found
Transforms server-name constants to their canonical form as filenames,
def self.require_handler(prefix, const_name)
  file = const_name.to_s.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
    gsub(/[A-Z]+[^A-Z]/, '_\&').downcase
  require(::File.join(prefix, file))
end