Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get web file with cache

DZone's Guide to

Get web file with cache

·
Free Resource

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
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}