Class: Berkshelf::Downloader
- Inherits:
-
Object
- Object
- Berkshelf::Downloader
- Extended by:
- Forwardable
- Defined in:
- lib/berkshelf/downloader.rb
Constant Summary collapse
- DEFAULT_LOCATIONS =
[ { type: :site, value: Location::OPSCODE_COMMUNITY_API, options: Hash.new } ]
Instance Attribute Summary collapse
-
#cookbook_store ⇒ String
readonly
A filepath to download cookbook sources to.
Instance Method Summary collapse
-
#add_location(type, value, options = {}) ⇒ Hash
Create a location hash and add it to the end of the array of locations.
-
#download(source) ⇒ Array
Download the given CookbookSource.
-
#has_location?(type, value) ⇒ Boolean
Checks the list of default locations if a location of the given type and value has already been added and returns true or false.
-
#initialize(cookbook_store, options = {}) ⇒ Downloader
constructor
A new instance of Downloader.
-
#locations ⇒ Array<Hash>
An Array of Hashes representing each default location that can be used to attempt to download cookbook sources which do not have an explicit location.
Constructor Details
#initialize(cookbook_store, options = {}) ⇒ Downloader
Returns a new instance of Downloader.
23 24 25 26 |
# File 'lib/berkshelf/downloader.rb', line 23 def initialize(cookbook_store, = {}) @cookbook_store = cookbook_store @locations = .fetch(:locations, Array.new) end |
Instance Attribute Details
#cookbook_store ⇒ String (readonly)
Returns a filepath to download cookbook sources to.
18 19 20 |
# File 'lib/berkshelf/downloader.rb', line 18 def cookbook_store @cookbook_store end |
Instance Method Details
#add_location(type, value, options = {}) ⇒ Hash
Create a location hash and add it to the end of the array of locations.
subject.add_location(:chef_api, “chef:8080”, node_name: “reset”, client_key: “/Users/reset/.chef/reset.pem”) =>
[ { type: :chef_api, value: "http://chef:8080/", node_name: "reset", client_key: "/Users/reset/.chef/reset.pem" } ]
46 47 48 49 50 51 52 53 |
# File 'lib/berkshelf/downloader.rb', line 46 def add_location(type, value, = {}) if has_location?(type, value) raise DuplicateLocationDefined, "A default '#{type}' location with the value '#{value}' is already defined" end @locations.push(type: type, value: value, options: ) end |
#download(source) ⇒ Array
Download the given CookbookSource.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/berkshelf/downloader.rb', line 71 def download(source) if source.location begin location = source.location cached = download_location(source, location, true) source.cached_cookbook = cached return [cached, location] rescue => e raise if e.kind_of?(CookbookValidationFailure) Berkshelf.formatter.error "Failed to download '#{source.name}' from #{source.location}" end else locations.each do |loc| = loc[:options].merge(loc[:type] => loc[:value]) location = Location.init(source.name, source.version_constraint, ) cached = download_location(source, location) if cached source.cached_cookbook = cached return [cached, location] end end end raise CookbookNotFound, "Cookbook '#{source.name}' not found in any of the default locations" end |
#has_location?(type, value) ⇒ Boolean
Checks the list of default locations if a location of the given type and value has already been added and returns true or false.
59 60 61 |
# File 'lib/berkshelf/downloader.rb', line 59 def has_location?(type, value) @locations.select { |loc| loc[:type] == type && loc[:value] == value }.any? end |
#locations ⇒ Array<Hash>
Returns an Array of Hashes representing each default location that can be used to attempt to download cookbook sources which do not have an explicit location. An array of default locations will be used if no locations are explicitly added by the #add_location function.
32 33 34 |
# File 'lib/berkshelf/downloader.rb', line 32 def locations @locations.any? ? @locations : DEFAULT_LOCATIONS end |