Class: PackerFiles::Utils::FastestURL
- Inherits:
-
Object
- Object
- PackerFiles::Utils::FastestURL
- Defined in:
- lib/PackerFiles/Utils/FastestURL.rb
Overview
Given a list of URL, returns the fastest URL (to access) among them. This is done by launching several threads in parallel and estimating the time taken to access the contents of these URLs.
Constant Summary collapse
- @@error =
Error URL
25000
Instance Method Summary collapse
-
#best_urls(url_list) ⇒ Object
Returns URLs sorted in terms of best access times from a list of input URLs.
-
#initialize(slow_limit = 0.5, trials = 1) ⇒ FastestURL
constructor
Creates a new object of type FastestURL.
Constructor Details
#initialize(slow_limit = 0.5, trials = 1) ⇒ FastestURL
Creates a new object of type FastestURL. slow_limit is used for signalling the response time (in seconds) of a web URL to be categorized as slow. Trials specifies the # of attempts the web URLs must be opened and closed.
22 23 24 25 |
# File 'lib/PackerFiles/Utils/FastestURL.rb', line 22 def initialize(slow_limit = 0.5 , trials = 1) @limit = slow_limit @trials = trials end |
Instance Method Details
#best_urls(url_list) ⇒ Object
Returns URLs sorted in terms of best access times from a list of input URLs.
29 30 31 32 33 34 35 36 37 |
# File 'lib/PackerFiles/Utils/FastestURL.rb', line 29 def best_urls(url_list) threads = url_list.map { |h| Thread.new {speed_trials(h) }} speeds = threads.map { |t| t.join; t.value } hash = Hash.new url_list.each_with_index do |url, index| hash[url] = speeds.at(index) if speeds.at(index) != @@error end hash.sort_by {|url, speed| speed} end |