In this warmup, you’ll get a chance to poke around with an existing API from Flickr. You’ll need to read the documentation to understand which calls to make but they have a nice API explorer tool which submits API calls on your behalf.
XYZ API docs
to locate these pages, which is usually much faster and easier than trying to find them by navigating the websites themselves.http://api.flickr.com/services/rest/
and include any required data in the GET query string or the POST body.When the page refreshes, you’ll see your results down at the bottom. You should see a big list of photo objects (after some meta data) that were returned by your search. They look like:
{ "id": "11357337313", "owner": "84645040@N00", "secret": "6dd795c9c6", "server": "3805", "farm": 4, "title": "Gavin-Feb2013-0127", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
More interestingly, you can see the URL they used to make the request below that. I’ve broken it apart here to show the parameters more clearly:
http://api.flickr.com/services/rest/
?method=flickr.photos.search
&api_key=e0eb58bf4b3e29b253e86d6092e69dee
&tags=puppies
&format=json
&nojsoncallback=1
&api_sig=200efb63cb01a3d141fff12585e1e20a
Flickr’s API requires two steps to actually display a photo – you need to get a photo’s meta information (which we just received in our search results) and then you need to piece it together into a URL that Flickr can understand to actually retrieve the photo. The format they suggest is:
http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
We can plug in values from the previously retrieved photo to display a photo:
http://farm4.staticflickr.com/3805/11357337313_6dd795c9c6.jpg
Which looks like:
We could also add in additional parameters like size
on the end.
This is a fast and straightforward project where you’ll set up a Rails app to be a data-producing API… which is just a fancy way of saying that all your controller methods will render data instead of HTML. Consider this a drill in quickly building a pure vanilla RESTful resource. We won’t be working with an external API until the next project.
We’ll start by building our Kitten application to work normally in the browser with HTML.
odin-kittens
) and Git repo.:name
, :age
, :cuteness
, and :softness
.:kittens
routes for all 7 RESTful actions.KittensController#index
.#index
should just list all Kittens, #show
should display a single Kitten, #new
should render a simple Kitten creation form, #edit
should use the same form (which should be a partial used by both the New and Edit views) to Edit the Kitten, #create
and #update
should do their jobs.delete
link on the Kitten’s Show and Edit pages, as well as next to each Kitten listed in the Index page.flash
hash which congratulates you on adding or editing or deleting kittens and makes fun of you for errors in your form.Now it’s time to make the Kittens resource available via API.
> require 'rest-client'
(you may need to $ gem install rest-client
if you haven’t already). Test it out by making a request to your application using > response = RestClient.get("http://localhost:3000/kittens")
#body
or #to_s
on the RestClient::Response
object response
should return a sloppy mess of HTML. If you check out your server output, it’s probably processing as */* (i.e. all media types), e.g. Processing by KittensController#index as */*
:accept => :json
, e.g. RestClient.get("http://localhost:3000/kittens", :accept => :json)
. It should throw an error.#index
method to #respond_to
JSON and render the proper variables.> r = RestClient.get("http://localhost:3000/kittens", :accept => :json)
, > puts r.body
.#show
method, which will require you to provide an ID when making your request. Your CSRF protection will prevent you from creating, updating or deleting kittens via the API, so it’s not necessary to implement those.This project may seem simple, but now you’ve got a website that is both a normal HTML-producing back end AND an API that can be used to pull data from it. You could use JavaScript calls from the front end to dynamically refresh your data now or even to load the whole page in the first place. Or maybe you’ll be hooking up a Kittens app to your iPhone and need a back end. It doesn’t matter, since now you’ve got a RESTful API.
This section contains helpful links to other content. It isn’t required, so consider it supplemental for if you need to dive deeper into something.
5-6 months
Job Guarantee
1-on-1 Mentorship