parallel_http 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/parallel_http.rb +18 -23
- metadata +1 -1
data/lib/parallel_http.rb
CHANGED
@@ -1,43 +1,38 @@
|
|
1
1
|
require 'iconv'
|
2
2
|
require 'eventmachine'
|
3
3
|
require 'em-http-request'
|
4
|
-
require 'fiber'
|
5
4
|
|
6
5
|
class ParallelHttp
|
7
6
|
def self.exec requests
|
8
|
-
results = []
|
7
|
+
@@results = []
|
8
|
+
@@request_size = requests.size
|
9
9
|
if EM.reactor_running?
|
10
|
-
results = [{:error => "Have not tested this with an eventmachine reactor that is already running. Might have to change the code around a bit... I have an EM.stop in there and I know that would be bad if I shut down your reactor."}]
|
10
|
+
@@results = [{:error => "Have not tested this with an eventmachine reactor that is already running. Might have to change the code around a bit... I have an EM.stop in there and I know that would be bad if I shut down your reactor."}]
|
11
11
|
else
|
12
12
|
EM.run do
|
13
|
-
|
13
|
+
requests.each do |request|
|
14
|
+
result = self.single(request)
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
|
-
results
|
18
|
+
@@results
|
17
19
|
end
|
18
20
|
|
19
|
-
def self.
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
result = self.single(request)
|
25
|
-
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
|
26
|
-
body = ic.iconv(result.response)
|
27
|
-
results << {:id => request[:id], :response_code => result.response_header.status, :body => body}
|
28
|
-
EM.stop if request_size == results.size
|
29
|
-
end.resume
|
30
|
-
end
|
31
|
-
results
|
21
|
+
def self.exec_result id, result
|
22
|
+
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
|
23
|
+
body = ic.iconv(result.response)
|
24
|
+
@@results << {:id => id, :response_code => result.response_header.status, :body => body}
|
25
|
+
EM.stop if @@request_size == @@results.size
|
32
26
|
end
|
33
27
|
|
34
28
|
def self.single request
|
35
|
-
f = Fiber.current
|
36
29
|
# puts "making a request #{request[:url]}, #{request[:verb]}, #{request[:options]}"
|
37
30
|
http = EventMachine::HttpRequest.new(request[:url]).send(request[:verb].downcase, request[:options] || {})
|
38
|
-
http.callback
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
http.callback do
|
32
|
+
self.exec_result(request[:id], http)
|
33
|
+
end
|
34
|
+
http.errback do
|
35
|
+
self.exec_result(request[:id], http)
|
36
|
+
end
|
42
37
|
end
|
43
38
|
end
|