How to download a delayed file via HTTP in Ruby?

I use the following Ruby function to download various files via HTTP:

def http_download(uri, filename)
  bytes_total = nil
  begin
    uri.open(
      read_timeout: 500,
      content_length_proc: lambda { |content_length|
        bytes_total = content_length
      },
      progress_proc: lambda { |bytes_transferred|
        if bytes_total
          print("r#{bytes_transferred} of #{bytes_total} bytes")
        else
          print("r#{bytes_transferred} bytes (total size unknown)")
        end
      }
    ) do |file|
      open filename, 'w' do |io|
        file.each_line do |line|
          io.write line
        end
      end
    end
  rescue => e
    puts e
  end
end

I also want to download files (csv, kml, zip) from this website. However, there is some kind of delay set up. When I click the download link in the browser it takes a bit until the download window appears. I suppose the file needs to be processed on the server before it can be served.

How can I modify my script to take the delay into account?


Source: ruby

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.