Class: Puppet::Forge::Cache
Overview
Cache
Provides methods for reading files from local cache, filesystem or network.
Class Method Summary collapse
-
.base_path ⇒ Object
Return the base Pathname for all the caches.
-
.clean ⇒ Object
Clean out all the caches.
Instance Method Summary collapse
-
#initialize(repository, options = {}) ⇒ Cache
constructor
Instantiate new cahe for the
repositry
instance. -
#path ⇒ Object
Return Pathname for repository’s cache directory, create it if needed.
-
#read_retrieve(uri) ⇒ Object
Return contents of file at the given URI’s
uri
. -
#retrieve(url) ⇒ Object
Return filename retrieved from
uri
instance.
Constructor Details
#initialize(repository, options = {}) ⇒ Cache
Instantiate new cahe for the repositry
instance.
10 11 12 13 |
# File 'lib/vendor/puppet/forge/cache.rb', line 10 def initialize(repository, = {}) @repository = repository @options = end |
Class Method Details
.base_path ⇒ Object
Return the base Pathname for all the caches.
46 47 48 |
# File 'lib/vendor/puppet/forge/cache.rb', line 46 def self.base_path Pathname(Puppet.settings[:module_working_dir]) + 'cache' end |
.clean ⇒ Object
Clean out all the caches.
51 52 53 |
# File 'lib/vendor/puppet/forge/cache.rb', line 51 def self.clean base_path.rmtree if base_path.exist? end |
Instance Method Details
#path ⇒ Object
Return Pathname for repository’s cache directory, create it if needed.
41 42 43 |
# File 'lib/vendor/puppet/forge/cache.rb', line 41 def path (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath } end |
#read_retrieve(uri) ⇒ Object
Return contents of file at the given URI’s uri
.
36 37 38 |
# File 'lib/vendor/puppet/forge/cache.rb', line 36 def read_retrieve(uri) return uri.read end |
#retrieve(url) ⇒ Object
Return filename retrieved from uri
instance. Will download this file and cache it if needed.
TODO: Add checksum support. TODO: Add error checking.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vendor/puppet/forge/cache.rb', line 20 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 |