request_interceptor 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/request_interceptor/version.rb +1 -1
- data/lib/request_interceptor.rb +41 -18
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 244056547ed0ff2bc1acaeb6fa12f6aec7eb7fb7
|
4
|
+
data.tar.gz: 8b0719757447a76119044afefb543020ed1718a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98452c1e2592dbdb2746a1eea5bf4627bd135743b0074864a1aab8b553bef146b0190950ac1d57311f492fe685697e757e24cffb5ecb8ba18c6569914c318731
|
7
|
+
data.tar.gz: 398a7146eb773b04749296906832216b93587aa05a279374df7c692b0def45a60fdbcf05b26bb0e7d88f8f32e151ad52023ae6886ef0621d766b2628412c1f87
|
data/README.md
CHANGED
@@ -91,7 +91,7 @@ These two features are only available for Sinatra based interceptors that inheri
|
|
91
91
|
|
92
92
|
### Constructor argument forwarding
|
93
93
|
|
94
|
-
Any
|
94
|
+
Any arguments provided to the `.intercept` method are forwarded to the interceptor's constructor:
|
95
95
|
|
96
96
|
```ruby
|
97
97
|
multilingual_app = RequestInterceptor.define do
|
@@ -117,7 +117,7 @@ end
|
|
117
117
|
|
118
118
|
multilingual_app.intercept do
|
119
119
|
response = Net::HTTP.get(URI("http://example.com/"))
|
120
|
-
response
|
120
|
+
response == "Hello World" # => true
|
121
121
|
end
|
122
122
|
```
|
123
123
|
|
data/lib/request_interceptor.rb
CHANGED
@@ -4,7 +4,14 @@ require "net/http"
|
|
4
4
|
require "rack/mock"
|
5
5
|
|
6
6
|
class RequestInterceptor
|
7
|
-
|
7
|
+
Transaction = Struct.new(:request, :response)
|
8
|
+
|
9
|
+
Restart = Struct.new(:required, :instructions) do
|
10
|
+
protected :required
|
11
|
+
def required?
|
12
|
+
!!required
|
13
|
+
end
|
14
|
+
end
|
8
15
|
|
9
16
|
GET = "GET".freeze
|
10
17
|
POST = "POST".freeze
|
@@ -103,6 +110,25 @@ class RequestInterceptor
|
|
103
110
|
response
|
104
111
|
end
|
105
112
|
|
113
|
+
def start(http_context, &block)
|
114
|
+
self.restart = Restart.new(true, block)
|
115
|
+
http_context.instance_variable_set(:@started, true)
|
116
|
+
return block.call(http_context) if block
|
117
|
+
http_context
|
118
|
+
end
|
119
|
+
|
120
|
+
def finish(http_context)
|
121
|
+
http_context.instance_variable_set(:@started, false)
|
122
|
+
nil
|
123
|
+
end
|
124
|
+
|
125
|
+
protected
|
126
|
+
|
127
|
+
attr_writer :restart
|
128
|
+
|
129
|
+
def restart
|
130
|
+
@restart || Restart.new(false)
|
131
|
+
end
|
106
132
|
|
107
133
|
private
|
108
134
|
|
@@ -120,21 +146,23 @@ class RequestInterceptor
|
|
120
146
|
runner = self
|
121
147
|
|
122
148
|
Net::HTTP.class_eval do
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
149
|
+
define_method(:start) { |&block| runner.start(self, &block) }
|
150
|
+
define_method(:finish) { runner.finish(self) }
|
151
|
+
define_method(:request) { |request, body = nil, &block| runner.request(self, request, body, &block) }
|
152
|
+
end
|
153
|
+
end
|
128
154
|
|
129
|
-
|
130
|
-
|
131
|
-
nil
|
132
|
-
end
|
155
|
+
def real_request(http_context, request, body, &block)
|
156
|
+
http_context.finish if restart.required?
|
133
157
|
|
134
|
-
|
135
|
-
|
136
|
-
|
158
|
+
restore_net_http_methods(http_context)
|
159
|
+
|
160
|
+
if restart.required?
|
161
|
+
http_context.start(&restart.instructions)
|
162
|
+
self.restart = nil
|
137
163
|
end
|
164
|
+
|
165
|
+
http_context.request(request, body, &block)
|
138
166
|
end
|
139
167
|
|
140
168
|
def restore_net_http_methods(instance = nil)
|
@@ -171,11 +199,6 @@ class RequestInterceptor
|
|
171
199
|
end
|
172
200
|
end
|
173
201
|
|
174
|
-
def real_request(http_context, request, body, &block)
|
175
|
-
restore_net_http_methods(http_context)
|
176
|
-
http_context.request(request, body, &block)
|
177
|
-
end
|
178
|
-
|
179
202
|
def log_transaction(request, response)
|
180
203
|
transactions << RequestInterceptor::Transaction.new(request, response)
|
181
204
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_interceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Tennhard
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-04-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -138,9 +138,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
141
|
+
rubygems_version: 2.6.11
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Sinatra based foreign API simulation
|
145
145
|
test_files: []
|
146
|
-
has_rdoc:
|