OpenURI open
returns an object that provides HTTP response headers, so you can get the expected number of bytes from the Content-Length header and compare it with the return value of IO.copy_stream
:
require 'open-uri' download = open 'http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png' bytes_expected = download.meta['content-length'] bytes_copied = IO.copy_stream download, 'image.png' if bytes_expected != bytes_copied raise "Expected #{bytes_expected} bytes but got #{bytes_copied}" end
It would be surprising if open
run without errors, and this check still failed, but you never know.
source share