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.

522 CHAPTER 18 Rack-based applications<br />

You’ve learned the basics of how a Rack application works and gained an understanding<br />

that your Rails application is a bigger version of this little application you’ve<br />

written. There’s much more to Rack than providing this abstraction for the underlying<br />

request/response cycle. For example, you can build more complex apps with logic<br />

for one part of the application in one class and additional logic in another.<br />

One other feature of Rack is the ability to build applications by combining smaller<br />

applications into a larger one. You saw this with Rails when you used the mount<br />

method in your application’s config/routes.rb to mount the engine you developed in<br />

chapter 17. Let’s see how you can do this with Rack.<br />

18.2 Building bigger Rack applications<br />

Your basic Rack application quickly outgrew the lambda shell you placed it in, and so<br />

you moved the logic in it into a class and added some more. With the class, you’re able<br />

to define a call method on it, which then returns the response that Rack needs. The<br />

class allows you to cleanly write a more complex Rack application than a lambda would.<br />

So what happens now if you outgrow a class? Well, you can abstract the function of<br />

your application into multiple classes and build a Rack application using those classes.<br />

The structure is not unlike the controller structure you have in a Rails application,<br />

because it will have separate classes that are responsible for different things.<br />

In your new Rack application, you’ll have two classes that perform separate tasks,<br />

but are still running on the same instance of the server. The first class is going to be<br />

your Heartbeat::Application class, and the second one will provide two forms, each<br />

with one button: one for success and one for failure. These forms will then submit to<br />

the actions provided within the Heartbeat::Application class, which will demonstrate<br />

how you can get your classes to talk to each other.<br />

18.2.1 You’re breaking up<br />

Now that your Rack application is getting more complex, you’re going to break it out<br />

into three files. The first file will be the Heartbeat::Application class, the second<br />

will be a new class called Heartbeat::TestApplication, and the third will be the<br />

Rackup file that will be responsible for combining these two classes into one glorious<br />

application.<br />

Let’s begin by separating out your application and the Rackup file into two separate<br />

files. In a new directory at lib/heartbeat.rb, add the code shown in the following<br />

listing to lib/heartbeat/application.rb.<br />

Listing 18.2 lib/heartbeat/application.rb<br />

module Heartbeat<br />

class Application<br />

def self.call(env)<br />

default_headers = { "Content-Type" => "text/plain"}<br />

if env["PATH_INFO"] =~ /200/<br />

body = "Success!"

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

Saved successfully!

Ooh no, something went wrong!