Sha256: 261c0c1d2b23d762dc9da5ef7f279b379a00e6489493cc442708f8b629a1d1fa

Contents?: true

Size: 837 Bytes

Versions: 3

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

require 'eac_fs/patches'
require 'eac_ruby_utils/fs/temp'

module Avm
  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.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

3 entries across 3 versions & 2 rubygems

Version Path
eac_fs-0.11.0 lib/eac_fs/cached_download.rb
avm-tools-0.114.0 sub/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.10.0 lib/eac_fs/cached_download.rb