Sha256: 2c0b8bf27c01916f86ec2e6d0d516451f01f7a446af99634b92e9f2c0bda48a7
Contents?: true
Size: 863 Bytes
Versions: 118
Compression:
Stored size: 863 Bytes
Contents
# frozen_string_literal: true require 'eac_fs/patches' require 'eac_ruby_utils/fs/temp' require 'open-uri' 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
118 entries across 118 versions & 2 rubygems