01 - finding photos on flikr.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 34 行

RB
34
字号
require 'open-uri'require 'rexml/document'require 'cgi'FLICKR_API_KEY = 'Your API key here'#---def flickr_call(method_name, arg_map={}.freeze)  args = arg_map.collect {|k,v| CGI.escape(k) + '=' + CGI.escape(v)}.join('&')  url = "http://www.flickr.com/services/rest/?api_key=%s&method=%s&%s" %    [FLICKR_API_KEY, method_name, args]  doc = REXML::Document.new(open(url).read)end#---def pick_a_photo(tag) doc = flickr_call('flickr.photos.search', 'tags' => tag, 'license' => '4',                   'per_page' => '1') photo = REXML::XPath.first(doc, '//photo') small_photo_url(photo) if photoend#---def small_photo_url(photo) server, id, secret = ['server', 'id', 'secret'].collect do |field|   photo.attribute(field) end "http://static.flickr.com/#{server}/#{id}_#{secret}_m.jpg"end#---pick_a_photo('elephants')# => http://static.flickr.com/32/102580480_506d5865d0_m.jpgpick_a_photo('what-will-happen-tomorrow')# => nil#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?