Method: TreasureData::Updater::ModuleDefinition#stream_fetch

Defined in:
lib/td/updater.rb

#stream_fetch(url, binfile, &progress) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/td/updater.rb', line 235

def stream_fetch(url, binfile, &progress)
  require 'net/http'
  require 'openssl'

  uri = URI(url)
  http_class = Command.get_http_class
  http = http_class.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  http.request_get(uri.path + (uri.query ? '?' + uri.query : '')) {|response|
    if response.class == Net::HTTPOK
      # $stdout.print a . every tick_period seconds
      response.read_body do |chunk|
        binfile.write chunk
        progress.call unless progress.nil?
      end
      return true
    elsif response.is_a?(Net::HTTPRedirection)
      unless ENV['TD_TOOLBELT_DEBUG'].nil?
        $stdout.puts "redirect '#{url}' to '#{response['Location']}'... "
      end
      return stream_fetch(response['Location'], binfile, &progress)
    else
      raise_error "An error occurred when fetching from '#{uri}' " +
        "(#{response.class.to_s}: #{response.message})."
      return false
    end
  }
end