class BilibiliSunday::Cacher
Public Class Methods
new(dir)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 9 def initialize(dir) @dir = dir FileUtils.mkdir_p(@dir) end
Public Instance Methods
read_url(url)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 15 def read_url(url) cached?(url) ? cached_content(url) : write_cache_for_url(url, open(url).read) end
Private Instance Methods
cache_path_for_url(url)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 36 def cache_path_for_url(url) File.join(@dir, Digest::MD5.hexdigest(url)) end
cached?(url)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 23 def cached?(url) File.exists?(cache_path_for_url(url)) end
cached_content(url)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 27 def cached_content(url) File.open(cache_path_for_url(url)) { |f| f.read } end
write_cache_for_url(url, content)
click to toggle source
# File lib/bilibili_sunday/cacher.rb, line 31 def write_cache_for_url(url, content) File.open(cache_path_for_url(url), 'w') { |f| f.write(content) } content end