require 'net/http' require 'rexml/document' require 'ostruct' class Flickr < OpenStruct include REXML def Flickr.search(text) doc = Document.new( Net::HTTP.get( URI.parse('http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=MY_API_KEY' + '&extras=license,owner_name,original_format&license=4,5&per_page=20&sort=interestingness-desc' + '&text=' + text))) throw "flickr error" unless doc.root.attributes['stat'] == "ok" doc.root.elements['photos'].get_elements('//photo').collect {|photo| photo << Flickr.new(photo) } end def initialize(e) super(e.attributes) self.new_ostruct_member("photo_id") self.photo_id = e.attributes['id'] end def to_url(image_type="s") "http://farm#{farm}.static.flickr.com/#{server}/#{photo_id}_#{secret}_#{image_type}.jpg" end end
You need to create an account or log in to post comments to this site.