Rails isn't magic, but sometimes it feels like it. To get a better understanding of Rails' MVC web framework, I built a "lite" version of Rails from scratch.
Below are some noteworthy facets of this project:
-
WEBRick Server: server.rb The server utilizes Ruby's WEBRick library to provide the interface for receiving HTTP requests and sending responses.
-
ControllerBase: controller_base.rb
ControllerBase
takes inHTTPRequest
objects to fill out anHTTPResponse
object. It sets thecontent_type
andbody
of the response and returns the response with rendered content. -
Session session.rb
Session
allows us to set and storeHTTPCookie
s for the user's session. -
Params params.rb
Params
allows us to receive and parse params from either the router's match of the URL, the query string, or the request body. -
Router router.rb
Router
keeps track of multiple routes, including the matched URL patterns, the HTTP method (GET, POST, PUT, DELETE), the controller class the route maps to, and the action to be invoked.