27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

518 CHAPTER 18 Rack-based applications<br />

18.1.1 A basic Rack application<br />

To build a basic Rack application, you only need to have an object in Ruby that<br />

responds to the call method. That call method needs to take one argument (the<br />

request) and also needs to return a three-element Array object. This array represents<br />

the response that will be given back to Rack, and looks something like this:<br />

[200, { "Content-Type" => "text/plain"}, ["Hello World"]]<br />

The first element in this response array is the status code for your response. In this<br />

case, it’s 200, which represents a successful response. You had a bit of a play with status<br />

codes back in chapter 13 when you were building your API, so these should be no mystery<br />

at this point.<br />

The second element in this array are the headers that will be sent back. These<br />

headers are used by the browser to determine how to deal with the response. In this<br />

case, the response will be rendered as-is to the page because the Content-Type header<br />

is text/plain, indicating normal text with no formatting applied. Usually your Rack<br />

application would set this to text/html to indicate an HTML response.<br />

Finally, the third element represents the response body, which is sent back along<br />

with the status code and headers to Rack. Rack then compiles it all into an HTTP<br />

response, which is sent back to where the request came from.<br />

Let’s see this in action now. You’re going to create a light Rack application that<br />

responds with “Hello World” whenever it receives a request. This kind of application is<br />

often used to check and see if a server is still up and responding to HTTP calls. You’ll<br />

create a new file inside your Ticketee’s application’s lib called lib/heartbeat.ru (you’re<br />

checking the “heartbeat” of the server) and fill it with this content:<br />

run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }<br />

The .ru extension for this file represents a Rack configuration file, also known as a<br />

Rackup file. In it, you call the run method, which needs an object that responds to<br />

You already have a Rackup file<br />

Your Rails application also has one of these .ru files, called config.ru, which is used<br />

by Rack-based servers to run your application. You can see this in action by running<br />

the rackup config.ru command, which will start up your application using the<br />

config.ru file’s configuration.<br />

If you look in this file, you’ll see these lines:<br />

# This file is used by Rack-based servers to start the application.<br />

require ::File.expand_path('../config/environment', __FILE__)<br />

run Ticketee::Application<br />

The first line requires config/environment.rb for the application, which is responsible<br />

for setting up the environment of the application. Then it uses the run method—just<br />

as you are—except it’s passing Ticketee::Application, which actually responds<br />

to call.<br />

Cool stuff.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!