Sha256: 074f2246456e22f5cb86644eface138786910b077450e3c9ed537b4c6d94c440
Contents?: true
Size: 844 Bytes
Versions: 18
Compression:
Stored size: 844 Bytes
Contents
# frozen_string_literal: true require 'eac_fs/patches' require 'eac_ruby_utils/fs/temp' module EacFs class CachedDownload attr_reader :url, :fs_cache def initialize(url, parent_fs_cache = nil) @url = url @fs_cache = (parent_fs_cache || fs_cache).child(url.to_s.parameterize) end def assert download unless fs_cache.stored? end def download ::EacRubyUtils::Fs::Temp.on_file do |temp| download_to(temp) fs_cache.content_path.to_pathname.dirname.mkpath ::FileUtils.mv(temp.to_path, fs_cache.content_path) end end def path fs_cache.content_path.to_pathname end private def download_to(local_file) ::URI.parse(url).open do |remote| local_file.open('wb') { |handle| handle.write(remote.read) } end end end end
Version data entries
18 entries across 18 versions & 2 rubygems