Rack key concepts

Posted December 6, 2016

Taken by Derek Liang

The most important thing to know about Rack and the internet is knowing how the internet works. First you have HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, and PATCH requests. πŸ€” huh? Let’s run through them.

A GET request will get a resource. A HEAD requests is a GET request without the body of a GET request. A POST request will submit data. A PUT will upload data. A DELETE will, well, delete a resource. A TRACE request will return the recieved request. OPTIONS will return the HTTP methods that your server supports. CONNECT converts the request to a TCP/IP tunnel, ususally used for SSL. A PATCH request will update do a partial modifications to an already existing resource.

You can see what type of requests a website is using by looking in the network tab in Dev Tools.

Now onto Rack itself. Rack is a Ruby gem that provides a minimal interface that goes between a server that supports Ruby and Ruby Frameworks.

To use Rack you must first make a call to a class #call. All this needs to do is return a response that contains status codes, headers, and the body. That can be done with a Rack::Response object:

1# call.rb
2
3class Application
4 def call(env)
5 resp = Rack::Response.new
6 resp.write "Hello, World"
7 resp.finish
8 end
9end

That will run whenever a request is received but you still need to set up a HTTP serve to receive that request. Using config.ru

1# config.ru
2
3require_relative "./application.rb"
4run Application.new

Now running rackup config.ru will start up your application.