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.

Mounting a Rack application with Rails<br />

Now remember, to make requests to the<br />

Heartbeat::Application class you must<br />

prefix them with /test; otherwise they’ll be<br />

served by Heartbeat::TestApplication.<br />

Keeping that in mind, let’s make a request<br />

to http://localhost:9292/test/200. You’ll see<br />

something unusual: the path displayed on Figure 18.2 Success or FAILURE?!<br />

the page isn’t /test/200 as you may expect,<br />

but rather it’s /200. The env["PATH_INFO"]key doesn’t need to contain the path<br />

where your application is mounted, as that’s not important for routing requests within<br />

the application itself.<br />

If you make a request to another path not beginning with the /test prefix (such as<br />

http://localhost:9292/foo/bar), you’ll see the two buttons in forms provided by the<br />

Heartbeat::TestApplication, as shown in figure 18.2.<br />

When you click the Success! button, you’ll send a request to the/test/200 path,<br />

which will be served by the Heartbeat::Application class and will respond with a<br />

body that says /200 == Success!. When you click the back button in your browser<br />

and click the Failure! button, you’ll see the /500 == Failure!.<br />

This is the basic foundation for Rack applications and a lightweight demonstration<br />

of how routing in Rack applications works. When you began, you were able to write<br />

run Heartbeat::Application to run a single class as your Rack application, but as it’s<br />

grown more complex you’ve split different pieces of the functionality out into different<br />

classes. To combine these classes into one super-application you used the<br />

Rack::Builder.app method.<br />

Now you should have a basic understanding of how you can build Rack applications<br />

to have a lightweight way of creating dynamic responses. So how does all of this<br />

apply to Rails? Well, in Rails you’re able to mount a Rack application so that it can<br />

serve requests on a path (like you did with Rack::Builder), rather than having the<br />

request go through the entire Rails stack.<br />

18.3 Mounting a Rack application with Rails<br />

Sometimes, you’ll want to serve requests in a lightning-fast fashion. Rails is great for<br />

serving super-dynamic requests quickly, but occasionally you’ll want to forego the<br />

heaviness of the Rails controller stack and have a piece of code that receives a request<br />

and quickly responds.<br />

Previously, your Rack application had done just that. However, when you mount<br />

your Rack application inside of a Rails application, you’re able to use the classes (that<br />

is, models) from within the Rails application. With these models, you can do any number<br />

of things. For example, you can build a re-implementation of your tickets API,<br />

which will allow you to see an alternate way to craft the API you created in chapter 13.<br />

So let’s do this.<br />

525

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

Saved successfully!

Ooh no, something went wrong!