]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/commitdiff
Clean up the code for UriUtilities.
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 25 Jan 2011 02:17:05 +0000 (21:17 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 25 Jan 2011 02:17:05 +0000 (21:17 -0500)
src/uri_utilities.rb

index dcff2fe11c4ed330cf8ada0eb5df5f8d4acad371..78966e81cc7b00c30ecb51720dd52dba17335ba4 100644 (file)
@@ -25,6 +25,23 @@ require 'vendor/ruby-progressbar/progressbar'
 # for trouble.
 class UriUtilities
 
 # for trouble.
 class UriUtilities
 
+  def initialize()
+    @pbar = nil
+  end
+
+  def content_length_callback(content_length)
+    if content_length && (0 < content_length)
+      @pbar = ProgressBar.new("Download", content_length)
+      @pbar.instance_eval { @bar_mark = '=' }
+      @pbar.file_transfer_mode
+    end
+  end
+
+  def progress_callback(size)
+    @pbar.set(size) if @pbar
+  end
+
+
   # Download the given URI object to <outfile_name>.
   # Should use the progress_proc parameter to show
   # a progress bar using the Ruby/ProgressBar library.
   # Download the given URI object to <outfile_name>.
   # Should use the progress_proc parameter to show
   # a progress bar using the Ruby/ProgressBar library.
@@ -33,18 +50,18 @@ class UriUtilities
     # we can clean up afterwards in case of an error.
     begin
       File.open(outfile_name, 'wb') do |outfile|
     # we can clean up afterwards in case of an error.
     begin
       File.open(outfile_name, 'wb') do |outfile|
-        pbar = nil
-        uri.open({:content_length_proc => lambda {|content_length|
-                   if content_length && (0 < content_length)
-                     pbar = ProgressBar.new("Download", content_length)
-                     pbar.instance_eval { @bar_mark = '=' }
-                     pbar.file_transfer_mode
-                   end
-                 },
-                 :progress_proc => lambda {|size|
-                   pbar.set(size) if pbar
-                   }
-                 }.merge!(headers)) do |video_file|
+        # Convert our methods to Proc objects.
+        content_length_callback = method(:content_length_callback)
+        progress_callback = method(:progress_callback)
+
+        # So that they can be passed as arguments to open().
+        open_params = { :content_length_proc => content_length_callback,
+                        :progress_proc => progress_callback}
+
+        # Add the headers as additional parameters to the open() call.
+        open_params.merge!(headers)
+
+        uri.open(open_params) do |video_file|
           outfile.write(video_file.read)
         end
       end
           outfile.write(video_file.read)
         end
       end