Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call an API (for example Flickr API) in Ruby on Rails? Newbie question

Im building my first app in rails and I would like to call Flickr's API

I know I can use flickr-fu, rflickr or other gem but I want to do it myself from scratch to learn

Say, for example, I want to call flickr.photos.comments.getList that returns the comments for a photo. The arguments are api_key and photo_id. I have both.

I will get the response as an xml

<comments photo_id="109722179">
<comment id="6065-109722179-72057594077818641" author="35468159852@N01" authorname="Rev Dan Catt" datecreate="1141841470" permalink="http://www.flickr.com/photos/straup/109722179/#comment72057594077818641">
Umm, I'm not sure, can I get back to you on that one?</comment>
</comments>`

I want to store the data received by calling that method into a variable comments that I can access the data using, for example,

comments.all.first.author_name = "Rev Dan Catt"

or

comments.all.first.content = "Umm, I'm not sure, can I get back to..."

How can I do this? Please be patient (sorry for the noob question)

like image 578
Victor Avatar asked Jan 21 '10 22:01

Victor


People also ask

How do I get Flickr API?

Obtaining an API key You must have an Yahoo! account to log in to the flickr.com or if you don't have one, you have to sign up to Flickr. Once you are within Flickr, point your browser to https://www.flickr.com/services/api/. Go to 'Create an APP' for creating an API key. Click on the link "Request an API Key".

Does Flickr have an API?

Getting Started To begin using the Flickr API: Request an API key, to sign your API requests. Read the Community Guidelines and the API Terms of Use. Build, build, build.

Where is my Flickr API key?

Go to the Flickr API Services page. If you have not yet logged into your Flickr account, you will be asked to do so. Once you have logged in, the Flickr Services page is displayed with some general information about API keys.


1 Answers

include 'rubygems'
include 'httparty'

class Flickr
  include HTTParty

  base_uri 'api.flickr.com'

  def self.getPhotos(uid)
    return get('/services/rest/', :query => {
      :method => 'flickr.people.getPublicPhotos',
      :api_key => 'api key goes here',
      :user_id => uid})
  end
end

and then call

Flickr.getPhotos('12345678')
like image 156
Victor Avatar answered Sep 17 '22 06:09

Victor