Class: Shoes::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/shoes/download.rb

Constant Summary collapse

UPDATE_STEPS =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}, &blk) ⇒ Download

Returns a new instance of Download



16
17
18
19
20
21
22
23
24
# File 'lib/shoes/download.rb', line 16

def initialize(url, opts = {}, &blk)
  @opts = opts
  @blk = blk
  @response = HttpResponse.new
  @finished = false
  @transferred = 0
  @length = 0
  start_download url
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length



9
10
11
# File 'lib/shoes/download.rb', line 9

def content_length
  @content_length
end

#guiObject (readonly)

Returns the value of attribute gui



9
10
11
# File 'lib/shoes/download.rb', line 9

def gui
  @gui
end

#lengthObject (readonly)

length and percent is preserved for Shoes3 compatibility



11
12
13
# File 'lib/shoes/download.rb', line 11

def length
  @length
end

#percentObject (readonly)

length and percent is preserved for Shoes3 compatibility



11
12
13
# File 'lib/shoes/download.rb', line 11

def percent
  @percent
end

#progressObject (readonly)

Returns the value of attribute progress



9
10
11
# File 'lib/shoes/download.rb', line 9

def progress
  @progress
end

#responseObject (readonly)

Returns the value of attribute response



9
10
11
# File 'lib/shoes/download.rb', line 9

def response
  @response
end

#threadObject (readonly)

thread is used by packager to sync download



13
14
15
# File 'lib/shoes/download.rb', line 13

def thread
  @thread
end

#transferredObject (readonly)

Returns the value of attribute transferred



9
10
11
# File 'lib/shoes/download.rb', line 9

def transferred
  @transferred
end

Instance Method Details

#content_length_procObject



48
49
50
51
52
53
# File 'lib/shoes/download.rb', line 48

def content_length_proc
  lambda do |content_length|
    download_started(content_length)
    #eval_block(@opts[:progress], self) if @opts[:progress]
  end
end

#download_started(content_length) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/shoes/download.rb', line 103

def download_started(content_length)
  @length = content_length
  @content_length = content_length
  @percent = 0.0
  @started = true
  eval_block(@opts[:start], self) if @opts[:start]
  #puts "download started #{@length} bytes"
end

#eval_block(blk, result) ⇒ Object



91
92
93
94
# File 'lib/shoes/download.rb', line 91

def eval_block(blk, result)
  blk.call result
  # @gui.eval_block(blk, result)
end

#finish_download(f) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/shoes/download.rb', line 69

def finish_download(f)
  puts "download method finishing"
  @finished = true
  @response.body = f.read
  @response.status = f.status[0]
  @response.headers = f.meta
  #puts "Calling finishers #{f.size}"
  # The download thread needs to quit.
  #@thread.exit if @opts[:save]
  # call :progress with 100%, just in case
  @percent = 1.0
  eval_block(@opts[:progress], self) if @opts[:progress]
  save_to_file(@opts[:save]) if @opts[:save]
  # :finish and block are mutully exclusive (see manual)
  if @opts[:finish]
    eval_block(@opts[:finish], self) 
  elsif @blk
    #puts "calling download blk"
    eval_block(@blk, self)
  end
end

#progress_procObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shoes/download.rb', line 55

def progress_proc
  lambda do |size|
    # size is number of bytes xferred, so far.
    if size > 0
#        if (size - self.transferred) > (content_length / UPDATE_STEPS) #&& !@gui.busy?
      #@gui.busy = true
      @percent = size.to_f / @length.to_f
      eval_block(@opts[:progress], self)
      sleep @opts[:pause] if @opts[:pause]
      @transferred = size
    end
  end
end

#save_to_file(str) ⇒ Object



96
97
98
99
100
101
# File 'lib/shoes/download.rb', line 96

def save_to_file(str)
  #puts "Saving to #{str}"
  @outf = open(str, 'wb')  
  @outf.print(@response.body)
  @outf.close
end

#start_download(url) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shoes/download.rb', line 27

def start_download(url)
  puts "download method: starting for #{url}"
  #require 'open-uri'
  @thread = Thread.new do
    uri_opts = {}
    uri_opts[:content_length_proc] = content_length_proc
    uri_opts[:progress_proc] = progress_proc if @opts[:progress]
    uri_opts[:redirect_to_https] = true
    #uri_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
    uri_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_PEER
    if RUBY_PLATFORM =~ /mingw|darwin/
      uri_opts[:ssl_ca_cert]  = File.join(DIR, "lib/shoes/cacert.pem")
    end
    open url, uri_opts do |f|
      # everything has been downloaded at this point. f is a tempfile
      finish_download f
      @thread.join
    end
  end
end