27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

524 CHAPTER 18 Rack-based applications<br />

A keen eye may have noticed that you’ve nested the paths to the heartbeat responses<br />

underneath a path called test. This is because when you build your combined class<br />

application, you’ll make your Heartbeat::Application sit under the /test route.<br />

When do you do this? Right now!<br />

18.2.2 Running a combined Rack application<br />

You’re now going to change the lib/heartbeat/config.ru file to now create a Rack application<br />

that uses both of your classes for different functionality. For this, you’re going to<br />

use the Rack::Builder class, which lets you build Rack applications from different<br />

parts. Let’s fill lib/heartbeat/config.ru with the content shown in the following listing.<br />

Listing 18.5 lib/heartbeat/config.ru<br />

heartbeat_root = File.expand_path(File.dirname(__FILE__))<br />

require heartbeat_root + '/application'<br />

require heartbeat_root + '/test_application'<br />

app = Rack::Builder.app do<br />

map '/test' do<br />

run Heartbeat::Application<br />

end<br />

map '/' do<br />

run Heartbeat::TestApplication<br />

end<br />

end<br />

run app<br />

Rather than calling run Heartbeat::Application here, you’re compiling a multifaceted<br />

Rack application using Rack::Builder.app. The run method you’ve been<br />

using all this time is defined inside the Rack::Builder class, actually. A *.ru file is usually<br />

evaluated within the instance of a Rack::Builder object by the code the rackup<br />

command uses, and so you are able to use the run method without having to call<br />

Rack::Builder.new before it or wrapping .ru code in a Rack::Builder.app block.<br />

This time, you’re being implicit and building a new Rack::Builder instance using<br />

Rack::Builder.app. Inside this instance, you’ll declare two routes using the map<br />

method. Within a block given to each of your map calls, you’re calling the run method<br />

again, passing it one of your two application classes.<br />

When a request comes into this application beginning with the path /test, it will be<br />

served by the Heartbeat::Application class. All other requests will be served by the<br />

Heartbeat::TestApplication class. This is not unlike the way certain requests in<br />

your Rails application beginning with /tickets are routed to the TicketsController<br />

and others beginning with /projects go to ProjectsController. 2<br />

Let’s start this application and see what it can do by running this command:<br />

rackup lib/heartbeat/config.ru<br />

2 In fact, the similarities are astounding.

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

Saved successfully!

Ooh no, something went wrong!