Class: AppEngine::Rack::DeferredDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-rack.rb

Overview

Split loading requests into 3 parts

deferred_dispatcher = AppEngine::Rack::DeferredDispatcher.new(

:require => File.expand_path('../config/environment', __FILE__),
:dispatch => 'ActionController::Dispatcher')

run deferred_dispatcher

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DeferredDispatcher

Returns a new instance of DeferredDispatcher.



36
37
38
# File 'lib/appengine-rack.rb', line 36

def initialize args
  @args = args
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appengine-rack.rb', line 40

def call env
  if @runtime.nil?
    @runtime = true
    # 1: redirect with runtime and jruby-rack loaded
    redirect_or_error(env)
  elsif @rack_app.nil?
    require @args[:require]
    @rack_app = Object.module_eval(@args[:dispatch]).new
    # 2: redirect with framework required & dispatched
    redirect_or_error(env)
  else
    # 3: process all other requests
    @rack_app.call(env)
  end
end

#redirect_or_error(env) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/appengine-rack.rb', line 56

def redirect_or_error(env)
  if env['REQUEST_METHOD'].eql?('GET')
    redir_url = env['REQUEST_URI'] +
        (env['QUERY_STRING'].eql?('') ? '?' : '&') + Time.now.to_i.to_s
    res = ::Rack::Response.new('*', 302)
    res['Location'] = redir_url
    res.finish
  else
    ::Rack::Response.new('Service Unavailable', 503).finish
  end
end