Class: Oaf::HTTP::Handler
- Inherits:
-
WEBrick::HTTPServlet::AbstractServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- Oaf::HTTP::Handler
- Defined in:
- lib/oaf/http/handler.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) ⇒ Handler
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) ⇒ Handler
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
44 45 46 47 |
# File 'lib/oaf/http/handler.rb', line 44 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
93 94 95 |
# File 'lib/oaf/http/handler.rb', line 93 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
58 59 60 61 62 63 64 65 66 |
# File 'lib/oaf/http/handler.rb', line 58 def process_request req, res req_headers = req.header req_query = req.query req_body = Oaf::HTTP::Server.get_request_body req file = Oaf::Util.get_request_file @path, req.path, req.request_method out = Oaf::Util.get_output file, req_headers, req_body, req_query res_headers, res_status, res_body = Oaf::HTTP::Server.parse_response out Oaf::HTTP::Server.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.
79 80 81 |
# File 'lib/oaf/http/handler.rb', line 79 def respond_to? method method.to_s =~ /^do_[A-Z]+$/ ? true : super end |