class Sc20XX::DownloadThread
Attributes
events[R]
file[R]
progress[R]
total[R]
url[R]
Public Class Methods
new(url, filename)
click to toggle source
# File lib/sc20XX/download_thread.rb, line 8 def initialize(url, filename) @events = Events.new @url = URI.parse(url) @file = File.open(filename, "w") @progress = 0 start! end
Public Instance Methods
log(s)
click to toggle source
# File lib/sc20XX/download_thread.rb, line 16 def log(s) Sc20XX::Application.logger.debug("DownloadThread #{s}") end
start!()
click to toggle source
# File lib/sc20XX/download_thread.rb, line 20 def start! Thread.start do begin log :start http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.request(Net::HTTP::Get.new(url.request_uri)) do |res| log "response: #{res.code}" raise res.body if res.code != '200' @total = res.header['Content-Length'].to_i res.read_body do |chunk| @progress += chunk.size @file << chunk @file.close if @progress == @total end end rescue => e log e.message end end sleep 0.1 while @total.nil? sleep 0.1 self end