class Puppet::Forge::Cache

Cache

Provides methods for reading files from local cache, filesystem or network.

Public Instance Methods

path() click to toggle source

Return Pathname for repository’s cache directory, create it if needed.

# File lib/puppet/forge/cache.rb, line 43
def path
  (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath }
end
read_retrieve(uri) click to toggle source

Return contents of file at the given URI’s uri.

# File lib/puppet/forge/cache.rb, line 38
def read_retrieve(uri)
  return uri.read
end
retrieve(url) click to toggle source

Return filename retrieved from uri instance. Will download this file and cache it if needed.

TODO: Add checksum support. TODO: Add error checking.

# File lib/puppet/forge/cache.rb, line 22
def retrieve(url)
  (path + File.basename(url.to_s)).tap do |cached_file|
    uri = url.is_a?(::URI) ? url : ::URI.parse(url)
    unless cached_file.file?
      if uri.scheme == 'file'
        FileUtils.cp(URI.unescape(uri.path), cached_file)
      else
        # TODO: Handle HTTPS; probably should use repository.contact
        data = read_retrieve(uri)
        cached_file.open('wb') { |f| f.write data }
      end
    end
  end
end

Public Class Methods

base_path() click to toggle source

Return the base Pathname for all the caches.

# File lib/puppet/forge/cache.rb, line 48
def self.base_path
  (Pathname(Puppet.settings[:module_working_dir]) + 'cache').tap do |o|
    o.mkpath unless o.exist?
  end
end
clean() click to toggle source

Clean out all the caches.

# File lib/puppet/forge/cache.rb, line 55
def self.clean
  base_path.rmtree if base_path.exist?
end
new(repository, options = {}) click to toggle source

Instantiate new cache for the repository instance.

# File lib/puppet/forge/cache.rb, line 12
def initialize(repository, options = {})
  @repository = repository
  @options = options
end