From: Quintus Date: 2013-05-14T04:04:08+09:00 Subject: Re: How do I use :progress_proc from OpenURI::OpenRead? --Sig_/4SEw8/9LGbRU8/piaZEhfIQ Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Am Mon, 13 May 2013 01:28:32 +0900 schrieb "Rafal C." : > I found OpenURI::OpenRead :progress_proc and it seems like it's > exactly what I want. I'm not too sure how to implement it though. > Also, does it use ruby-progressbar? It doesn=E2=80=99t, ruby-progressbar is just a usage example. You are free = to do whatever you want in the :progress_proc and :content_length_proc callbacks. > So, all I want from it is some kind of text-based progress bar while > the file is being downloaded. Is :progress_proc the thing I need to > use to do that? The documentation sadly misses a complete working example to demonstrate the usage, so I=E2=80=99ll just post one here. The following co= de downloads the the Ruby 2.0 sourcecode archive in a hopefully clean matter (apart from the fact that the API of open-uri=E2=80=99s #open method looks confusing once you use the callback parameters): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # -*- coding: utf-8 -*- require "open-uri" TARGET =3D "http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p0.tar.bz2" bytes_total =3D nil open(TARGET, "rb", :content_length_proc =3D> lambda{|content_length| bytes_total =3D content_length}, :progress_proc =3D> lambda{|bytes_transferred| if bytes_total # Print progress print("\r#{bytes_transferred}/#{bytes_total}") else # We don=E2=80=99t know how much we get, so just print number # of transferred bytes print("\r#{bytes_transferred} (total size unknown)") end }) do |page| # Now the real operation File.open("ruby-2.0.0.tar-bz2", "wb") do |file| # The file may not fit into RAM entirely, so copy it # chunk by chunk. while chunk =3D page.read(1024) file.write(chunk) end end end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The above example just prints out the number of already transferred bytes together with the number of total bytes, but you can of course convert the numbers to MiB, GiB, etc., calculate a percentage value or display a progress bar. \r is your friend to overwrite the current line on the standard output (but remember to terminate with \n once you=E2=80=99re done). > Thanks. Vale, Marvin. --=20 Blog: http://pegasus-alpha.eu/blog ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign () - Stoppt HTML-E-Mail /\ | - Against HTML E-Mail /\ - Stoppt propriet=C3=A4re Anh=C3=A4nge | - Against proprietary attachm= ents www.asciiribbon.org/index-de.html | www.asciiribbon.org --Sig_/4SEw8/9LGbRU8/piaZEhfIQ Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iQEcBAEBAgAGBQJRkTkgAAoJELh1XLHFkqhaopIH/AucXhZv8y4xU76uhlQH0TIc 64W/QKzZlgYmH1t9DFp0yvcEYuTV2xLT41vS5UhkIewlrH9AgADlWUhs85c0zB/9 vpmjgOh9e33ajWDySRVdutex3ya6y8MjGTNrX3eJshKVHiApD66N7WL1eFRsYPZj +gwzfs5uEYw3qzQnmNPY5Hi+7At21AGl3lamyl1ZrPKmUlbtv9cS5FqqyRiCdvJT jgKWqD+dxjuiaJdfoPZtshzNHim/iXXR5QhkvkNdDyv1GZPL7F9v4vsqGt5X+OJN lq8fBtbHmMAXQ3eElEz2t2wg6IyT/nAM3+LiFwbk+GK9cJ2If1IL7ke/qGt0gnE= =OyEw -----END PGP SIGNATURE----- --Sig_/4SEw8/9LGbRU8/piaZEhfIQ--