Class: Wee::WEBrickAdaptor

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/wee/adaptors/webrick.rb

Overview

Example of usage:

require 'wee/adaptors/webrick'
s = WEBrick::HTTPServer.new(:Port => 2000) 
s.mount '/app', Wee::WEBrickAdaptor, '/app', application

trap("INT") { s.shutdown; exit }
s.start

Or when using the convenience methods:

require 'wee/adaptors/webrick'
Wee::WEBrickAdaptor.
  request_class(Wee::Request).      # this is the default!
  register('/app' => application).
  register('/cnt' => application2).
  mount('/', WEBrick::HTTPServlet::FileHandler, '.').
  start(:Port => 2000)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, mount_path, application, request_class = Wee::Request) ⇒ WEBrickAdaptor

Returns a new instance of WEBrickAdaptor.



69
70
71
72
73
74
# File 'lib/wee/adaptors/webrick.rb', line 69

def initialize(server, mount_path, application, request_class=Wee::Request)
  super(server)
  @mount_path = mount_path
  @application = application
  @request_class = request_class || Wee::Request
end

Class Method Details

.mount(*args, &block) ⇒ Object

Convenience method



63
64
65
66
67
# File 'lib/wee/adaptors/webrick.rb', line 63

def self.mount(*args, &block)
  @mounts ||= []
  @mounts << [args, block]
  self
end

.register(hash) ⇒ Object

Convenience method



54
55
56
57
58
59
60
# File 'lib/wee/adaptors/webrick.rb', line 54

def self.register(hash)
  @apps ||= []
  hash.each do |path, application|
    @apps << [path, application, @request_class]
  end
  self
end

.request_class(request_class) ⇒ Object

Sets the used request_class for all following calls to #register



48
49
50
51
# File 'lib/wee/adaptors/webrick.rb', line 48

def self.request_class(request_class)
  @request_class = request_class
  self
end

.start(options = {}) {|server| ... } ⇒ Object

Convenience method

Yields:

  • (server)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wee/adaptors/webrick.rb', line 28

def self.start(options={})
  server = WEBrick::HTTPServer.new({:Port => 2000}.update(options))
  trap("INT") { server.shutdown }

  (@apps||[]).each do |path, app, request_class|
    server.mount(path, self, path, app, request_class)
  end

  (@mounts||[]).each do |args, block|
    server.mount(*args, &block)
  end

  yield server if block_given?

  server.start
  server
end

Instance Method Details

#handle_request(req, res) ⇒ Object Also known as: do_GET, do_POST



76
77
78
79
80
81
82
83
84
85
# File 'lib/wee/adaptors/webrick.rb', line 76

def handle_request(req, res)
  context = Wee::Context.new(@request_class.new(@mount_path, req.path, req.header, req.query, req.cookies))
  @application.handle_request(context)
  res.status = context.response.status
  res.body = context.response.content
  context.response.header.each { |k,v| res.header[k] = v }
  if context.response.cookies?
    context.response.cookies.each {|c| res.cookies << c } 
  end
end