Class: Ramaze::Adapter::Base

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

Overview

This class is holding common behaviour for its subclasses.

Direct Known Subclasses

Cgi, Ebb, Fake, Fcgi, Lsws, Mongrel, Scgi, Thin, WEBrick

Class Method Summary collapse

Class Method Details

.before(&block) ⇒ Object

Helper to assign a new block to before_call Usage:

Ramaze::Adapter.before do |env|
  if env['PATH_INFO'] =~ /suerpfast/
    [200, {'Content-Type' => 'text/plain'}, ['super fast!']]
  end
end


43
44
45
46
# File 'lib/ramaze/adapter/base.rb', line 43

def before(&block)
  @before = block if block
  @before
end

.before_call(env) ⇒ Object

Tries to find the block assigned by #before and calls it, logs and raises again any errors encountered during this process.



51
52
53
54
55
56
57
58
59
60
# File 'lib/ramaze/adapter/base.rb', line 51

def before_call env
  if before
    begin
      before.call(env)
    rescue Object => e
      Ramaze::Log.error e
      raise e
    end
  end
end

.call(env) ⇒ Object

This is called by Rack with the usual env, subsequently calls ::respond with it.

The method itself acts just as a wrapper for benchmarking and then calls .finish on the current response after ::respond has finished.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ramaze/adapter/base.rb', line 68

def call(env)
  if returned = before_call(env)
    returned
  elsif Global.benchmarking
    require 'benchmark'
    time = Benchmark.measure{ returned = respond(env) }
    Log.debug('request took %.5fs [~%.0f r/s]' % [time.real, 1.0/time.real])
    returned
  else
    respond(env)
  end
end

.middleware_respond(env) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ramaze/adapter/base.rb', line 93

def middleware_respond(env)
  if Global.middleware
    MIDDLEWARE.inject{|app, middleware| middleware.new(app) }.call(env)
  else
    Current.call(env)
  end
end

.respond(env) ⇒ Object

Initializes Request with env and an empty Response. Records the request into Ramaze::Record if Global.record is true. Then goes on and calls Dispatcher::handle with request and response.



85
86
87
88
89
90
91
# File 'lib/ramaze/adapter/base.rb', line 85

def respond(env)
  if Global.server == Thread.current
    Thread.new{ middleware_respond(env) }.value
  else
    middleware_respond(env)
  end
end

.start(host, port) ⇒ Object

For the specified host and for all given ports call run_server and add the returned thread to the Global.adapters ThreadGroup. Afterwards adds a trap for the value of Global.shutdown_trap which calls Ramaze.shutdown when triggered (usually by SIGINT).



24
25
26
27
# File 'lib/ramaze/adapter/base.rb', line 24

def start host, port
  Global.server = run_server(host, port)
  trap(Global.shutdown_trap){ Ramaze.shutdown }
end

.stopObject

Does nothing



31
32
33
# File 'lib/ramaze/adapter/base.rb', line 31

def stop
  Log.debug("Stopping #{self.class}")
end