Class: RankMirror::Mirrors

Inherits:
Object
  • Object
show all
Defined in:
lib/rankmirror/mirrors.rb

Instance Method Summary collapse

Constructor Details

#initialize(mirrors) ⇒ Mirrors

Returns a new instance of Mirrors.



4
5
6
# File 'lib/rankmirror/mirrors.rb', line 4

def initialize(mirrors)
	@mirrors = mirrors
end

Instance Method Details

#sort_by_speed(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rankmirror/mirrors.rb', line 8

def sort_by_speed(options)
	speed_matrix = Hash.new
	size = @mirrors.length
	jobs = Queue.new
	@mirrors.each {|i| jobs.push i}

	workers = size.times.map do
		Thread.new do 
			begin
				while x = jobs.pop(true)
					x << "/" unless x.index(/\/$/)
					uri = x + options.path + options.file
					speed = RankMirror::Speed.new(uri).get
					speed_matrix[x] = speed
				end
			rescue ThreadError
			end
		end
	end

	workers.map(&:join)

	speed_sorted = speed_matrix.values.sort.reverse
	sorted = Hash.new
	speed_sorted.each {|v| sorted[speed_matrix.key(v)] = v}
	
	return sorted
end