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.

348 CHAPTER 13 Designing an API<br />

One great example of how an API is used is Twitter. Twitter has had an API for an<br />

exceptionally long time now, and people have written Twitter clients for just about<br />

every operating system out there using this API. The functionality of the site is effectively<br />

the same, but people have come up with interesting ways of displaying and modifying<br />

that information, such as the Twitter for Mac clients.<br />

There are many, many Rails sites out there that already provide an API interface,<br />

such as GitHub 3 and (as previously mentioned) Twitter. 4 Both of these APIs have<br />

exceptionally well-documented examples of how to use them and what can be done<br />

with them, which is a great way to convince people to start using your API. API documentation,<br />

however, is an exercise best left for you after this chapter’s done.<br />

APIs aren’t unique to Rails. There are plenty of other sites out there that have<br />

implemented their own APIs, such as the StackExchange services that run on Microsoft.Net.<br />

Furthermore, APIs created in one language are not for exclusive use of API<br />

clients written in that specific language. For example, if you wanted to parse the data<br />

from the StackExchange API in Ruby, you could do that just fine.<br />

In Rails, however, it’s extremely easy to make a modern API for an application, as<br />

you’ll see in this chapter. Your application will serve in two formats: JSON and XML.<br />

Back to the Twitter and GitHub examples now, and one further thing to note is<br />

both of these APIs are also versioned. The point of this is that an API should be presented<br />

in a “frozen” state and should not be modified once it’s considered stable. This<br />

way, a user is be able to use an API without fear of it changing and potentially breaking<br />

the code that they’re using to parse the data from the API.<br />

Twitter has a URL such as http://api.twitter.com/1/statuses/public_timeline.json<br />

that has the version as the first part of the URL after the site name and then the format<br />

as an extension at the end. Github’s is slightly different, with a URL such as<br />

http://github.com/api/v2/json/repos/show/rails3book/ticketee having the version<br />

prefixed with a v as the second part of the URL, and the format as the part of the<br />

URL directly after. The v prefix here makes the version part of the URL clearer to<br />

those who are reading it, which is a good thing.<br />

You’re going to borrow ideas from both of these API designs, presenting your API<br />

at the base URL of /api/v1 and the format at the end of the URL, like most web<br />

requests. Your URLs will look like /api/v1/projects.json, which will be the URL to<br />

return a list of projects that the user is able to read. The reason for this versioning is so<br />

that you can always provide data that is predictable to your end users. If you wished to<br />

change the format of this data, you would create a new version namespace, which is<br />

the final thing we look at toward the end of this chapter. Really, these new version<br />

numbers can be whatever you wish, with minor versions such as 0.1 being the standard<br />

3 The octopi gem was built to interact with the GitHub API—you can see the source at http://github.com/<br />

fcoury/octopi.<br />

4 The (non-official) twitter gem was built to interact with the API that Twitter provides; you can see the<br />

source of this gem at http://github.com/jnunemaker/twitter.

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

Saved successfully!

Ooh no, something went wrong!