Class: Download

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/download.rb

Overview

An object of this type represents one unfinished or queued attempt to download a file.

Defined Under Namespace

Classes: DownloadException

Constant Summary collapse

@@log =
init_logger(STDOUT)
@@SHA256 =
Digest::SHA256.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initialize(url, local = nil) ⇒ Download

set attributes to reasonable defaults


41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date


115
116
117
# File 'lib/download.rb', line 115

def date
  @date
end

#digestObject (readonly)

Returns the value of attribute digest


115
116
117
# File 'lib/download.rb', line 115

def digest
  @digest
end

#local_fileObject

Returns the value of attribute local_file


114
115
116
# File 'lib/download.rb', line 114

def local_file
  @local_file
end

#positionObject

Returns the value of attribute position


114
115
116
# File 'lib/download.rb', line 114

def position
  @position
end

#sizeObject

Returns the value of attribute size


114
115
116
# File 'lib/download.rb', line 114

def size
  @size
end

#updatedObject

Returns the value of attribute updated


114
115
116
# File 'lib/download.rb', line 114

def updated
  @updated
end

#urlObject (readonly)

Returns the value of attribute url


115
116
117
# File 'lib/download.rb', line 115

def url
  @url
end

Class Method Details

.logObject


109
110
111
# File 'lib/download.rb', line 109

def self::log
	@@log
end

Instance Method Details

#file_hashObject


102
103
104
105
106
107
# 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_dumpObject

define the elements that will be saved in the base of queued downloads


68
69
70
# File 'lib/download.rb', line 68

def marshal_dump
	[@date, @updated, @url, @local_file, @position, @size, @digest]
end

#marshal_load(array) ⇒ Object

reconstruct a Download-object from the previously stored dump


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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_sObject


52
53
54
55
56
57
58
59
60
61
62
63
64
# 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