class DwcaHunter::Downloader
Attributes
url[R]
Public Class Methods
new(source_url, file_path)
click to toggle source
# File lib/dwca_hunter/downloader.rb, line 7 def initialize(source_url, file_path) @source_url = source_url @file_path = file_path @url = Url.new(source_url) @download_length = 0 @filename = nil end
Public Instance Methods
download() { |download_length| ... }
click to toggle source
downloads a given file into a specified filename. If block is given returns download progress
# File lib/dwca_hunter/downloader.rb, line 17 def download raise "#{@source_url} is not accessible" unless @url.valid? f = open(@file_path,'wb') count = 0 @url.net_http.request_get(@url.path) do |r| r.read_body do |s| @download_length += s.length f.write s if block_given? count += 1 if count % 100 == 0 yield @download_length end end end end f.close downloaded = @download_length @download_length = 0 downloaded end
download_with_percentage() { |res| ... }
click to toggle source
# File lib/dwca_hunter/downloader.rb, line 39 def download_with_percentage start_time = Time.now download do |r| percentage = r.to_f/@url.header.content_length * 100 elapsed_time = Time.now - start_time eta = calculate_eta(percentage, elapsed_time) res = { percentage: percentage, elapsed_time: elapsed_time, eta: eta } yield res end end
Protected Instance Methods
calculate_eta(percentage, elapsed_time)
click to toggle source
# File lib/dwca_hunter/downloader.rb, line 54 def calculate_eta(percentage, elapsed_time) eta = elapsed_time/percentage * 100 - elapsed_time eta = 1.0 if eta <= 0 eta end