class WebPage

Constants

REFRESH_AFTER
USER_AGENT

Public Class Methods

new(url) click to toggle source
# File lib/webpage.rb, line 9
def initialize(url)
  @url = URI::parse url
  @raw = nil
end

Public Instance Methods

raw() click to toggle source
# File lib/webpage.rb, line 14
def raw
  tmp = "/tmp/#{@url.hostname}#{@url.path.gsub(/\//, '_')}"
  begin
    if File.mtime(tmp) + REFRESH_AFTER > Time.now
      return @raw if @raw != nil
      @raw = File.read tmp
      return @raw
    end
  rescue
  end
  begin
    doc = open @url, :allow_redirections => :all,
                    'User-Agent' => USER_AGENT
  rescue => exc
    raise "Could not get #{@url}: #{exc.message}"
  end
  @raw = doc.read
  File.write tmp, @raw
  return @raw
end