Sticky Plus!: Flickr検索機能を付けました

http://storehouse.quickvps.net/sticky_plus/sticky/board?url=http%3A%2F%2Fd.hatena.ne.jp%2Fwinebarrel
適当な言葉で検索(Flickr)すると、写真がぺたぺたぺたぺた…

flikrのコードはこんな感じ。

require 'cgi'
require 'open-uri'
require 'rexml/document'

module Flickr
  class Search
    @@base_url = 'http://api.flickr.com/services/rest/'

    def initialize(api_key)
      @api_key = api_key
    end

    def query(phrase, params = {})
      qs = query_string({
        :method   => 'flickr.photos.search',
        :api_key  => @api_key,
        :text     => phrase,
        :sort     => 'interestingness-desc',
        :per_page => 10
      }.update(params))

      open("#{@@base_url}?#{qs}") {|f| photos(f) }
    end

    private
    def query_string(params)
      params.map {|k, v| "#{k.to_s}=#{CGI.escape(v.to_s)}" }.join('&')
    end

    def photos(source)
      doc = REXML::Document.new(source)
      doc.elements.to_a('rsp/photos/photo').map do |e|
        attrs = e.attributes
        keys = [ :id, :owner, :secret, :server, :farm, :title ]
        vals = keys.map {|k| attrs[k.to_s] }
        ::Flickr::Photo.new(Hash[*keys.zip(vals).flatten])
      end
    end
  end

  class Photo
    def initialize(attrs)
      @id     = attrs[:id]
      @owner  = attrs[:owner]
      @secret = attrs[:secret]
      @server = attrs[:server]
      @farm   = attrs[:farm]
      @title  = attrs[:title]
    end

    def url
      "http://www.flickr.com/photos/#{@owner}/#{@id}/"
    end

    def image_url
      "http://farm#{@farm}.static.flickr.com/#{@server}/#{@id}_#{@secret}.jpg"
    end

    def thumbnail_url
      "http://farm#{@farm}.static.flickr.com/#{@server}/#{@id}_#{@secret}_t.jpg"
    end
  end
end