本文介绍了我怎么能叫在Ruby on Rails的一个API(例如Flickr的API)?新手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林建设我的第一个应用在铁轨和我想调用Flickr的API

我知道我可以使用Flickr的福,rflickr或其他宝石,但我想从头开始做我自己学习

说,例如,我想打电话给 flickr.photos.comments.getList 返回相片的意见。该参数是API_KEY和photo_id。我兼得。

我会得到响应为一个XML

 <评论photo_id =109722179>
<评论ID =6065-109722179-72057594077818641作者=35468159852 @ N01AUTHORNAME =冯丹CATTdatecreate =1141841470永久=htt​​p://www.flickr.com/photos/straup/109722179/ #comment72057594077818641>
嗯,我不知道,我可以还给你的,一个<?/评论>
< /意见>`

我要存储通过调用该方法到一个变量注释我可以使用,例如访问数据,

接收的数据

  comments.all.first.author_name =冯丹CATT

  comments.all.first.content =嗯,我不知道,我能回去......

我怎样才能做到这一点?请耐心等待(遗憾的noob问题)


解决方案

 包括RubyGems的'
包括httpartyFlickr的类
  包括HTTParty  base_uriapi.flickr.com  高清self.getPhotos(UID)
    得到的回报('/服务/ REST /':查询=> {
      :方法=> flickr.people.getPublicPhotos',
      :API_KEY => API密钥放在这里,
      :USER_ID => UID})
  结束
结束

,然后调用

  Flickr.getPhotos('12345678')

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)

解决方案
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')

这篇关于我怎么能叫在Ruby on Rails的一个API(例如Flickr的API)?新手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:49