-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchProducts.rb
64 lines (52 loc) · 1.43 KB
/
FetchProducts.rb
1
2
3
4
5
6
7
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require_relative "Provider/Otto";
require_relative "Provider/Armani";
require_relative "Provider/HugoBoss";
require_relative "Provider/Zalando";
require_relative "Provider/Amazon";
require_relative "Entity/Product";
require_relative "SearchProduct";
#require "rubygems";
#require "faraday";
class FetchProducts
def getProductData(id)
# search product - id
searchProductEngine = SearchProduct.new();
foundedProduct = searchProductEngine.getByGroupId(id);
if !foundedProduct then
raise 'product not found!';
end
return foundedProduct;
end
def get(id)
costumer = [
Amazon,
Otto,
Zalando,
HugoBoss,
Armani
];
foundedProduct = self.getProductData(id);
mutex = Mutex.new;
j = 0;
threads = Array.new();
while j < foundedProduct.length() do
threads2 = (1..5).map do |i|
Thread.new(i, j) do |i, j|
mutex.synchronize do
priceData = costumer[i - 1].getProduct(foundedProduct[j-1]);
if priceData then
foundedProduct[j-1].addPrice(priceData);
if (rand(0..1) === 1) then
sleep(rand(0..1))
end
end
end
end
end
threads = (threads + threads2);
j +=1
end
threads.each {|t| t.join}
return foundedProduct;
end
end