hobbit-contrib 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 180341ee21296f6323f8956a60fed82f0d910c2a
4
- data.tar.gz: 2bb527f0d3aefb60dd4ef4fbbd0a1ba80ac277da
3
+ metadata.gz: 5d5577a3a973a53f5d608319d42f2f7bbe764277
4
+ data.tar.gz: 3571ca057d566d5c618bca565cb11a255fe0bba8
5
5
  SHA512:
6
- metadata.gz: 0339d6abdfe5e0fb02286798bd048e1ce42d64945ebd7221091223345371a69585e676a7d7d131d1f82a681a681654b565a828b087a66c6f74d1f0d5f0993043
7
- data.tar.gz: e3dcedd00cac9be0677021242524b7657671638f3f2d38faba2148512a6a71974e5c8cebfbe46fe03fd8845494371552b1d285d63b88ecca6698fc0d2a4b4cfb
6
+ metadata.gz: 9d2c7435085e2359a06036dc9953382644398f59b865ed5a667f55081853873aca86609a16053655a2fb70c029109e0d339720f5278fe973dd623f3e7cdbd58d
7
+ data.tar.gz: fa1a182827742a465144c88e9c2e0a0bf8c64481ac8c49e9255452f63f2f2ffd5c9d150c6e3d648929ea7fa7951b813c014e88243e149aa26176858703179889
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.4.0
2
+
3
+ * Fix `Hobbit::Filter`. Now it works with `halt`, introduced in `hobbit` 0.4.0.
4
+
1
5
  # 0.3.0
2
6
 
3
7
  * Fix `hobbit-contrib.gemspec`: add `erubis` and remove redundant dependencies.
@@ -1,5 +1,5 @@
1
1
  module Hobbit
2
2
  module Contrib
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -13,7 +13,7 @@ module Hobbit
13
13
  def _call(env)
14
14
  super
15
15
  rescue *self.class.errors.keys => e
16
- rescued = self.class.errors.keys.detect { |k| e.kind_of?(k)}
16
+ rescued = self.class.errors.keys.detect { |k| e.kind_of?(k) }
17
17
 
18
18
  body = instance_eval { self.class.errors[rescued].call(e) }
19
19
  response.body = [body]
data/lib/hobbit/filter.rb CHANGED
@@ -2,7 +2,7 @@ module Hobbit
2
2
  module Filter
3
3
  module ClassMethods
4
4
  %w(after before).each do |kind|
5
- define_method(kind) { |path = '', &block| filters[kind.to_sym] << compile_filter(path, &block) }
5
+ define_method(kind) { |path = '/', &block| filters[kind.to_sym] << compile_filter(path, &block) }
6
6
  end
7
7
 
8
8
  def filters
@@ -29,14 +29,21 @@ module Hobbit
29
29
  @env = env
30
30
  @request = Rack::Request.new(@env)
31
31
  @response = Hobbit::Response.new
32
- filter :before
33
- unless @response.status == 302
34
- super
35
- filter :after
32
+ catch :halt do
33
+ filter :before
34
+ unless @response.status == 302
35
+ super
36
+ filter :after unless @halted
37
+ end
36
38
  end
37
39
  @response.finish
38
40
  end
39
41
 
42
+ def halt(status, headers: {}, body: [])
43
+ @halted = true
44
+ super
45
+ end
46
+
40
47
  def self.included(othermod)
41
48
  othermod.extend ClassMethods
42
49
  end
@@ -44,14 +51,25 @@ module Hobbit
44
51
  private
45
52
 
46
53
  def filter(kind)
47
- filter = self.class.filters[kind].detect { |f| f[:compiled_path] =~ request.path_info || f[:path] =~ // }
54
+ filter = find_filter(kind)
55
+ if filter
56
+ instance_eval(&filter[:block])
57
+ end
58
+ end
59
+
60
+ def find_filter(kind)
61
+ filter = self.class.filters[kind].detect do |f|
62
+ f[:compiled_path] =~ request.path_info
63
+ end
64
+
48
65
  if filter
49
66
  $~.captures.each_with_index do |value, index|
50
67
  param = filter[:extra_params][index]
51
68
  request.params[param] = value
52
69
  end
53
- instance_eval(&filter[:block])
54
70
  end
71
+
72
+ filter
55
73
  end
56
74
  end
57
75
  end
data/spec/filter_spec.rb CHANGED
@@ -170,4 +170,75 @@ EOS
170
170
  last_response.body.must_match /goodbye world/
171
171
  end
172
172
  end
173
+
174
+ describe 'when halting in a before filter' do
175
+ let :app do
176
+ mock_app do
177
+ include Hobbit::Filter
178
+
179
+ before do
180
+ halt 401
181
+ end
182
+
183
+ get '/' do
184
+ 'hello world'
185
+ end
186
+ end
187
+ end
188
+
189
+ it 'wont execute the route' do
190
+ get '/'
191
+ last_response.status.must_equal 401
192
+ last_response.body.must_be :empty?
193
+ end
194
+ end
195
+
196
+ describe 'when halting in a route' do
197
+ let :app do
198
+ mock_app do
199
+ include Hobbit::Filter
200
+
201
+ before do
202
+ response.headers['Content-Type'] = 'text/plain'
203
+ end
204
+
205
+ after do
206
+ response.headers['Content-Type'] = 'application/json'
207
+ end
208
+
209
+ get '/' do
210
+ halt 401, body: 'Unauthenticated'
211
+ end
212
+ end
213
+ end
214
+
215
+ it 'wont execute the after filter' do
216
+ get '/'
217
+ last_response.status.must_equal 401
218
+ last_response.headers.wont_include 'Content-Type'
219
+ last_response.body.must_equal 'Unauthenticated'
220
+ end
221
+ end
222
+
223
+ describe 'when halting in an after filter' do
224
+ let :app do
225
+ mock_app do
226
+ include Hobbit::Filter
227
+
228
+ after do
229
+ halt 401
230
+ end
231
+
232
+ get '/' do
233
+ 'hello world'
234
+ end
235
+ end
236
+ end
237
+
238
+ it 'wont execute the route' do
239
+ get '/'
240
+ last_response.status.must_equal 401
241
+ last_response.body.must_be :empty?
242
+ end
243
+ end
173
244
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobbit-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Mac Adden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-29 00:00:00.000000000 Z
11
+ date: 2014-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: '0'
176
176
  requirements: []
177
177
  rubyforge_project:
178
- rubygems_version: 2.2.0
178
+ rubygems_version: 2.2.1
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: Contributed Hobbit extensions