class CyberarmEngine::Cache::DownloadManager::Download
Attributes
callback[R]
error_message[R]
finished_at[R]
remaining_bytes[R]
save_as[R]
started_at[R]
status[RW]
total_bytes[R]
total_downloaded_bytes[R]
uri[R]
Public Class Methods
new(uri:, save_as:, callback: nil)
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 58 def initialize(uri:, save_as:, callback: nil) @uri = uri @save_as = save_as @callback = callback @status = :pending @remaining_bytes = 0.0 @total_downloaded_bytes = 0.0 @total_bytes = 0.0 @error_message = "" end
Public Instance Methods
download()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 79 def download @status = :downloading @started_at = Time.now # TODO: monotonic time io = File.open(@save_as, "w") streamer = lambda do |chunk, remaining_bytes, total_bytes| io.write(chunk) @remaining_bytes = remaining_bytes.to_f @total_downloaded_bytes += chunk.size @total_bytes = total_bytes.to_f end begin response = Excon.get( @uri.to_s, middlewares: Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower], response_block: streamer ) if response.status == 200 @status = :finished @finished_at = Time.now # TODO: monotonic time @callback.call(self) if @callback else @error_message = "Got a non 200 HTTP status of #{response.status}" @status = :failed @finished_at = Time.now # TODO: monotonic time @callback.call(self) if @callback end rescue StandardError => e # TODO: cherrypick errors to cature @status = :failed @finished_at = Time.now # TODO: monotonic time @error_message = e.message @callback.call(self) if @callback end ensure io.close if io end
progress()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 72 def progress v = 1.0 - (@remaining_bytes.to_f / total_bytes) return 0.0 if v.nan? v end