Class: SpeedStream
- Inherits:
-
Object
- Object
- SpeedStream
- Defined in:
- lib/speed_stream.rb
Direct Known Subclasses
Constant Summary collapse
- USER_AGENT =
%{Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3}- BUF_SIZE =
1024
Instance Attribute Summary collapse
-
#bytes_per_conn ⇒ Object
readonly
Returns the value of attribute bytes_per_conn.
-
#concurrent_connections ⇒ Object
readonly
Returns the value of attribute concurrent_connections.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#file_handle ⇒ Object
readonly
Returns the value of attribute file_handle.
-
#on_progress ⇒ Object
Returns the value of attribute on_progress.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #download! ⇒ Object
-
#initialize(uri, output_file) ⇒ SpeedStream
constructor
A new instance of SpeedStream.
Constructor Details
#initialize(uri, output_file) ⇒ SpeedStream
Returns a new instance of SpeedStream.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/speed_stream.rb', line 16 def initialize(uri, output_file) @uri = URI.parse(uri) @output_file = output_file @write_mutex = Mutex.new @finished = [] @bytes_per_conn = 200_000 @concurrent_connections = 15 @cookies = {} @extra_file_length_headers = {'Range' => 'bytes=1-'} end |
Instance Attribute Details
#bytes_per_conn ⇒ Object (readonly)
Returns the value of attribute bytes_per_conn.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def bytes_per_conn @bytes_per_conn end |
#concurrent_connections ⇒ Object (readonly)
Returns the value of attribute concurrent_connections.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def concurrent_connections @concurrent_connections end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def @cookies end |
#file_handle ⇒ Object (readonly)
Returns the value of attribute file_handle.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def file_handle @file_handle end |
#on_progress ⇒ Object
Returns the value of attribute on_progress.
14 15 16 |
# File 'lib/speed_stream.rb', line 14 def on_progress @on_progress end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def output_file @output_file end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
13 14 15 |
# File 'lib/speed_stream.rb', line 13 def uri @uri end |
Instance Method Details
#download! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/speed_stream.rb', line 27 def download! @start_time = Time.now @bytes_written = 0 pool = ThreadPool.new(concurrent_connections, 999999) File.open(output_file, "wb") do |f| @file_handle = f begin ranges.each do |range| pool.add_work(range) do |drange| download_range drange end end ensure pool.shutdown end end end |