Class: Avm::CachedDownload

Inherits:
Object show all
Defined in:
lib/avm/cached_download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, parent_fs_cache = nil) ⇒ CachedDownload

Returns a new instance of CachedDownload.



10
11
12
13
# File 'lib/avm/cached_download.rb', line 10

def initialize(url, parent_fs_cache = nil)
  @url = url
  @fs_cache = (parent_fs_cache || ::Avm.fs_cache).child(url.parameterize)
end

Instance Attribute Details

#fs_cacheObject (readonly)

Returns the value of attribute fs_cache.



8
9
10
# File 'lib/avm/cached_download.rb', line 8

def fs_cache
  @fs_cache
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/avm/cached_download.rb', line 8

def url
  @url
end

Instance Method Details

#assertObject



15
16
17
# File 'lib/avm/cached_download.rb', line 15

def assert
  download unless fs_cache.cached?
end

#downloadObject



19
20
21
22
23
24
25
# File 'lib/avm/cached_download.rb', line 19

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

#pathObject



27
28
29
# File 'lib/avm/cached_download.rb', line 27

def path
  @path ||= fs_cache.content_path.to_pathname
end