Class: Berkshelf::Downloader
- Inherits:
-
Object
- Object
- Berkshelf::Downloader
- Extended by:
- Forwardable
- Defined in:
- lib/berkshelf/downloader.rb
Overview
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
Downloads 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.
21 22 23 24 |
# File 'lib/berkshelf/downloader.rb', line 21 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.
16 17 18 |
# File 'lib/berkshelf/downloader.rb', line 16 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" } ]
44 45 46 47 48 49 50 51 |
# File 'lib/berkshelf/downloader.rb', line 44 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
Downloads the given CookbookSource.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/berkshelf/downloader.rb', line 69 def download(source) cached_cookbook, location = if source.location begin [source.location.download(storage_path), source.location] rescue Berkshelf.formatter.error "Failed to download #{source.name} from #{source.location}" raise end else search_locations(source) end source.cached_cookbook = cached_cookbook [cached_cookbook, location] 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.
57 58 59 |
# File 'lib/berkshelf/downloader.rb', line 57 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.
30 31 32 |
# File 'lib/berkshelf/downloader.rb', line 30 def locations @locations.any? ? @locations : DEFAULT_LOCATIONS end |