Sha256: 776bec605cf9854f4b7cc21a2dd12ed1650a96421917294da71651e57bce565e

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'uri'
require 'open-uri'
require 'fileutils'
require 'digest/md5'

module Rhcf
  module Utils
    class DownloadCache
      include FileUtils
      def initialize(cache_id= 'default', ttl = nil, root_path = nil)
        @cache_id = cache_id
        @ttl = ttl
        @root_path ||= "/tmp/#{self.class.name}"
      end
  
  
      def get(url)
        outfile = filename_for(url)
        return outfile if self.class.hit_fname?(outfile, @ttl) # here goes the cache
        download!(url, outfile)
      rescue 
        File.unkink(outfile) if File.exist?(outfile)
        raise
      end
 

      def download!(url, outfile)
        mkdir_p(File.dirname(outfile))
        File.open(outfile, 'w') do |fd|
          open(url, "r") do |down|
            fd.write(down.read)
          end
        end
        outfile
      end 

      def hit?(url)
        outfile = filename_for(url)
        self.class.hit_fname?(outfile, @ttl) # here goes the cache
      end
  
      def self.hit_fname?(fname, ttl)
        
        if File.exist?(fname) 
          if ttl
            File::Stat.new(fname).ctime > Time.now - ttl
          else 
            true
          end
        else
          false
        end
      end
  
      def filename_for(url)
        hash =  Digest::MD5.hexdigest(url)
        uri = URI(url)
        basename = File.basename(uri.path)
        File.join(@root_path, @cache_id, hash, basename) 
      end

    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rhcf-utils-0.0.1 lib/rhcf/utils/download_cache.rb