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.

358 CHAPTER 13 Designing an API<br />

Booyah, it works! How about bin/rspec spec/api/v1/projects_spec.rb too?<br />

1 example, 0 failures<br />

All green there too, and so it’s definitely time to do a commit now. You should run the<br />

customary checks before you commit by running rake cucumber:ok spec:<br />

63 scenarios (63 passed)<br />

732 steps (732 passed)<br />

# and<br />

43 examples, 0 failures, 19 pending<br />

Great! Everything’s still green. From now on you will only run the spec tests, because<br />

all you are going to be changing is the API, which will not impact anything that the features<br />

test. At the end of the chapter, you’ll run it again to make sure that nothing is<br />

broken. Commit and push the changes that you’ve made:<br />

git add .<br />

git commit -m "Implemented token-based authentication API base"<br />

git push<br />

You’ve begun to implement the API now, and you’ve got the /api/v1/projects URL<br />

returning a list of the projects that a user can see. To check what user this is, you’ve<br />

implemented a basic token authentication using functionality built in to Devise.<br />

There’s still a little way to go before you’re done with the API. For starters, this API<br />

only serves JSON requests, and some people who use it may wish for the data to be<br />

returned in XML. You’ve also only got the one action in your controller, and you need<br />

to implement a way for people to create, update, and delete projects through the API.<br />

Before you do that, you’ll add in support for XML. This is incredibly easy to implement,<br />

as you’ll soon see.<br />

13.1.5 Serving XML<br />

So far, you’ve been using the respond_with and respond_to methods to serve JSON<br />

responses. You can serve XML using these same methods while continuing to serve<br />

JSON. It’s very, very easy. First of all, you’re going to want to create a test to make sure<br />

that your new XML data is being returned correctly. You’ll place this test in the index<br />

context for “projects viewable by this user” in spec/api/v1/projects_spec.rb using the<br />

code from the following listing.<br />

Listing 13.7 spec/api/v1/projects_spec.rb<br />

it "XML" do<br />

get "#{url}.xml", :token => token<br />

last_response.body.should eql(Project.readable_by(user).to_xml)<br />

projects = Nokogiri::XML(last_response.body)<br />

projects.css("project name").text.should eql("Ticketee")<br />

end<br />

In this spec you use the nokogiri gem to parse the XML (in chapter 6, you used it to<br />

parse HTML). Then you use the css method to find an element called name inside

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

Saved successfully!

Ooh no, something went wrong!