Class: Cifrado::StreamingDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/cifrado/streaming_downloader.rb

Class Method Summary collapse

Class Method Details

.get(url, output, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cifrado/streaming_downloader.rb', line 7

def self.get url, output, options = {}
  if output.nil? and !options[:stream]
    raise ArgumentError.new('Invalid output file')
  end
  uri = URI.parse url
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  copts = options[:connection_options]
  if copts[:ssl_verify_peer] == false
    Log.debug "SSL verification DISABLED"
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
  end
  #http.open_timeout = 10 # seconds
  #http.read_timeout = 10 # seconds
  Log.debug "Request URL #{uri.request_uri}"
  request = Net::HTTP::Get.new(uri.request_uri)

  headers = options[:headers]
  request.initialize_http_header headers

  rate_limit = nil
  rate_limit = Cifrado::RateLimit.new(options[:bwlimit]) if options[:bwlimit]

  unless options[:stream]
    file = File.open(output, "wb") 
    Log.debug "Downloading file to #{output}"
  end
  callback = options[:progress_callback]

  http.request(request) do |response|
    clength = response['Content-Length'].to_i
    response.read_body do |segment|
      rate_limit.limit(segment.size) if rate_limit
      callback.call(clength, segment.length, segment) if callback
      file.write(segment) if file
    end
  end
ensure
  file.close if file
end