Module: Arfor::Download
- Defined in:
- lib/arfor/download.rb
Constant Summary collapse
- WRAP =
60- TEMP_EXT =
'.tmp'
Class Method Summary collapse
Class Method Details
.gem(gem) ⇒ Object
97 98 99 |
# File 'lib/arfor/download.rb', line 97 def self.gem(gem) system("gem fetch #{gem}") end |
.get(url) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/arfor/download.rb', line 56 def self.get(url) resolved_url, file_size = resolve_url(url) uri = URI.parse(resolved_url) target_file = File.basename(uri.path) tempfile = target_file + TEMP_EXT # check if the resolved filename exists locally - this will work even when # user hits the download url that includes ver=latest since we resolve to # the real filenam if ! File.exists?(target_file) = ProgressBar.create(:title => target_file) Net::HTTP.start(uri.host, uri.port, :use_ssl=>(uri.scheme == 'https')) do |http| request = Net::HTTP::Get.new uri http.request request do |resp| puts "Downloading: #{resolved_url}" downloaded_bytes = 0 File.open tempfile, 'wb' do |io| resp.read_body do |chunk| downloaded_bytes += chunk.size percent_complete = (downloaded_bytes.to_f / file_size.to_i) * 100 io.write chunk .progress=(percent_complete) end end end end if File.exist?(tempfile) # get to here without erroring then safe to move FileUtils.mv(tempfile, target_file) end else puts "installer for #{target_file} already downloaded" end end |
.resolve_url(url, limit = 10) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/arfor/download.rb', line 28 def self.resolve_url(url,limit = 10) url_resolved = false file_size = -1 raise ArgumentError, 'HTTP redirect too deep' if limit == 0 uri = URI.parse(url) Net::HTTP.start(uri.host, uri.port, :use_ssl=>(uri.scheme == 'https')) { |http| http.read_timeout = 1000 resp = http.head(url) file_size = resp['content-length'] case resp when Net::HTTPSuccess url_resolved = url when Net::HTTPRedirection url_resolved, file_size = resolve_url(resp['location'], limit - 1) else #resp.error! raise "Error #{resp.code} downloading #{url}" end } return url_resolved, file_size end |