class WWWJDic::Utils::Downloader::Downloader
rubocop:disable Style/AsciiComments
- Copyright
-
© 2012 Jon Maken
rubocop:enable Style/AsciiComments see gist.github.com/jonforums/2202048
An HTTP/HTTPS/FTP file downloader library/CLI based upon MiniPortile's HTTP implementation.
- Author
-
Jon Maken
- License
-
3-clause BSD
- Revision
-
2012-03-25 23:01:19 -0600
Constants
- VERSION
Attributes
ftp_data_chunk_size[RW]
logger[RW]
max_ca_verify_depth[RW]
Public Class Methods
download_file(url, full_path, count = 3)
click to toggle source
# File lib/wwwjdic/utils/downloader.rb, line 67 def self.download_file(url, full_path, count = 3) raise I18n.t('error.nil') if url.nil? http_download(url, full_path, count) end
Private Class Methods
http_download(url, full_path, count)
click to toggle source
# File lib/wwwjdic/utils/downloader.rb, line 83 def self.http_download(url, full_path, count) uri = URI.parse(url) begin filename = File.basename(uri.path) if ENV['HTTP_PROXY'] # protocol, userinfo, proxy_host, proxy_port = URI.split(ENV['HTTP_PROXY']) proxy_user, proxy_pass = userinfo.split(/:/) if userinfo http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_pass) else http = Net::HTTP.new(uri.host, uri.port) end http.request_get(uri) do |response| case response when Net::HTTPNotFound output '404 - Not Found' return false when Net::HTTPClientError output "Error: Client Error: #{response.inspect}" return false when Net::HTTPRedirection raise 'Too many redirections, halting.' if count <= 0 url = response['location'] return http_download(url, full_path, count - 1) when Net::HTTPOK temp_file = Tempfile.new("download-#{filename}") temp_file.binmode temp_file << response.body temp_file.close return response.body if full_path.nil? File.unlink full_path if File.exist?(full_path) FileUtils.mkdir_p File.dirname(full_path) FileUtils.mv temp_file.path, full_path, force: true else puts response end end rescue StandardError => e File.unlink full_path if !full_path.nil? && File.exist?(full_path) output "ERROR: #{e.message}" raise "Failed to download file: #{e.message}" end end
message(text)
click to toggle source
# File lib/wwwjdic/utils/downloader.rb, line 73 def self.message(text) @logger.print text @logger.flush end
output(text = '')
click to toggle source
# File lib/wwwjdic/utils/downloader.rb, line 78 def self.output(text = '') @logger.puts text @logger.flush end