Module: Rack::Handler
- Defined in:
- lib/rack/handler.rb,
lib/rack/handler/webrick.rb
Overview
Handlers connect web servers with Rack.
Handlers usually are activated by calling MyHandler.run(myapp). A second optional hash can be passed to include server-specific configuration.
Defined Under Namespace
Classes: WEBrick
Class Method Summary collapse
- .default(options = {}) ⇒ Object
- .get(server) ⇒ Object
- .register(server, klass) ⇒ Object
-
.try_require(prefix, const_name) ⇒ Object
Transforms server-name constants to their canonical form as filenames, then tries to require them but silences the LoadError if not found.
Class Method Details
.default(options = {}) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/rack/handler.rb', line 27 def self.default( = {}) # Guess. if ENV.include?("RACK_HANDLER") get(ENV["RACK_HANDLER"]) else "webrick" end end |
.get(server) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rack/handler.rb', line 9 def self.get(server) return unless server server = server.to_s unless @handlers.include? server load_error = try_require("rack/handler", server) end if klass = @handlers[server] klass.split("::").inject(Object) { |o, x| o.const_get(x) } else const_get(server) end rescue NameError => name_error raise load_error || name_error end |
.register(server, klass) ⇒ Object
57 58 59 60 |
# File 'lib/rack/handler.rb', line 57 def self.register(server, klass) @handlers ||= {} @handlers[server.to_s] = klass.to_s end |
.try_require(prefix, const_name) ⇒ Object
Transforms server-name constants to their canonical form as filenames, then tries to require them but silences the LoadError if not found
Naming convention:
Foo # => 'foo'
FooBar # => 'foo_bar.rb'
FooBAR # => 'foobar.rb'
# => 'foobar.rb'
FOOBAR # => 'foobar.rb'
FooBarBaz # => 'foo_bar_baz.rb'
47 48 49 50 51 52 53 54 55 |
# File 'lib/rack/handler.rb', line 47 def self.try_require(prefix, const_name) file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }. gsub(/[A-Z]+[^A-Z]/, '_\&').downcase require(::File.join(prefix, file)) nil rescue LoadError => error error end |