typhoeus 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/typhoeus.rb +1 -1
- data/lib/typhoeus/hydra.rb +25 -9
- metadata +1 -1
data/lib/typhoeus.rb
CHANGED
data/lib/typhoeus/hydra.rb
CHANGED
@@ -3,7 +3,7 @@ module Typhoeus
|
|
3
3
|
def initialize
|
4
4
|
@multi = Multi.new
|
5
5
|
@easy_pool = []
|
6
|
-
@
|
6
|
+
@stubs = []
|
7
7
|
@memoized_requests = {}
|
8
8
|
@retrieved_from_cache = {}
|
9
9
|
end
|
@@ -11,9 +11,17 @@ module Typhoeus
|
|
11
11
|
def self.hydra
|
12
12
|
@hydra ||= new
|
13
13
|
end
|
14
|
+
|
15
|
+
def self.hydra=(val)
|
16
|
+
@hydra = val
|
17
|
+
end
|
18
|
+
|
19
|
+
def clear_stubs
|
20
|
+
@stubs = []
|
21
|
+
end
|
14
22
|
|
15
23
|
def queue(request)
|
16
|
-
return if
|
24
|
+
return if assign_to_stub(request)
|
17
25
|
|
18
26
|
if request.method == :get
|
19
27
|
if @memoized_requests.has_key? request.url
|
@@ -33,7 +41,7 @@ module Typhoeus
|
|
33
41
|
end
|
34
42
|
|
35
43
|
def run
|
36
|
-
@
|
44
|
+
@stubs.each do |m|
|
37
45
|
m.requests.each do |request|
|
38
46
|
m.response.request = request
|
39
47
|
handle_request(request, m.response)
|
@@ -60,16 +68,16 @@ module Typhoeus
|
|
60
68
|
@on_complete = proc
|
61
69
|
end
|
62
70
|
|
63
|
-
def
|
64
|
-
@
|
65
|
-
@
|
71
|
+
def stub(method, url)
|
72
|
+
@stubs << HydraMock.new(url, method)
|
73
|
+
@stubs.last
|
66
74
|
end
|
67
75
|
|
68
|
-
def
|
69
|
-
m = @
|
76
|
+
def assign_to_stub(request)
|
77
|
+
m = @stubs.detect {|stub| stub.matches? request}
|
70
78
|
m && m.add_request(request)
|
71
79
|
end
|
72
|
-
private :
|
80
|
+
private :assign_to_stub
|
73
81
|
|
74
82
|
def get_from_cache_or_queue(request)
|
75
83
|
if @cache_getter
|
@@ -156,5 +164,13 @@ module Typhoeus
|
|
156
164
|
def and_return(val)
|
157
165
|
@response = val
|
158
166
|
end
|
167
|
+
|
168
|
+
def matches?(request)
|
169
|
+
if url.kind_of?(String)
|
170
|
+
request.method == method && request.url == url
|
171
|
+
else
|
172
|
+
request.method == method && url =~ request.url
|
173
|
+
end
|
174
|
+
end
|
159
175
|
end
|
160
176
|
end
|