Class: Oaf::HTTPHandler
- Inherits:
-
WEBrick::HTTPServlet::AbstractServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- Oaf::HTTPHandler
- Defined in:
- lib/oaf/httphandler.rb
Overview
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.
Instance Method Summary collapse
-
#initialize(server, path) ⇒ HTTPHandler
constructor
Creates a new abstract server object and allows passing in the root path of the server via an argument.
-
#method_missing(method, *opt) ⇒ Object
A magic method to handle any and all do_* methods.
-
#process_request(req, res) ⇒ Object
Main server method.
-
#respond_to?(method) ⇒ Boolean
A magic respond_to? implementation to trick WEBrick into thinking that any do_* methods are already defined.
Constructor Details
#initialize(server, path) ⇒ HTTPHandler
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
- path
-
A string containing the root path
50 51 52 53 |
# File 'lib/oaf/httphandler.rb', line 50 def initialize server, path super server @path = path end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *opt) ⇒ Object
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
100 101 102 |
# File 'lib/oaf/httphandler.rb', line 100 def method_missing method, *opt method.to_s =~ /^do_[A-Z]+$/ ? process_request(*opt) : super end |
Instance Method Details
#process_request(req, res) ⇒ Object
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
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/oaf/httphandler.rb', line 64 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 @path, req.path, req.request_method 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) ⇒ Boolean
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.
86 87 88 |
# File 'lib/oaf/httphandler.rb', line 86 def respond_to? method method.to_s =~ /^do_[A-Z]+$/ ? true : super end |