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.

384 CHAPTER 13 Designing an API<br />

git push<br />

renaming name to title for projects"<br />

Alright, so now you’ve got two versions of your API. Generally, there’s much more than<br />

a single change in a new API version, but this is a good start. When you announce this<br />

API to the people who use your application, they can switch their libraries over to<br />

using it immediately, or, ideally, remain using the old version. After a while, you may<br />

elect to turn off the first version of the API, and you would do that by giving your users<br />

considerable notice such as a month, and then un-defining the routes and deleting<br />

the controllers and tests associated with that version. Out with the old and in with the<br />

new, as they say.<br />

You’ve now seen how you can serve data from your application using an API, but<br />

how do you read it? The answer to that lies in a part of Rails called Active Resource.<br />

13.5 Summary<br />

You’ve seen in this chapter how you can use the Rack::Test::Methods module, given<br />

to you for free by the rack-test gem, to test that requests to URLs provided by your<br />

application return valid API responses in JSON and XML formats. Users will then be<br />

able to make these same requests for their own applications or libraries to get at the<br />

data in your system. What they come up with is up to their imagination. In this chapter,<br />

we only covered one aspect (projects) for your API, but with the knowledge found in<br />

this chapter you could easily create the other aspects for tickets, users, states, or tags.<br />

In the second section of this chapter you saw how you can limit the request rate to<br />

your API on a per-user basis. Users can make up to 100 requests to your system, and<br />

when they attempt to make their 101st the application denies them the request and<br />

provides a relevant error message. This is to deter people from excessively using the<br />

API, as you do not want your server to become overloaded immediately.<br />

Last, you saw how you can generate a new version of your API so that you can introduce<br />

a change, or changes, so as to not break the previous version of the API. Once an<br />

API has been released to the public, its output shouldn’t be modified, as this may<br />

affect the libraries referring to it. The easiest way to introduce these modifications is<br />

through a new version, which is what you did. Eventually, you may choose to deprecate<br />

the old API, or you may not. It’s really a matter of personal choice.<br />

Your application’s at a pretty great point now and is ready for prime time! To show<br />

it off to the masses, it’s best that you put the code on a computer dedicated to serving<br />

the application, rather than running it on some local hardware. In chapter 14, you’ll<br />

deploy your application to an Ubuntu 10.10 box, learning about the core components<br />

to a deployment software stack as you go.

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

Saved successfully!

Ooh no, something went wrong!