request_interceptor 0.2.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78ccd56344e9eeee0f940e9042db9d19f26490fa
4
- data.tar.gz: 356ededd222ceac8fea4b64ae978994cef14bd6b
3
+ metadata.gz: 244056547ed0ff2bc1acaeb6fa12f6aec7eb7fb7
4
+ data.tar.gz: 8b0719757447a76119044afefb543020ed1718a8
5
5
  SHA512:
6
- metadata.gz: b12b0f6b6eb87bfbd3203c1e9087c7681be611932df6dcf1d108975a96b2277e9666b9e2e4addffb96ea82603d5cf103bd024e3d772da4f6f483da69c1c93ecd
7
- data.tar.gz: 9781bed571b57a806e658d08dd64ce4f93003130c86e4cd9d1b63085787d26b72c70017e184740492dfd7f6faae37db395f65c91074d03d08c0ea63864907542
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 arugments provided to the `.intercept` method are forwarded to the interceptor's constructor:
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 = "Hello World" # => true
120
+ response == "Hello World" # => true
121
121
  end
122
122
  ```
123
123
 
@@ -1,3 +1,3 @@
1
1
  class RequestInterceptor
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -4,7 +4,14 @@ require "net/http"
4
4
  require "rack/mock"
5
5
 
6
6
  class RequestInterceptor
7
- class Transaction < Struct.new(:request, :response); end
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
- def start
124
- @started = true
125
- return yield(self) if block_given?
126
- self
127
- end
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
- def finish
130
- @started = false
131
- nil
132
- end
155
+ def real_request(http_context, request, body, &block)
156
+ http_context.finish if restart.required?
133
157
 
134
- define_method(:request) do |request, body = nil, &block|
135
- runner.request(self, request, body, &block)
136
- end
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.0
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: 2016-05-10 00:00:00.000000000 Z
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.5.1
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: