использование Google Docs в приложении

Posted on August 25, 2009
Недавно мне понадобилось в один сервис вставить несколько страниц текста, поскольку все эти тексты были уже в виде google docs, то я написал простой класс который их получает и показывает:

require 'hpricot'

class Gdoc
  def self.get id
    text = Rails.cache.fetch("doc-#{id}", :expires_in => 15.minutes) do
      url = URI.parse('http://docs.google.com')
      res = Net::HTTP.start(url.host, url.port) do |http|
        http.get("/Doc?id=#{id}")
      end
      text = res.body
      doc = Hpricot(text)
      body = (doc/'body')
      body.search("#google-view-footer").remove
      body.search("script").remove
      body.inner_html.gsub('File?id=', 'http://docs.google.com/File?id=')
    end
    text
  end

  def self.index
    docs = Rails.cache.fetch("docs", :expires_in => 15.minutes) do
      raw = self.get GDOC_INDEX
      doc = Hpricot(raw)
      docs = []
      doc.search('//a').each do |link|
        docs << {:id => link['href'].gsub('View?docid=',''), :name => link.innerText, :path => link.innerText.downcase.gsub(' ','-')}
      end
      docs
    end
    docs
  end
end
Комментарии, думаю, излишни.