vendor/bundle/ruby/1.8/gems/io-reactor-1.0.4/rake/helpers.rb
### Download the file at +sourceuri+ via HTTP and write it to +targetfile+.
Download the file at +sourceuri+ via HTTP and write it to +targetfile+.
Download the file at + sourceuri + via HTTP and write it to + targetfile + .
def download( sourceuri, targetfile=nil )
	oldsync = $defout.sync
	$defout.sync = true
	require 'net/http'
	require 'uri'
	targetpath = Pathname.new( targetfile )
	log "Downloading %s to %s" % [sourceuri, targetfile]
	targetpath.open( File::WRONLY|File::TRUNC|File::CREAT, 0644 ) do |ofh|
		url = sourceuri.is_a?( URI ) ? sourceuri : URI.parse( sourceuri )
		downloaded = false
		limit = 5
		until downloaded or limit.zero?
			Net::HTTP.start( url.host, url.port ) do |http|
				req = Net::HTTP::Get.new( url.path )
				http.request( req ) do |res|
					if res.is_a?( Net::HTTPSuccess )
						log "Downloading..."
						res.read_body do |buf|
							ofh.print( buf )
						end
						downloaded = true
						puts "done."
					elsif res.is_a?( Net::HTTPRedirection )
						url = URI.parse( res['location'] )
						log "...following redirection to: %s" % [ url ]
						limit -= 1
						sleep 0.2
						next
					else
						res.error!
					end
				end
			end
		end
	end
	return targetpath
ensure
	$defout.sync = oldsync
end
def download ( sourceuri , targetfile = nil ) oldsync = $defout . sync $defout . sync = true require 'net/http' require 'uri' targetpath = Pathname . new ( targetfile ) log "Downloading %s to %s" % [ sourceuri , targetfile ] targetpath . open ( File :: WRONLY | File :: TRUNC | File :: CREAT , 0644 ) do | ofh | url = sourceuri . is_a? ( URI ) ? sourceuri : URI . parse ( sourceuri ) downloaded = false limit = 5 until downloaded or limit . zero? Net :: HTTP . start ( url . host , url . port ) do | http | req = Net :: HTTP :: Get . new ( url . path ) http . request ( req ) do | res | if res . is_a? ( Net :: HTTPSuccess ) log "Downloading..." res . read_body do | buf | ofh . print ( buf ) end downloaded = true puts "done." elsif res . is_a? ( Net :: HTTPRedirection ) url = URI . parse ( res [ 'location' ] ) log "...following redirection to: %s" % [ url ] limit -= 1 sleep 0.2 next else res . error! end end end end end return targetpath ensure $defout . sync = oldsync end
Download the file at +sourceuri+ via HTTP and write it to +targetfile+.
Download the file at + sourceuri + via HTTP and write it to + targetfile + .
sourceuri
targetfile