Class: Jenkins::Peace::ContentDownloader
- Inherits:
-
Object
- Object
- Jenkins::Peace::ContentDownloader
- Defined in:
- lib/jenkins/peace/content_downloader.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#target_file ⇒ Object
readonly
Returns the value of attribute target_file.
Instance Method Summary collapse
- #build_progress_bar(total) ⇒ Object
- #download(url, limit = 10) ⇒ Object
- #download_content(url) ⇒ Object
-
#initialize(target_file, logger) ⇒ ContentDownloader
constructor
A new instance of ContentDownloader.
- #start_http_session(url) ⇒ Object
Constructor Details
#initialize(target_file, logger) ⇒ ContentDownloader
Returns a new instance of ContentDownloader.
13 14 15 16 |
# File 'lib/jenkins/peace/content_downloader.rb', line 13 def initialize(target_file, logger) @target_file = target_file @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/jenkins/peace/content_downloader.rb', line 10 def logger @logger end |
#target_file ⇒ Object (readonly)
Returns the value of attribute target_file.
9 10 11 |
# File 'lib/jenkins/peace/content_downloader.rb', line 9 def target_file @target_file end |
Instance Method Details
#build_progress_bar(total) ⇒ Object
57 58 59 |
# File 'lib/jenkins/peace/content_downloader.rb', line 57 def (total) ProgressBar.create(title: 'Downloading', starting_at: 0, total: total, format: '%a |%b>%i| %p%% %t') end |
#download(url, limit = 10) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jenkins/peace/content_downloader.rb', line 19 def download(url, limit = 10) raise ArgumentError, 'too many HTTP redirects' if limit == 0 start_http_session(url) do |http, uri| response = http.request_head(uri.path) case response when Net::HTTPSuccess download_content(url) when Net::HTTPRedirection redirect = response['location'] logger.info "Redirected to : '#{redirect}'" download(redirect, limit - 1) else logger.error response.value end end rescue => e logger.error "Error while downloading '#{url}' : #{e.}" end |
#download_content(url) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jenkins/peace/content_downloader.rb', line 40 def download_content(url) logger.info "Downloading : '#{url}'" start_http_session(url) do |http, uri| response = http.request_head(uri.path) = (response['content-length'].to_i) File.open(target_file, 'wb') do |file| http.get(uri.path) do |chunk| file.write chunk .progress += chunk.length end end end rescue => e logger.error "Error while downloading '#{url}' : #{e.}" end |
#start_http_session(url) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/jenkins/peace/content_downloader.rb', line 62 def start_http_session(url) uri = URI(url) Net::HTTP.start(uri.host, uri.port) do |http| yield http, uri end end |