Class: WebPage

Inherits:
Object
  • Object
show all
Defined in:
lib/webpage.rb

Constant Summary collapse

REFRESH_AFTER =
60
USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ WebPage

Returns a new instance of WebPage.



9
10
11
12
# File 'lib/webpage.rb', line 9

def initialize(url)
  @url = URI::parse url
  @raw = nil
end

Instance Method Details

#rawObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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