Sha256: 83f6a0fb6aa8c8b6cdf57ac7a05ac00e8ae9076a13f35161739a9bfe5d4db395

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

module Hobo
  module Helper
    def http_download url, target_file, opts = {}
      require 'net/http'
      require 'uri'
      opts = { :progress => Hobo.method(:progress) }.merge(opts)
      uri = URI.parse(url)
      size = 0
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = uri.scheme == 'https'

      # TODO: May want to verify SSL validity...
      # http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/#comment-22252
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE

      http.start do
        begin
          file = open(target_file, 'wb+')
          http.request_get(uri.path) do |response|
            size = response.content_length
            response.read_body do |chunk|
              file.write(chunk)
              opts[:progress].call(
                target_file,
                chunk.length,
                size,
                :update
              ) if opts[:progress]
            end
          end
        ensure
          opts[:progress].call(target_file, 0, size, :finsh) if opts[:progress]
          file.close
        end
      end
    end
  end
end

include Hobo::Helper

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
hobo-inviqa-0.0.15 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.14 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.13 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.11 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc11 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc10 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc9 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc8 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc7 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc6 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc4 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.10.pre.rc2 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.9 lib/hobo/helper/http_download.rb
hobo-inviqa-0.0.9.pre.rc2 lib/hobo/helper/http_download.rb