Class: Waves::Dispatchers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatchers/base.rb

Overview

The Base dispatcher simply makes it easier to write dispatchers by inheriting from it. It creates a Waves request, ensures the request processing is done within a mutex, benchmarks the request processing, logs it, and handles common exceptions and redirects. Derived classes need only process the request within their safe method, which takes a Waves::Request and returns a Waves::Response.

Direct Known Subclasses

Default

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Like any Rack application, Waves’ dispatchers must provide a call method taking an env parameter.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dispatchers/base.rb', line 25

def call( env )
  Waves::Application.instance.synchronize do
    request = Waves::Request.new( env )
    response = request.response
    t = Benchmark.realtime do
      begin
        safe( request )
      rescue Dispatchers::Redirect => redirect
        response.status = redirect.status
        response.location = redirect.path
      end
    end
    Waves::Logger.info "#{request.method}: #{request.url} handled in #{(t*1000).round} ms."
    response.finish
  end
end

#deferred?(env) ⇒ Boolean

Called by event driven servers like thin and ebb. Return true if the server should run the request in a separate thread.

Returns:

  • (Boolean)


44
45
46
# File 'lib/dispatchers/base.rb', line 44

def deferred?( env )
  Waves::Application.instance.mapping.threaded?( env )
end