hobbit 0.3.1 → 0.4.0

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: c718834426f243de65e59d905f8aed621ec9d3e0
4
- data.tar.gz: bea4cc760951fa709df57ae17a4db4425996b006
3
+ metadata.gz: 790649ff947ec7ea93289cc41f241ae04a2b60ea
4
+ data.tar.gz: af6820dcde77d613276b8f80a5a0bbef188c82c9
5
5
  SHA512:
6
- metadata.gz: eda1cb013991847b0a71076fbdbd00d43125d1c927fe94707f04bc8aaf3b604575d29351ff464a5ee752cc8e0e6cc4670fe2c8211eb519957ee27c55c0b8964e
7
- data.tar.gz: 81b2aea8af926f5f6cb5042e6b6d887b9614d06ddd950ecb5b2a5027324e61c658644de94975fa431109b279c5e1ef449a8e6b672b4cae768e468ed94a993695
6
+ metadata.gz: 7a6f43952d33eb86244c7345b43a6490d487a1cc6934d428ebd0316433eaa7be801f0cc05a915cce7bf797fefe1b2184058974ddd10d9ef2aadbf114c56513b2
7
+ data.tar.gz: ea115f7eeb26199d18839cdfd1f03a6e05270effce2f95710e8694ccdbd7de8e24c89c18ceffa81256931e48231084dd61ed52e934280b073d0fa2da49707c26
@@ -1,3 +1,7 @@
1
+ # 0.4.0
2
+
3
+ * Add halt method.
4
+
1
5
  # 0.3.1
2
6
 
3
7
  * Remove unused `attr_accessor` (`:length`) from `Hobbit::Response`.
data/README.md CHANGED
@@ -229,6 +229,81 @@ end
229
229
  run App.new
230
230
  ```
231
231
 
232
+ #### Halting
233
+
234
+ To immediately stop a request within a filter or route you must specify the
235
+ status:
236
+
237
+ ```ruby
238
+ require 'hobbit'
239
+
240
+ class App < Hobbit::Base
241
+ use Rack::Session::Cookie, secret: SecureRandom.hex(64)
242
+
243
+ def session
244
+ env['rack.session']
245
+ end
246
+
247
+ get '/' do
248
+ halt 401 unless session['user_id']
249
+ end
250
+ end
251
+ ```
252
+
253
+ And also you can add a body:
254
+
255
+ ```ruby
256
+ require 'hobbit'
257
+
258
+ class App < Hobbit::Base
259
+ use Rack::Session::Cookie, secret: SecureRandom.hex(64)
260
+
261
+ def session
262
+ env['rack.session']
263
+ end
264
+
265
+ get '/' do
266
+ halt 401, body: 'This will be the body' unless session['user_id']
267
+ end
268
+ end
269
+ ```
270
+
271
+ Or headers:
272
+
273
+ ```ruby
274
+ require 'hobbit'
275
+
276
+ class App < Hobbit::Base
277
+ use Rack::Session::Cookie, secret: SecureRandom.hex(64)
278
+
279
+ def session
280
+ env['rack.session']
281
+ end
282
+
283
+ get '/' do
284
+ halt 401, headers: { 'Content-Type' => 'text/html; charset=utf-8' }
285
+ end
286
+ end
287
+ ```
288
+
289
+ Or both:
290
+
291
+ ``` ruby
292
+ require 'hobbit'
293
+
294
+ class App < Hobbit::Base
295
+ use Rack::Session::Cookie, secret: SecureRandom.hex(64)
296
+
297
+ def session
298
+ env['rack.session']
299
+ end
300
+
301
+ get '/' do
302
+ halt 401, headers: { 'Content-Type' => 'text/html; charset=utf-8' }, body: 'Woops'
303
+ end
304
+ end
305
+ ```
306
+
232
307
  ### Security
233
308
 
234
309
  By default, Hobbit (nor Rack) comes without any protection against web
@@ -55,19 +55,47 @@ module Hobbit
55
55
  @response.finish
56
56
  end
57
57
 
58
+ # Stops the execution and return a response
59
+ #
60
+ # status | Number - HTTP status code
61
+ # body | Response body
62
+ # headers | Response headers
63
+ #
64
+ def halt(status, headers: {}, body: [])
65
+ response.status = status # Without a default, the user must know why is using halt
66
+ response.headers = headers
67
+ response.body = Array(body) # With this we support strings, arrays or hashes
68
+
69
+ throw :halt, response
70
+ end
71
+
58
72
  private
59
73
 
60
74
  def route_eval
61
- route = self.class.routes[request.request_method].detect { |r| r[:compiled_path] =~ request.path_info }
75
+ catch :halt do
76
+ if route = find_route
77
+ response.write instance_eval(&route[:block])
78
+ else
79
+ halt 404
80
+ end
81
+
82
+ response
83
+ end
84
+ end
85
+
86
+ def find_route
87
+ route = self.class.routes[request.request_method].detect do |r|
88
+ r[:compiled_path] =~ request.path_info
89
+ end
90
+
62
91
  if route
63
92
  $~.captures.each_with_index do |value, index|
64
93
  param = route[:extra_params][index]
65
94
  request.params[param] = value
66
95
  end
67
- response.write instance_eval(&route[:block])
68
- else
69
- response.status = 404
70
96
  end
97
+
98
+ route
71
99
  end
72
100
  end
73
101
  end
@@ -1,3 +1,3 @@
1
1
  module Hobbit
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -205,6 +205,64 @@ EOS
205
205
  end
206
206
  end
207
207
 
208
+ describe '::halt' do
209
+ before do
210
+ mock_app do
211
+ get '/halt' do
212
+ halt 501
213
+ response.write 'Hello world'
214
+ end
215
+
216
+ get '/halt_string' do
217
+ halt 501, body: 'Halt!'
218
+ end
219
+
220
+ get '/halt_array' do
221
+ halt 501, body: ['Halt!']
222
+ end
223
+
224
+ get '/halt_hash' do
225
+ halt 501, body: { message: 'Halt!' }
226
+ end
227
+
228
+ get '/halt_headers' do
229
+ halt 501, headers: { header: 'OK' }
230
+ end
231
+ end
232
+ end
233
+
234
+ it 'return the response given to halt function' do
235
+ get '/halt'
236
+ last_response.headers.must_equal({ 'Content-Length' => nil })
237
+ last_response.body.must_equal ''
238
+ last_response.status.must_equal 501
239
+ end
240
+
241
+ it 'accepts a string as body' do
242
+ get '/halt_string'
243
+ last_response.body.must_equal 'Halt!'
244
+ last_response.status.must_equal 501
245
+ end
246
+
247
+ it 'accepts an Array as body' do
248
+ get '/halt_array'
249
+ last_response.body.must_equal 'Halt!'
250
+ last_response.status.must_equal 501
251
+ end
252
+
253
+ it 'accepts a Hash as body' do
254
+ get '/halt_hash'
255
+ last_response.body.must_equal '[:message, "Halt!"]'
256
+ last_response.status.must_equal 501
257
+ end
258
+
259
+ it 'accepts headers' do
260
+ get '/halt_headers'
261
+ last_response.headers.must_equal({ header: 'OK', 'Content-Length' => nil })
262
+ last_response.status.must_equal 501
263
+ end
264
+ end
265
+
208
266
  it 'must respond to call' do
209
267
  app.to_app.must_respond_to :call
210
268
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Mac Adden