class Oaf::HTTPHandler

Provides all required handlers to WEBrick for serving all basic HTTP methods. WEBrick handles GET, POST, HEAD, and OPTIONS out of the box, but to mock most RESTful applications we are going to want PUT and DELETE undoubtedly.

Public Class Methods

new(server, options) click to toggle source

Creates a new abstract server object and allows passing in the root path of the server via an argument.

Parameters:

server

A WEBrick::HTTPServer object

options

A hash of Oaf configuration options

Calls superclass method
# File lib/oaf/httphandler.rb, line 28
def initialize server, options
  super server
  @options = options
end

Public Instance Methods

method_missing(method, *opt) click to toggle source

A magic method to handle any and all do_* methods. This allows Oaf to claim some degree of support for any HTTP method, be it a known and commonly used method such as PUT or DELETE, or custom methods.

Parameters:

method

The name of the method being called

*opt

A list of arguments to pass along to the processing method

Calls superclass method
# File lib/oaf/httphandler.rb, line 79
def method_missing method, *opt
  method.to_s =~ /^do_[A-Z]+$/ ? process_request(*opt) : super
end
process_request(req, res) click to toggle source

Main server method. Oaf does not really differentiate between different HTTP methods, but needs to at least support passing them all.

Parameters:

req

A WEBrick::HTTPRequest object

res

A WEBrick::HTTPResponse object

# File lib/oaf/httphandler.rb, line 42
def process_request req, res
  req_headers = req.header
  req_query = req.query
  req_body = Oaf::HTTPServer.get_request_body req
  file = Oaf::Util.get_request_file(@options[:path], req.path,
    req.request_method, @options[:default_response])
  out = Oaf::Util.get_output(file, req.path, req_headers, req_body,
    req_query)
  res_headers, res_status, res_body = Oaf::HTTPServer.parse_response out
  Oaf::HTTPServer.set_response! res, res_headers, res_body, res_status
end
respond_to?(method) click to toggle source

A magic respond_to? implementation to trick WEBrick into thinking that any do_* methods are already defined. This allows method_missing to do its job once WEBrick makes its call to the method.

Parameters:

method

The name of the class method being checked

Returns:

Boolean, true if the method name matches do_+, else super.

Calls superclass method
# File lib/oaf/httphandler.rb, line 65
def respond_to? method
  method.to_s =~ /^do_[A-Z]+$/ ? true : super
end