lib/bukkit/download.rb in bukkit-1.1.1 vs lib/bukkit/download.rb in bukkit-2.0.0

- old
+ new

@@ -1,13 +1,32 @@ -require 'open-uri' +require 'curb' module Bukkit - def self.download(filename, uri) - begin - File.open("#{filename}", "wb") do |file| - file.write open("#{uri}").read + class Server + # Download a file from a URI. + def self.download(uri, options = {}) # Options: { :filename => "filename.ext" } + # Get the filename. If it isn't defined, derive it from the URI. + if options[:filename] + filename = options[:filename] + else + filename = uri.split("\/").last end - rescue Errno::ENOENT - abort "ERROR: No internet connection." + + # Give some friendly output. + puts "Downloading: ".yellow + filename + puts " From: ".yellow + uri + + # Download the file. + data = Curl::Easy.perform(uri) + data.follow_location = true + data.max_redirects = 8 + data.useragent = "curb" + data.perform + File.open(filename, "wb") do |file| + file.write(data.body_str) + end + # => filename.ext + + puts filename.light_green + " successfully downloaded!".green end end end \ No newline at end of file