Sha256: 63629669e0476e0b4331a8898e452f78c689dbd1aaf58d668bf243d185caffa8

Contents?: true

Size: 837 Bytes

Versions: 19

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.cached?
    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

19 entries across 19 versions & 3 rubygems

Version Path
avm-tools-0.113.6 sub/eac_fs/lib/eac_fs/cached_download.rb
avm-tools-0.113.5 sub/eac_fs/lib/eac_fs/cached_download.rb
avm-tools-0.113.4 sub/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.9.0 lib/eac_fs/cached_download.rb
avm-tools-0.113.3 sub/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.8.1 lib/eac_fs/cached_download.rb
eac_fs-0.8.0 lib/eac_fs/cached_download.rb
avm-tools-0.113.2 sub/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.7.0 lib/eac_fs/cached_download.rb
ehbrs-tools-0.28.3 vendor/eac_fs/lib/eac_fs/cached_download.rb
avm-tools-0.110.0 vendor/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.6.0 lib/eac_fs/cached_download.rb
avm-tools-0.109.1 vendor/eac_fs/lib/eac_fs/cached_download.rb
avm-tools-0.109.0 vendor/eac_fs/lib/eac_fs/cached_download.rb
ehbrs-tools-0.28.2 vendor/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.5.0 lib/eac_fs/cached_download.rb
avm-tools-0.108.0 vendor/eac_fs/lib/eac_fs/cached_download.rb
eac_fs-0.4.0 lib/eac_fs/cached_download.rb
avm-tools-0.107.0 vendor/eac_fs/lib/eac_fs/cached_download.rb