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.

350 CHAPTER 13 Designing an API<br />

critical role in an API, providing key information such as if the request was successful<br />

(the 200 status code) or if the user is unauthorized (the 401 status code) to perform<br />

the request. These are standardized ways of quickly informing people of the result of<br />

their requests.<br />

TIP There’s a handy gem called cheat that provides cheat sheets for a<br />

number of things, including one for HTTP status codes. You can install<br />

this gem using the gem install cheat command and then bring up the<br />

cheat sheet for status codes using cheat status_codes.<br />

But if you’re on Windows, this won’t work because Cheat requires a<br />

function not found on your system. Instead, go to http://cheat<br />

.errtheblog .com/b where you can view the list of all the cheat sheets.<br />

To begin writing this API, you’ll need to define the routes to it. Without routes, making<br />

requests to /api/v1/projects.json will forever be fruitless. If you recall from this chapter’s<br />

introduction, the API URL that you’ll be using looks like /api/v1/projects.json. Previously,<br />

when you wanted URLs to be prefixed with a name (such as back in chapter 7),<br />

you used a namespace method for them. You’re going to do the same thing here,<br />

except you’ll use a namespace within another namespace. Let’s open config/routes.rb<br />

and add the code from the following listing to the top of the routes definition.<br />

Listing 13.1 config/routes.rb<br />

Ticketee::Application.routes.draw do<br />

namespace :api do<br />

namespace :v1 do<br />

resources :projects<br />

end<br />

end<br />

...<br />

This new route defines routes and routing helpers for the projects resources, such as<br />

/api/v1/projects, and api_v1_projects_path respectively. You’re going to need to<br />

be serving content from this route, namely a list of projects. This list will be served in<br />

one of two forms: XML or JSON. Before you actually implement the code that makes<br />

these responses get served, you’re going to write a new RSpec test that makes sure<br />

these routes return the right data. To help you along, you’ll be using a feature provided<br />

by one of the dependencies of Rails: the rack-test gem.<br />

This gem provides a module called Rack::Test::Methods, which contains methods<br />

such as get, post, put, and delete. Look familiar? They should. They’re the four<br />

basic HTTP methods that you use when making requests. The methods from<br />

Rack::Test::Methods take a path on which to make a request and then return a Rack<br />

response (an Array) that consists of three parts: the HTTP status code, the HTTP headers<br />

(in Hash form), and the body. The simplest Rack response would look something<br />

like this:<br />

[200, {}, "Hello World!"]

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

Saved successfully!

Ooh no, something went wrong!