How do I download a file from a link but also get the download progress in order to display it ?
From the very little I know open-uri and the typhoeus gem ( I use both ) manage the download internaly and I have no idea on how to get the download progression from them.
I suppose what I am looking for is a way of pausing the download and get the progress of update a value each time a packet is received but I do not know how to proceed.
I could also use an other gem ? If yes, which one and why ?
You need to do it as a chunked request, which is what libraries like open-uri do behind the scenes if you use their high level APIs (like open(uri).read
).
Untested example based on https://www.ruby-forum.com/topic/170237:
u = URI.parse(uri)
s = ""
progress = 0
Net::HTTP.start(u.host, u.port) do |http|
http.request_get(u.path) do |response|
response.read_body do |chunk|
s << chunk
progress += chunk.length
puts "Downloaded #{progress} so far"
end
end
end
puts "Received:\n#{s}"