Module: CachedHtml

Included in:
EventScraper
Defined in:
lib/cached_html.rb

Constant Summary collapse

CACHE_DIR =
'html_cache'

Instance Method Summary collapse

Instance Method Details

#cached_html(url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cached_html.rb', line 15

def cached_html(url)
  if File.size?(filename(url))
    File.read(filename(url))
  else
    puts "Fetching #{url}"
    res  = open(url)
    html = res.read
    File.open(filename(url), 'w') {|f| f.write html}
    puts "Cached html to #{filename(url)}"
    html
  end
end

#filename(url) ⇒ Object



7
8
9
# File 'lib/cached_html.rb', line 7

def filename(url)
  File.join(CACHE_DIR, munge(url))
end

#munge(url) ⇒ Object



11
12
13
# File 'lib/cached_html.rb', line 11

def munge(url)
  url.sub("http://", '').sub(/\W$/, '').gsub('/', '.').gsub(/\W/, '-')
end