Class: FunWith::Files::Downloader
- Inherits:
-
Object
- Object
- FunWith::Files::Downloader
- Defined in:
- lib/fun_with/files/downloader.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#download(url, io, opts = {}) ⇒ Object
options: :md5 => <digest> : Running md5 on the downloaded file should result in an error :sha256 => <digest> :.
- #handle_network_errors(e) ⇒ Object
Class Method Details
.download(*args) ⇒ Object
11 12 13 |
# File 'lib/fun_with/files/downloader.rb', line 11 def self.download( *args ) self.new.download( *args ) end |
Instance Method Details
#download(url, io, opts = {}) ⇒ Object
options:
:md5 => <digest> : Running md5 on the downloaded file should result in an error
:sha256 => <digest> :
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fun_with/files/downloader.rb', line 21 def download( url, io, opts = {} ) @uri = URI.parse( url ) @io = io URI.open( url ) do |f| @io << f.read end io_path = @io.fwf_filepath if io_path.file? if io_path.valid_digest?( opts ) true else warn( "File may not have downloaded correctly, or is validating against a bad hash #{io_path} #{opts.inspect}") false end else warn( "File did not download correctly, or was deleted: #{io_path}") false end rescue StandardError => e handle_network_errors( e ) end |
#handle_network_errors(e) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fun_with/files/downloader.rb', line 48 def handle_network_errors( e ) raise e rescue URI::InvalidURIError => e puts "Tried to get #{@uri.path} but failed with URI::InvalidURIError." rescue OpenURI::HTTPError => e STDERR.write( "Couldn't fetch podcast info from #{@uri.path}\n" ) STDERR.write( "#{e}\n\n" ) rescue SocketError => e STDERR.write( "Problem connecting to server (Socket error) when downloading #{@uri.path}." ) STDERR.write( "#{e}\n\n" ) rescue URI::InvalidURIError => e STDERR.write( "URI::InvalidURIError for #{@uri.path}." ) STDERR.write( "#{e}\n\n" ) # this may be too broad a filter # TODO: retry? rescue SystemCallError => e STDERR.write( "Problem connecting to server (System call error) when downloading #{@uri.path}" ) STDERR.write( "#{e}\n\n" ) rescue OpenSSL::SSL::SSLError => e STDERR.write( "OpenSSL::SSL::SSLError while downloading #{@uri.path}" ) STDERR.write( "#{e}\n\n" ) rescue Timeout::Error STDERR.write( "Timeout error connecting to #{@uri.path}" ) STDERR.write( "#{e}\n\n" ) end |