Class: Suvii::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/suvii/cache.rb

Overview

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.fetch(url, options = {}) {|path| ... } ⇒ String

Note:

This method doesn’t write to a disk. It is a responsibility of a block implementation.

Maps an archive URL to a path to its cached file.

Parameters:

  • url (String)

    URL of an archive to be processed.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :cache_path (String) — default: random temporary directory

    where the downloaded archive should be stored. You can provide path to a persistent folder to prevent downloading same files again.

Yields:

  • a block if the archive was not previously cached.

Yield Parameters:

  • path (String)

    full path to the archive to be stored.

Returns:

  • (String)

    full path to the stored archive.

Since:

  • 0.1.0



16
17
18
19
20
21
22
# File 'lib/suvii/cache.rb', line 16

def self.fetch(url, options = {})
  cache_path = options[:cache_path] || Dir.mktmpdir
  escaped_url = CGI.escape(url)
  archive_path = File.join(cache_path, escaped_url)
  yield archive_path unless File.exist?(archive_path)
  archive_path
end