hobbit 0.2.1 → 0.3.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: 672fdf5ecb22bb543a996b77a3e9075a514ea07f
4
- data.tar.gz: f6e93b013f31f18c4de763d10198f5c99fe7ec02
3
+ metadata.gz: 2a68ce23c8798644e29dae77f912930b9d862dec
4
+ data.tar.gz: 373a23fd64590f1691929adb749f7cf272789dbd
5
5
  SHA512:
6
- metadata.gz: 4b67669025634a181383d1f3b01e6ddab23d90fccc1db1ff00c5c7fdae1dc8aa94c942adcf473e06463ad02fd68b0bd81b49c4d764c3f0b3ed81118b9354b2b1
7
- data.tar.gz: fb0fe4d2c7e87d76f5adc8ab0dab1cdefd7d63b7fd987035d631a2c05fee99294c9294691eddaede31af0ccf1e0787cc63ea5b5b3bdb2440493bb6d411887b6b
6
+ metadata.gz: df6c35a1c894293ce4d6b05287b7bd7032cfe5c274fef9e20e682f09af6d2cdbd61e68918d1b8e5194715ebfad317164bf149317eb15b9bc5ed841d75d0b0eaa
7
+ data.tar.gz: f4723f82bd5e3036e9a93ca75aba09adccc13bfcfc56effaf5fc662eee1c47d00346953a370589151664f38c244a25f4e40b404d5d0981b91c7ba3833e3ad837
data/.travis.yml CHANGED
@@ -1,12 +1,4 @@
1
1
  ---
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
- - rbx-19mode
6
- - jruby-19mode
7
- - jruby-20mode
8
- - jruby-head
9
4
  - ruby-head
10
- matrix:
11
- allow_failures:
12
- - rvm: ruby-head
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # 0.3.0
2
+
3
+ * `Hobbit::Response` is no longer a subclass of `Rack::Response`.
4
+ * Forward `#map` and `#use` methods to `Rack::Builder` instead of define these
5
+ methods.
data/lib/hobbit/base.rb CHANGED
@@ -1,14 +1,16 @@
1
+ require 'forwardable'
2
+
1
3
  module Hobbit
2
4
  class Base
3
5
  class << self
6
+ extend Forwardable
7
+
8
+ def_delegators :stack, :map, :use
9
+
4
10
  %w(DELETE GET HEAD OPTIONS PATCH POST PUT).each do |verb|
5
11
  define_method(verb.downcase) { |path, &block| routes[verb] << compile_route(path, &block) }
6
12
  end
7
13
 
8
- def map(path, &block)
9
- stack.map(path, &block)
10
- end
11
-
12
14
  alias :_new :new
13
15
  def new(*args, &block)
14
16
  stack.run _new(*args, &block)
@@ -23,10 +25,6 @@ module Hobbit
23
25
  @stack ||= Rack::Builder.new
24
26
  end
25
27
 
26
- def use(middleware, *args, &block)
27
- stack.use(middleware, *args, &block)
28
- end
29
-
30
28
  private
31
29
 
32
30
  def compile_route(path, &block)
@@ -1,10 +1,22 @@
1
- require 'rack'
1
+ require 'forwardable'
2
2
 
3
3
  module Hobbit
4
- class Response < Rack::Response
5
- def initialize(body = [], status = 200, header = {})
6
- header['Content-Type'] = 'text/html; charset=utf-8'
7
- super
4
+ class Response
5
+ attr_accessor :body, :headers, :length, :status
6
+ extend Forwardable
7
+ def_delegators :headers, :[], :[]=
8
+
9
+ def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' })
10
+ @body, @headers, @status = body, headers, status
11
+ end
12
+
13
+ def finish
14
+ headers['Content-Length'] = body.each.map(&:size).inject { |memo, current| memo += current }
15
+ [status, headers, body]
16
+ end
17
+
18
+ def write(string)
19
+ self.body << string
8
20
  end
9
21
  end
10
22
  end
@@ -1,3 +1,3 @@
1
1
  module Hobbit
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -2,8 +2,73 @@ require 'minitest_helper'
2
2
 
3
3
  describe Hobbit::Response do
4
4
  describe '#initialize' do
5
- it 'must initialize Content-Type with text/html; charset=utf-8' do
6
- Hobbit::Response.new.headers['Content-Type'].must_equal 'text/html; charset=utf-8'
5
+ it 'must set the body, status and headers with no arguments given' do
6
+ default_headers = { 'Content-Type' => 'text/html; charset=utf-8' }
7
+ response = Hobbit::Response.new
8
+ response.status.must_equal 200
9
+ response.headers.must_equal default_headers
10
+ response.body.must_equal []
11
+ end
12
+
13
+ it 'must set the body, status and headers with arguments given' do
14
+ status, headers, body = 200, { 'Content-Type' => 'application/json' }, ['{"name":"Hobbit"}']
15
+ response = Hobbit::Response.new body, status, headers
16
+ response.status.must_equal status
17
+ response.headers.must_equal headers
18
+ response.body.must_equal body
19
+ end
20
+ end
21
+
22
+ describe '#[]' do
23
+ let(:response) { Hobbit::Response.new }
24
+
25
+ it 'must respond to #[]' do
26
+ response.must_respond_to :[]
27
+ end
28
+
29
+ it 'must return a header' do
30
+ response['Content-Type'].must_equal 'text/html; charset=utf-8'
31
+ end
32
+ end
33
+
34
+ describe '#[]=' do
35
+ let(:response) { Hobbit::Response.new }
36
+
37
+ it 'must respond to #[]=' do
38
+ response.must_respond_to :[]=
39
+ end
40
+
41
+ it 'must set a header' do
42
+ content_type = 'text/html; charset=utf-8'
43
+ response['Content-Type'] = content_type
44
+ response['Content-Type'].must_equal content_type
45
+ end
46
+ end
47
+
48
+ describe '#finish' do
49
+ let(:status) { 200 }
50
+ let(:headers) { { 'Content-Type' => 'application/json' } }
51
+ let(:body) { ['{"name":"Hobbit"}'] }
52
+
53
+ it 'must return a 3 elements array with status, headers and body' do
54
+ response = Hobbit::Response.new body, status, headers
55
+ response.finish.must_equal [status, headers, body]
56
+ end
57
+
58
+ it 'must calculate the Content-Length of the body' do
59
+ response = Hobbit::Response.new body, status, headers
60
+ s, h, b = response.finish
61
+ h.must_include 'Content-Length'
62
+ h['Content-Length'].must_equal body.each.map(&:size).inject { |m, s| m += s }
63
+ end
64
+ end
65
+
66
+ describe '#write' do
67
+ let(:response) { Hobbit::Response.new }
68
+
69
+ it 'must append the argument to the body of the response' do
70
+ response.write 'hello world'
71
+ response.body.must_equal ['hello world']
7
72
  end
8
73
  end
9
74
  end
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.2.1
4
+ version: 0.3.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-09-24 00:00:00.000000000 Z
11
+ date: 2013-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - .gitignore
119
119
  - .travis.yml
120
+ - CHANGELOG.md
120
121
  - Gemfile
121
122
  - LICENSE
122
123
  - README.md
@@ -150,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
151
  version: '0'
151
152
  requirements: []
152
153
  rubyforge_project:
153
- rubygems_version: 2.0.3
154
+ rubygems_version: 2.0.14
154
155
  signing_key:
155
156
  specification_version: 4
156
157
  summary: A minimalistic microframework built on top of rack