hobbit 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +6 -6
- data/lib/hobbit/base.rb +4 -10
- data/lib/hobbit/response.rb +3 -2
- data/lib/hobbit/version.rb +1 -1
- data/spec/base_spec.rb +21 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 944ab35171d45442e32e96a5587753f37ed50f0d
|
4
|
+
data.tar.gz: c1e65e014055647553c5eaf4e9c055f8cb43afe5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b5d57ba581c5676c1670f8eb6cece3d7442cf0a9d286d8848dc7726b806a493217c0ed8de6133da3c8596156e40f08e23bef60cab889c2915b1dd36972bd362
|
7
|
+
data.tar.gz: 995bf2aeb75e3a5f6116aad7715d5d4cffd92f84d0bd1cc58c9a36593ee604c678487f313f8912aae68b66a80a60db18c15951743391f9e28322a3848a914095
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# 0.5.0
|
2
|
+
|
3
|
+
* Refactor `Hobbit::Base#halt`. It now sets the status, merges the headers and
|
4
|
+
writes the body (using `Hobbit::Response#write`) when given a fixnum, a hash or
|
5
|
+
a string.
|
6
|
+
* `Hobbit::Response` headers and body are not accessors anymore. This is
|
7
|
+
because when you set the body directly, the `Content-Length` is not calculated
|
8
|
+
(it's calculated on `#write`).
|
9
|
+
|
1
10
|
# 0.4.4
|
2
11
|
|
3
12
|
* Refactor `Hobbit::Response`.
|
data/README.md
CHANGED
@@ -176,8 +176,8 @@ end
|
|
176
176
|
|
177
177
|
#### Halting
|
178
178
|
|
179
|
-
To immediately stop a request within
|
180
|
-
status:
|
179
|
+
To immediately stop a request within route you can use `halt`. You can also
|
180
|
+
specify a status:
|
181
181
|
|
182
182
|
```ruby
|
183
183
|
require 'hobbit'
|
@@ -195,7 +195,7 @@ class App < Hobbit::Base
|
|
195
195
|
end
|
196
196
|
```
|
197
197
|
|
198
|
-
|
198
|
+
Or body:
|
199
199
|
|
200
200
|
```ruby
|
201
201
|
require 'hobbit'
|
@@ -208,7 +208,7 @@ class App < Hobbit::Base
|
|
208
208
|
end
|
209
209
|
|
210
210
|
get '/' do
|
211
|
-
halt 401,
|
211
|
+
halt 401, 'This will be the body' unless session['user_id']
|
212
212
|
end
|
213
213
|
end
|
214
214
|
```
|
@@ -226,7 +226,7 @@ class App < Hobbit::Base
|
|
226
226
|
end
|
227
227
|
|
228
228
|
get '/' do
|
229
|
-
halt 401,
|
229
|
+
halt 401, { 'Content-Type' => 'text/html; charset=utf-8' }
|
230
230
|
end
|
231
231
|
end
|
232
232
|
```
|
@@ -244,7 +244,7 @@ class App < Hobbit::Base
|
|
244
244
|
end
|
245
245
|
|
246
246
|
get '/' do
|
247
|
-
halt 401,
|
247
|
+
halt 401, { 'Content-Type' => 'text/html; charset=utf-8' }, 'Woops'
|
248
248
|
end
|
249
249
|
end
|
250
250
|
```
|
data/lib/hobbit/base.rb
CHANGED
@@ -55,16 +55,10 @@ module Hobbit
|
|
55
55
|
@response.finish
|
56
56
|
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
# @param body [Array] Response body.
|
63
|
-
# @return [Hobbit::Response] The Response.
|
64
|
-
def halt(status, headers: {}, body: [])
|
65
|
-
response.status = status
|
66
|
-
response.headers = headers
|
67
|
-
response.body = Array(body)
|
58
|
+
def halt(*res)
|
59
|
+
response.status = res.detect { |s| s.is_a? Fixnum } || 200
|
60
|
+
response.headers.merge! res.detect { |h| h.is_a? Hash } || {}
|
61
|
+
response.write res.detect { |b| b.is_a? String } || ''
|
68
62
|
|
69
63
|
throw :halt, response
|
70
64
|
end
|
data/lib/hobbit/response.rb
CHANGED
@@ -2,7 +2,8 @@ require 'forwardable'
|
|
2
2
|
|
3
3
|
module Hobbit
|
4
4
|
class Response
|
5
|
-
attr_accessor :
|
5
|
+
attr_accessor :status
|
6
|
+
attr_reader :headers, :body
|
6
7
|
extend Forwardable
|
7
8
|
def_delegators :headers, :[], :[]=
|
8
9
|
|
@@ -15,7 +16,7 @@ module Hobbit
|
|
15
16
|
elsif body.respond_to? :each
|
16
17
|
body.each { |i| write i.to_s }
|
17
18
|
else
|
18
|
-
raise TypeError, 'body must #respond_to?
|
19
|
+
raise TypeError, 'body must #respond_to? #to_str or #each'
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
data/lib/hobbit/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -205,61 +205,54 @@ EOS
|
|
205
205
|
end
|
206
206
|
end
|
207
207
|
|
208
|
-
describe '
|
208
|
+
describe '#halt' do
|
209
209
|
before do
|
210
210
|
mock_app do
|
211
|
-
get '/
|
211
|
+
get '/halt_fixnum' do
|
212
212
|
halt 501
|
213
213
|
response.write 'Hello world'
|
214
214
|
end
|
215
215
|
|
216
216
|
get '/halt_string' do
|
217
|
-
halt
|
218
|
-
end
|
219
|
-
|
220
|
-
get '/halt_array' do
|
221
|
-
halt 501, body: ['Halt!']
|
217
|
+
halt 'Halt!'
|
222
218
|
end
|
223
219
|
|
224
220
|
get '/halt_hash' do
|
225
|
-
halt
|
221
|
+
halt({ header: 'OK' })
|
226
222
|
end
|
227
223
|
|
228
|
-
get '/
|
229
|
-
halt
|
224
|
+
get '/halt_combined' do
|
225
|
+
halt 404, 'Not Found'
|
230
226
|
end
|
231
227
|
end
|
232
228
|
end
|
233
229
|
|
234
230
|
it 'returns the response given to halt function' do
|
235
|
-
get '/
|
236
|
-
last_response.headers.must_equal({ 'Content-Length' => '0' })
|
231
|
+
get '/halt_fixnum'
|
237
232
|
last_response.body.must_equal ''
|
233
|
+
last_response.headers.must_equal({ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => '0' })
|
238
234
|
last_response.status.must_equal 501
|
239
235
|
end
|
240
236
|
|
241
|
-
it 'accepts
|
237
|
+
it 'accepts body' do
|
242
238
|
get '/halt_string'
|
243
239
|
last_response.body.must_equal 'Halt!'
|
244
|
-
last_response.
|
245
|
-
|
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
|
240
|
+
last_response.headers.must_equal({ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => '5' })
|
241
|
+
last_response.status.must_equal 200
|
251
242
|
end
|
252
243
|
|
253
|
-
it 'accepts
|
244
|
+
it 'accepts headers' do
|
254
245
|
get '/halt_hash'
|
255
|
-
last_response.body.must_equal '
|
256
|
-
last_response.
|
246
|
+
last_response.body.must_equal ''
|
247
|
+
last_response.headers.must_equal({ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => '0', header: 'OK' })
|
248
|
+
last_response.status.must_equal 200
|
257
249
|
end
|
258
|
-
|
259
|
-
it 'accepts
|
260
|
-
get '/
|
261
|
-
last_response.
|
262
|
-
last_response.
|
250
|
+
|
251
|
+
it 'accepts combinations' do
|
252
|
+
get '/halt_combined'
|
253
|
+
last_response.body.must_equal 'Not Found'
|
254
|
+
last_response.headers.must_equal({ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => '9' })
|
255
|
+
last_response.status.must_equal 404
|
263
256
|
end
|
264
257
|
end
|
265
258
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobbit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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: 2014-
|
11
|
+
date: 2014-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|