Class: ChefApply::FileFetcher
- Inherits:
-
Object
- Object
- ChefApply::FileFetcher
- Defined in:
- lib/chef_apply/file_fetcher.rb
Class Method Summary collapse
- .download_file(url, local_path) ⇒ Object
-
.fetch(path) ⇒ Object
Simple fetcher of an http(s) url.
Class Method Details
.download_file(url, local_path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/chef_apply/file_fetcher.rb', line 42 def download_file(url, local_path) temp_path = "#{local_path}.downloading" file = open(temp_path, "wb") ChefApply::Log.debug "Downloading: #{temp_path}" Net::HTTP.start(url.host) do |http| http.request_get(url.path) do |resp| resp.read_body do |segment| file.write(segment) end end rescue e @error = true raise ensure file.close # If any failures occurred, don't risk keeping # an incomplete download that we'll see as 'cached' if @error FileUtils.rm_f(temp_path) else FileUtils.mv(temp_path, local_path) end end end |
.fetch(path) ⇒ Object
Simple fetcher of an http(s) url. Returns the local path of the downloaded file.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/chef_apply/file_fetcher.rb', line 28 def fetch(path) cache_path = ChefApply::Config.cache.path FileUtils.mkdir_p(cache_path) url = URI.parse(path) name = File.basename(url.path) local_path = File.join(cache_path, name) # TODO header check for size or checksum? return local_path if File.exist?(local_path) download_file(url, local_path) local_path end |