class Download
An object of this type represents one unfinished or queued attempt to download a file.
Attributes
date[R]
digest[R]
local_file[RW]
position[RW]
size[RW]
updated[RW]
url[R]
Public Class Methods
log()
click to toggle source
# File lib/download.rb, line 109 def self::log @@log end
new(url, local = nil)
click to toggle source
set attributes to reasonable defaults
# File lib/download.rb, line 41 def initialize(url, local = nil) @log = @@log @url = url @local_file = local @date = Date.today @updated = DateTime.now @position = 0 @size = 0 @digest = "" end
Public Instance Methods
file_hash()
click to toggle source
# File lib/download.rb, line 102 def file_hash @digest=' ' if File.exist?(@local_file) && File.readable?(@local_file) @digest = @@SHA256.hexdigest(File::read(@local_file)) end end
marshal_dump()
click to toggle source
define the elements that will be saved in the base of queued downloads
# File lib/download.rb, line 68 def marshal_dump [@date, @updated, @url, @local_file, @position, @size, @digest] end
marshal_load(array)
click to toggle source
reconstruct a Download-object from the previously stored dump
# File lib/download.rb, line 74 def marshal_load array @date, @updated, @url, @local_file, @position, @size, @digest = array if(array.include?(nil) ) raise DownloadException.new('Incomplete Download-data. NIL-values detected') end if(@local_file && File.exist?(@local_file)) if(File.readable?(@local_file)) d = @@SHA256.hexdigest(File::read(@local_file)) if(d && d != @digest) @digest ||= " N I L " de = DownloadException.new('Calculated digest for current file is different from stored value' << "\n\t" << d << " != " << @digest << "\n" ) puts(de.message) puts "Do you want to continue anyway (Y/n)?" r = wait_for_user.chr if('Y' != r.upcase) raise de exit true end end else puts ('local file ' << @local_file << ' is unusable! Aborting.') exit false end end end
to_s()
click to toggle source
# File lib/download.rb, line 52 def to_s s = self.class.name s << '{' instance_variables.each do |v| unless [:@target, :@log].include?(v) val = instance_variable_get(v) s << v.to_s << '=' << (val ? val.to_s : 'NIL') s << ' [' << val.class.name << ']' if val s << ', ' end end s << '}' end