Class: EpubForge::Utils::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/epubforge/utils/downloader.rb

Instance Method Summary collapse

Instance Method Details

#download(url, io) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epubforge/utils/downloader.rb', line 6

def download( url, io )
  @uri = URI.parse( url )
  @io  = io
  
  open( url ) do |f|
    @io << f.read
  end
  
  # @io << Net::HTTP.get( @uri )
  
  # Net::HTTP.start( @uri.host, @uri.port ) do |http| 
  #   http.request_get( @uri.path ) do |request| 
  #     request.read_body do |seg|
  #       puts "==============================  #{seg} ============================="
  #       io << seg
  #       #hack -- adjust to suit:
  #       sleep 0.005 
  #     end
  #   end
  # end
rescue Exception => e
  handle_network_errors( e )
end

#handle_network_errors(e) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/epubforge/utils/downloader.rb', line 30

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