pauldix-typhoeus 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/typhoeus/hydra.rb +32 -25
  2. metadata +1 -1
@@ -5,18 +5,24 @@ module Typhoeus
5
5
  @easy_pool = []
6
6
  @mocks = []
7
7
  @memoized_requests = {}
8
+ @retrieved_from_cache = {}
8
9
  end
9
-
10
+
10
11
  def self.hydra
11
12
  @hydra ||= new
12
13
  end
13
14
 
14
15
  def queue(request)
15
16
  return if assign_to_mock(request)
16
-
17
+
17
18
  if request.method == :get
18
19
  if @memoized_requests.has_key? request.url
19
- @memoized_requests[request.url] << request
20
+ if response = @retrieved_from_cache[request.url]
21
+ request.response = response
22
+ request.call_handlers
23
+ else
24
+ @memoized_requests[request.url] << request
25
+ end
20
26
  else
21
27
  @memoized_requests[request.url] = []
22
28
  get_from_cache_or_queue(request)
@@ -35,41 +41,42 @@ module Typhoeus
35
41
  end
36
42
  @multi.perform
37
43
  @memoized_requests = {}
44
+ @retrieved_from_cache = {}
38
45
  end
39
-
46
+
40
47
  def cache_getter(&block)
41
48
  @cache_getter = block
42
49
  end
43
-
50
+
44
51
  def cache_setter(&block)
45
52
  @cache_setter = block
46
53
  end
47
-
54
+
48
55
  def on_complete(&block)
49
56
  @on_complete = block
50
57
  end
51
-
58
+
52
59
  def on_complete=(proc)
53
60
  @on_complete = proc
54
61
  end
55
-
62
+
56
63
  def mock(method, url)
57
64
  @mocks << HydraMock.new(url, method)
58
65
  @mocks.last
59
66
  end
60
-
67
+
61
68
  def assign_to_mock(request)
62
69
  m = @mocks.detect {|mck| mck.method == request.method && mck.url == request.url}
63
70
  m && m.add_request(request)
64
71
  end
65
72
  private :assign_to_mock
66
-
73
+
67
74
  def get_from_cache_or_queue(request)
68
75
  if @cache_getter
69
76
  val = @cache_getter.call(request)
70
77
  if val
71
- request.response = val
72
- request.call_handlers
78
+ @retrieved_from_cache[request.url] = val
79
+ handle_request(request, val, false)
73
80
  else
74
81
  @multi.add(get_easy_object(request))
75
82
  end
@@ -78,7 +85,7 @@ module Typhoeus
78
85
  end
79
86
  end
80
87
  private :get_from_cache_or_queue
81
-
88
+
82
89
  def get_easy_object(request)
83
90
  easy = @easy_pool.pop || Easy.new
84
91
  easy.url = request.url
@@ -98,21 +105,21 @@ module Typhoeus
98
105
  easy
99
106
  end
100
107
  private :get_easy_object
101
-
108
+
102
109
  def release_easy_object(easy)
103
110
  easy.reset
104
111
  @easy_pool.push easy
105
112
  end
106
113
  private :release_easy_object
107
-
108
- def handle_request(request, response)
114
+
115
+ def handle_request(request, response, live_request = true)
109
116
  request.response = response
110
117
 
111
- if (request.cache_timeout && @cache_setter)
112
- @cache_setter.call(request)
113
- end
118
+ if live_request && request.cache_timeout && @cache_setter
119
+ @cache_setter.call(request)
120
+ end
114
121
  @on_complete.call(response) if @on_complete
115
-
122
+
116
123
  request.call_handlers
117
124
  if requests = @memoized_requests[request.url]
118
125
  requests.each do |r|
@@ -122,7 +129,7 @@ module Typhoeus
122
129
  end
123
130
  end
124
131
  private :handle_request
125
-
132
+
126
133
  def response_from_easy(easy, request)
127
134
  Response.new(:code => easy.response_code,
128
135
  :headers => easy.response_header,
@@ -132,20 +139,20 @@ module Typhoeus
132
139
  end
133
140
  private :response_from_easy
134
141
  end
135
-
142
+
136
143
  class HydraMock
137
144
  attr_reader :url, :method, :response, :requests
138
-
145
+
139
146
  def initialize(url, method)
140
147
  @url = url
141
148
  @method = method
142
149
  @requests = []
143
150
  end
144
-
151
+
145
152
  def add_request(request)
146
153
  @requests << request
147
154
  end
148
-
155
+
149
156
  def and_return(val)
150
157
  @response = val
151
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pauldix-typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix