class Codeforces::Viewer::Cacher

Attributes

option[RW]

Public Class Methods

new(enabled = true, cache_dir = DEFAULT_CACHE_DIR) click to toggle source
# File lib/codeforces/viewer/cacher.rb, line 13
def initialize(enabled = true, cache_dir = DEFAULT_CACHE_DIR)
  @option = {}
  @option[:enabled] = enabled
  if @option[:enabled]
    @option[:cache_dir] = File.expand_path cache_dir
    FileUtils.mkdir_p @option[:cache_dir]
  end
end

Public Instance Methods

get_url(url) click to toggle source
# File lib/codeforces/viewer/cacher.rb, line 22
def get_url(url)
  file_path = "#{@option[:cache_dir]}/#{Digest::MD5.hexdigest url}"
  if @option[:enabled] && File.exists?(file_path)
    File.read file_path
  else
    body = Net::HTTP.get URI.parse url
    if @option[:enabled]
      FileUtils.touch file_path
      File.write file_path, body
    end
    body
  end
end