hobbit 0.4.3 → 0.4.4

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: fc695229b6dbea81ceaf3de386769ca9dff5ded5
4
- data.tar.gz: f5444f4ca2eeb0edd6929bfe7058476a75690972
3
+ metadata.gz: 0ad21a4064eb73894490f6439b24759e4e205152
4
+ data.tar.gz: a39b6f89449f3f610b8ea689a9be411640491834
5
5
  SHA512:
6
- metadata.gz: 6c7d75254bd0ae03006229ed7f665548eb7a5557dbdc0a866910d4ebf7aa9840b7bb2dcc93ec15d2f839219310ffc0e14b4ef2a7ac4b1449fc0a4b2ec0098af6
7
- data.tar.gz: 4e5519917bbee84db20e22de8208e9776ec5bdeaef48b88c03f0158151373029ed695e015df5476e9ed34ca6e63b4dc1077bbdd6ab209949d8ed08c678643ca3
6
+ metadata.gz: f0d3165b578580625915845cc1c681c03edfcc6a9ab8aae7a8ef9cc009152fc533592be8479741f628ba65970562e9210cb771bd1c313fae13f309b7f29f7b17
7
+ data.tar.gz: f0ae8fb5175333c1bb06484141e0609cb34c0340315cef48c95fb79227529284cf657d15eabe49ce4b23d9cb63055791f70f6f4ba1754a42e0e0058f9d598228
@@ -1,3 +1,7 @@
1
+ # 0.4.4
2
+
3
+ * Refactor `Hobbit::Response`.
4
+
1
5
  # 0.4.3
2
6
 
3
7
  * Calculate the `Content-Length` of a `Hobbit::Response` using `#bytesize`
@@ -7,11 +7,20 @@ module Hobbit
7
7
  def_delegators :headers, :[], :[]=
8
8
 
9
9
  def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' })
10
- @body, @headers, @status = body, headers, status
10
+ @body, @headers, @status = [], headers, status
11
+ @length = 0
12
+
13
+ if body.respond_to? :to_str
14
+ write body.to_str
15
+ elsif body.respond_to? :each
16
+ body.each { |i| write i.to_s }
17
+ else
18
+ raise TypeError, 'body must #respond_to? :to_str or :each'
19
+ end
11
20
  end
12
21
 
13
22
  def finish
14
- headers['Content-Length'] = body.each.map { |i| i.is_a?(Array) ? i.map(&:to_s).map(&:bytesize) : i.bytesize }.flatten.inject(0, &:+).to_s
23
+ headers['Content-Length'] = @length.to_s
15
24
  [status, headers, body]
16
25
  end
17
26
 
@@ -21,7 +30,10 @@ module Hobbit
21
30
  end
22
31
 
23
32
  def write(string)
24
- body << string
33
+ s = string.to_s
34
+ @length += s.bytesize
35
+
36
+ body << s
25
37
  end
26
38
  end
27
39
  end
@@ -1,3 +1,3 @@
1
1
  module Hobbit
2
- VERSION = '0.4.3'
2
+ VERSION = '0.4.4'
3
3
  end
@@ -11,12 +11,16 @@ describe Hobbit::Response do
11
11
  end
12
12
 
13
13
  it 'must set the body, status and headers with arguments given' do
14
- status, headers, body = 200, { 'Content-Type' => 'application/json' }, ['{"name":"Hobbit"}']
14
+ status, headers, body = 200, { 'Content-Type' => 'application/json' }, ['{"name": "Hobbit"}']
15
15
  response = Hobbit::Response.new body, status, headers
16
16
  response.status.must_equal status
17
17
  response.headers.must_equal headers
18
18
  response.body.must_equal body
19
19
  end
20
+
21
+ it 'must raise a TypeError if body does not respond to :to_str or :each' do
22
+ proc { Hobbit::Response.new 1 }.must_raise TypeError
23
+ end
20
24
  end
21
25
 
22
26
  describe '#[]' do
@@ -48,7 +52,7 @@ describe Hobbit::Response do
48
52
  describe '#finish' do
49
53
  let(:status) { 200 }
50
54
  let(:headers) { { 'Content-Type' => 'application/json' } }
51
- let(:body) { ['{"name":"Hobbit"}'] }
55
+ let(:body) { ['{"name": "Hobbit"}'] }
52
56
 
53
57
  it 'must return a 3 elements array with status, headers and body' do
54
58
  response = Hobbit::Response.new body, status, headers
@@ -59,7 +63,7 @@ describe Hobbit::Response do
59
63
  response = Hobbit::Response.new body, status, headers
60
64
  s, h, b = response.finish
61
65
  h.must_include 'Content-Length'
62
- h['Content-Length'].must_equal '17'
66
+ h['Content-Length'].must_equal '18'
63
67
  end
64
68
 
65
69
  it 'must calculate the Content-Length of the body, even if the body is empty' do
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.3
4
+ version: 0.4.4
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-02-08 00:00:00.000000000 Z
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler