Sha256: ce61e4f7c1cf32738849204f2becf6694ae96197408c3cace0b33e7ef0528571

Contents?: true

Size: 852 Bytes

Versions: 69

Compression:

Stored size: 852 Bytes

Contents

# frozen_string_literal: true

require 'avm/fs_cache'
require 'eac_ruby_utils/core_ext'

module Avm
  class CachedDownload
    attr_reader :url, :fs_cache

    def initialize(url, parent_fs_cache = nil)
      @url = url
      @fs_cache = (parent_fs_cache || ::Avm.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
      @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

69 entries across 69 versions & 1 rubygems

Version Path
avm-tools-0.106.0 lib/avm/cached_download.rb
avm-tools-0.105.0 lib/avm/cached_download.rb
avm-tools-0.104.0 lib/avm/cached_download.rb
avm-tools-0.103.1 lib/avm/cached_download.rb
avm-tools-0.103.0 lib/avm/cached_download.rb
avm-tools-0.102.2 lib/avm/cached_download.rb
avm-tools-0.102.1 lib/avm/cached_download.rb
avm-tools-0.102.0 lib/avm/cached_download.rb
avm-tools-0.101.0 lib/avm/cached_download.rb
avm-tools-0.100.0 lib/avm/cached_download.rb
avm-tools-0.99.1 lib/avm/cached_download.rb
avm-tools-0.99.0 lib/avm/cached_download.rb
avm-tools-0.98.0 lib/avm/cached_download.rb
avm-tools-0.97.0 lib/avm/cached_download.rb
avm-tools-0.96.0 lib/avm/cached_download.rb
avm-tools-0.95.0 lib/avm/cached_download.rb
avm-tools-0.94.3 lib/avm/cached_download.rb
avm-tools-0.94.2 lib/avm/cached_download.rb
avm-tools-0.94.1 lib/avm/cached_download.rb
avm-tools-0.94.0 lib/avm/cached_download.rb