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.

Building Rack applications<br />

it back up through the stack to Rack, and then Rack will pass<br />

it back to the server, which will finally pass it back to the<br />

browser. All of this happens in a lightning quick fashion.<br />

Separating Rack from Rails not only reduces bloat in the<br />

framework, but also provides a common interface that other<br />

frameworks can use. By standardizing the request/response<br />

cycle, applications that are built on top of Rack can interact<br />

with one another. In this chapter, you’ll see how you can do<br />

this by making your Rails application work with applications<br />

built using Rack, but not Rails.<br />

You’ll build some Rack applications in this chapter that<br />

aren’t Rails applications but will work just as seamlessly.<br />

You’ll learn how Rack provides the request/response cycle<br />

underneath Rails and other Ruby frameworks, and learn how<br />

to build your own small, lightweight Rack-based applications.<br />

With these lightweight applications crafted, you’ll then<br />

create one more application that will re-implement the tickets<br />

API functionality you first created in chapter 13, using<br />

another Rack-based web framework called Sinatra. You’ll<br />

then mount this Sinatra application inside your Rails appli-<br />

cation using methods that Rails provides. This will provide an apt example of how<br />

you’re able to interact with classes from your Rails application from within a mounted<br />

Rack application.<br />

Finally, we’ll take a look at middleware within both the Rack and Rails stacks, and<br />

you’ll learn how to use it to your advantage to manipulate requests coming into your<br />

application.<br />

All Rack-based applications work the same way. You request a URL from the application,<br />

and it sends back a response. But it’s what goes on between that request and<br />

the response that’s the most interesting part. Let’s create a basic Rack application now<br />

so that you can understand the basics.<br />

18.1 Building Rack applications<br />

Browser<br />

Web Server<br />

Rack standardizes the way an application receives requests across all the Ruby frameworks.<br />

With this standardization, you know that any application purporting to be a<br />

Rack application is going to have a standard way for you to send requests to it and a<br />

standard way of receiving responses.<br />

You’re going to build a basic Rack application so that you can learn about the<br />

underlying architecture for requests and responses found in Rails and other Ruby<br />

frameworks. With this knowledge, you’ll be able to build lightweight Rack applications<br />

that you can hook into your Rails stack, or even Rack middleware.<br />

When you’re content with the first application, you’ll create another and then<br />

make them work together as one big application. First things first, though.<br />

Rack<br />

Application Stack<br />

Middleware<br />

...<br />

Middleware<br />

Application<br />

517<br />

Figure 18.1 Application<br />

request through the stack

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

Saved successfully!

Ooh no, something went wrong!