Get web file with cache
Join the DZone community and get the full member experience.Join For Free
Gets a file from the web over http. A cache is maintained in the tmp directory. Cached entries are permanent, so a file will never be downloaded twice even if it has changed on the server.
If you want a dynamic cache, you might try open-uri-cache .
require 'open-uri' require 'tmpdir' require 'fileutils' require 'digest' def wget(url) cachedir = File.join( Dir.tmpdir, 'httpcache' ) filename = File.join( cachedir, Digest::MD5.hexdigest(url) ) FileUtils.mkdir_p(cachedir) if !File.exists?(cachedir) if File.exist?(filename) body = File.binread(filename) else body = open(url).read File.open(filename, "wb") do |saved_file| saved_file.write(body) end end body end