hobbit-contrib 0.2.0 → 0.2.1

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: 774186fdc100731a772b7134afc48cae8d824154
4
- data.tar.gz: 96380c2994f125c6594170f75b52e16434e2cee3
3
+ metadata.gz: 160bf219184ef2cdbcd0a5447498bb174bed1d1f
4
+ data.tar.gz: ac07c279356e39dc84f49411b7889cb1de3b5361
5
5
  SHA512:
6
- metadata.gz: 8e969363d5fd33c76f57c147e498abf4efa1a284290420ff9f1b96136f2c8e9823793e9f7357dc30207337481e93fd17241944a03602f1eab926623aa34704ad
7
- data.tar.gz: e9336cee1b8c292a8d6e82913b6a1f6c813105416c25b8a6b0353722ab915a9299002baf3a59e730f9a112a265472f4c5dae254fab30f23c0a2a4c124c25559b
6
+ metadata.gz: 877a329df35df6e31de1687f427faf5dbb91a033111925de889406223f461fa70bb845f79cf51146345a82e655112f8a6cb9801a2a1382d70f516d245a31a984
7
+ data.tar.gz: bd72e44636f80a4b8d4d83cae8a3b538e65fd5819e4f279064d7b61d68d7813b16bc92812787a40a37d9495be39baa2b7c432ee5a13ec516b198e5f47fe0dc32
data/README.md CHANGED
@@ -61,7 +61,7 @@ class App < Hobbit::Base
61
61
  # * public/stylesheets
62
62
  use Rack::Static, root: 'public', urls: ['/images', '/javascripts', '/stylesheets']
63
63
  include Hobbit::AssetTag
64
- include Hobbit::EnhancedRender # see below
64
+ include Hobbit::Render # see below
65
65
 
66
66
  get '/' do
67
67
  render 'index', {}, layout: 'layout'
@@ -142,7 +142,6 @@ run App.new
142
142
 
143
143
  * `environment`: Returns the current environment. By default is
144
144
  `ENV['RACK_ENV']`.
145
- * `environment=()`: Sets the environment.
146
145
  * `development?`: Returns true if the current environment is `:development`.
147
146
  * `production?`: Returns true if the current environment is `:production`.
148
147
  * `test?`: Returns true if the current environment is `:test`.
@@ -20,4 +20,4 @@ module Hobbit
20
20
  url =~ /^http(s)?:\/\// ? url : "/stylesheets/#{url}.css"
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module Hobbit
2
2
  module Contrib
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -1,15 +1,11 @@
1
1
  module Hobbit
2
2
  module Environment
3
3
  def environment
4
- self.class.settings[:environment] || ENV['RACK_ENV'].to_sym
4
+ ENV['RACK_ENV'].to_sym
5
5
  end
6
6
 
7
- def environment=(environment)
8
- self.class.settings[:environment] = environment.to_sym
9
- end
10
-
11
- %w(development production test).each do |environment|
12
- define_method("#{environment}?") { self.class.settings[:environment] == environment.to_sym }
7
+ %w(development production test).each do |env|
8
+ define_method("#{env}?") { environment == env.to_sym }
13
9
  end
14
10
  end
15
11
  end
@@ -12,9 +12,11 @@ module Hobbit
12
12
 
13
13
  def _call(env)
14
14
  super
15
- rescue Exception => e
16
- body = instance_eval { self.class.errors[e.class].call(e) }
17
- response.body = [body] if self.class.errors.include? e.class
15
+ rescue *self.class.errors.keys => e
16
+ rescued = self.class.errors.keys.detect { |k| e.kind_of?(k)}
17
+
18
+ body = instance_eval { self.class.errors[rescued].call(e) }
19
+ response.body = [body]
18
20
  response.finish
19
21
  end
20
22
 
@@ -22,4 +24,4 @@ module Hobbit
22
24
  othermod.extend ClassMethods
23
25
  end
24
26
  end
25
- end
27
+ end
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
@@ -11,7 +11,7 @@ module Hobbit
11
11
 
12
12
  private
13
13
 
14
- def compile_filter!(path, &block)
14
+ def compile_filter(path, &block)
15
15
  filter = { block: block, compiled_path: nil, extra_params: [], path: path }
16
16
 
17
17
  compiled_path = path.gsub(/:\w+/) do |match|
@@ -26,11 +26,11 @@ module Hobbit
26
26
 
27
27
  def _call(env)
28
28
  @env = env
29
- @request = self.class.settings[:request_class].new(@env)
30
- @response = self.class.settings[:response_class].new
31
- filter!(:before)
29
+ @request = Rack::Request.new(@env)
30
+ @response = Hobbit::Response.new
31
+ filter :before
32
32
  super
33
- filter!(:after)
33
+ filter :after
34
34
  @response.finish
35
35
  end
36
36
 
@@ -40,7 +40,7 @@ module Hobbit
40
40
 
41
41
  private
42
42
 
43
- def filter!(kind)
43
+ def filter(kind)
44
44
  filter = self.class.filters[kind].detect { |f| f[:compiled_path] =~ request.path_info || f[:path] =~ // }
45
45
  if filter
46
46
  $~.captures.each_with_index do |value, index|
@@ -51,4 +51,4 @@ module Hobbit
51
51
  end
52
52
  end
53
53
  end
54
- end
54
+ end
@@ -4,4 +4,4 @@ module Hobbit
4
4
  env['rack.session']
5
5
  end
6
6
  end
7
- end
7
+ end
@@ -81,4 +81,4 @@ describe Hobbit::AssetTag do
81
81
  end
82
82
  end
83
83
  end
84
- end
84
+ end
@@ -11,55 +11,45 @@ describe Hobbit::Environment do
11
11
 
12
12
  describe '#environment' do
13
13
  it 'must return the current environment' do
14
- app.to_app.environment = :development
14
+ env, ENV['RACK_ENV'] = ENV['RACK_ENV'], 'development'
15
15
  app.to_app.environment.must_equal :development
16
- end
17
-
18
- it 'must default to RACK_ENV' do
19
- app.to_app.environment.must_equal ENV['RACK_ENV'].to_sym
20
- end
21
- end
22
-
23
- describe '#environment=()' do
24
- it 'must set the environment' do
25
- app.to_app.environment = :test
26
- app.to_app.environment.must_equal :test
16
+ ENV['RACK_ENV'] = env
27
17
  end
28
18
  end
29
19
 
30
20
  describe '#development?' do
31
- it 'must return true if self.class.settings[:environment] = :development' do
32
- app.to_app.environment = :development
21
+ it "must return true if ENV['RACK_ENV'] = :development" do
22
+ env, ENV['RACK_ENV'] = ENV['RACK_ENV'], 'development'
33
23
  app.to_app.development?.must_equal true
24
+ ENV['RACK_ENV'] = env
34
25
  end
35
26
 
36
- it 'must return false if self.class.settings[:environment] != :development' do
37
- app.to_app.environment = :production
27
+ it "must return false if ENV['RACK_ENV'] != :development" do
38
28
  app.to_app.development?.must_equal false
39
29
  end
40
30
  end
41
31
 
42
32
  describe '#production?' do
43
- it 'must return true if self.class.settings[:environment] = :production' do
44
- app.to_app.environment = :production
33
+ it "must return true if ENV['RACK_ENV'] = :production" do
34
+ env, ENV['RACK_ENV'] = ENV['RACK_ENV'], 'production'
45
35
  app.to_app.production?.must_equal true
36
+ ENV['RACK_ENV'] = env
46
37
  end
47
38
 
48
- it 'must return false if self.class.settings[:environment] != :production' do
49
- app.to_app.environment = :test
39
+ it "must return false if ENV['RACK_ENV'] != :production" do
50
40
  app.to_app.production?.must_equal false
51
41
  end
52
42
  end
53
43
 
54
44
  describe '#test?' do
55
- it 'must return true if self.class.settings[:environment] = :test' do
56
- app.to_app.environment = :test
45
+ it "must return true if ENV['RACK_ENV'] = :test" do
57
46
  app.to_app.test?.must_equal true
58
47
  end
59
48
 
60
- it 'must return false if self.class.settings[:environment] != :test' do
61
- app.to_app.environment = :development
49
+ it "must return false if ENV['RACK_ENV'] != :test" do
50
+ env, ENV['RACK_ENV'] = ENV['RACK_ENV'], 'development'
62
51
  app.to_app.test?.must_equal false
52
+ ENV['RACK_ENV'] = env
63
53
  end
64
54
  end
65
- end
55
+ end
@@ -137,4 +137,4 @@ describe 'combine Hobbit::ErrorHandling and Hobbit::Filter' do
137
137
  end
138
138
  end
139
139
  end
140
- end
140
+ end
@@ -4,33 +4,46 @@ describe Hobbit::ErrorHandling do
4
4
  include Hobbit::Contrib::Mock
5
5
  include Rack::Test::Methods
6
6
 
7
- class NotFoundException < StandardError ; end
7
+ class NotFoundException < Exception ; end
8
+ class SpecificNotFoundException < NotFoundException ; end
9
+ class UnknownException < Exception ; end
8
10
 
9
11
  let(:app) do
10
12
  mock_app do
11
13
  include Hobbit::ErrorHandling
12
14
 
13
- error Exception do |exception|
14
- exception.message
15
- end
16
-
17
15
  error NotFoundException do
18
16
  'Not Found'
19
17
  end
20
18
 
19
+ error StandardError do |exception|
20
+ exception.message
21
+ end
22
+
23
+
21
24
  get '/' do
22
25
  'hello'
23
26
  end
24
27
 
25
28
  get '/raises' do
26
29
  'not this'
27
- raise Exception, 'Exception'
30
+ raise StandardError, 'StandardError'
28
31
  end
29
32
 
30
33
  get '/other_raises' do
31
34
  response.write 'not this'
32
35
  raise NotFoundException
33
36
  end
37
+
38
+ get '/same_other_raises' do
39
+ response.write 'not this'
40
+ raise SpecificNotFoundException
41
+ end
42
+
43
+ get '/uncaught_raise' do
44
+ response.write 'not this'
45
+ raise UnknownException
46
+ end
34
47
  end
35
48
  end
36
49
 
@@ -39,11 +52,11 @@ describe Hobbit::ErrorHandling do
39
52
  p = Proc.new { 'error' }
40
53
  app = mock_app do
41
54
  include Hobbit::ErrorHandling
42
- error Exception, &p
55
+ error StandardError, &p
43
56
  end
44
57
 
45
- app.to_app.class.errors.must_include Exception
46
- app.to_app.class.errors[Exception].call.must_equal p.call
58
+ app.to_app.class.errors.must_include StandardError
59
+ app.to_app.class.errors[StandardError].call.must_equal p.call
47
60
  end
48
61
  end
49
62
 
@@ -53,7 +66,7 @@ describe Hobbit::ErrorHandling do
53
66
  end
54
67
  end
55
68
 
56
- describe 'when does not raises an exception' do
69
+ describe 'when does not raise exception' do
57
70
  it 'must work as expected' do
58
71
  get '/'
59
72
  last_response.must_be :ok?
@@ -61,11 +74,17 @@ describe Hobbit::ErrorHandling do
61
74
  end
62
75
  end
63
76
 
64
- describe 'when raises an exception' do
77
+ describe 'when does raise an unknown exception class' do
78
+ it 'must not halt default propagation of the unknown class' do
79
+ proc {get '/uncaught_raise'}.must_raise(UnknownException)
80
+ end
81
+ end
82
+
83
+ describe 'when raises an known exception class' do
65
84
  it 'must call the block set in error' do
66
85
  get '/raises'
67
86
  last_response.must_be :ok?
68
- last_response.body.must_equal 'Exception'
87
+ last_response.body.must_equal 'StandardError'
69
88
  end
70
89
 
71
90
  it 'must allow to define more than one exception' do
@@ -74,6 +93,12 @@ describe Hobbit::ErrorHandling do
74
93
  last_response.body.must_equal 'Not Found'
75
94
  end
76
95
 
96
+ it 'must allow to define a general exception class to catch' do
97
+ get '/same_other_raises'
98
+ last_response.must_be :ok?
99
+ last_response.body.must_equal 'Not Found'
100
+ end
101
+
77
102
  it 'must set the returned value of the error block as the body' do
78
103
  get '/other_raises'
79
104
  last_response.must_be :ok?
@@ -82,7 +107,7 @@ describe Hobbit::ErrorHandling do
82
107
  end
83
108
 
84
109
  it 'must override a previous block if a new one is passed' do
85
- app.to_app.class.error Exception do
110
+ app.to_app.class.error StandardError do
86
111
  'other handler!'
87
112
  end
88
113
 
@@ -91,4 +116,4 @@ describe Hobbit::ErrorHandling do
91
116
  last_response.body.must_equal 'other handler!'
92
117
  end
93
118
  end
94
- end
119
+ end
data/spec/filter_spec.rb CHANGED
@@ -55,6 +55,57 @@ EOS
55
55
  end
56
56
  end
57
57
 
58
+ describe '::compile_filter' do
59
+ let(:block) { block = Proc.new { |env| [200, {}, []] } }
60
+
61
+ it 'must compile an empty string' do
62
+ path = ''
63
+ route = app.to_app.class.send :compile_filter, path, &block
64
+ route[:block].call({}).must_equal block.call({})
65
+ route[:compiled_path].to_s.must_equal /^$/.to_s
66
+ end
67
+
68
+ it 'must compile /' do
69
+ path = '/'
70
+ route = app.to_app.class.send :compile_filter, path, &block
71
+ route[:block].call({}).must_equal block.call({})
72
+ route[:compiled_path].to_s.must_equal /^\/$/.to_s
73
+ end
74
+
75
+ it 'must compile with .' do
76
+ path = '/route.json'
77
+ route = app.to_app.class.send :compile_filter, path, &block
78
+ route[:block].call({}).must_equal block.call({})
79
+ route[:compiled_path].to_s.must_equal /^\/route.json$/.to_s
80
+ end
81
+
82
+ it 'must compile with -' do
83
+ path = '/hello-world'
84
+ route = app.to_app.class.send :compile_filter, path, &block
85
+ route[:block].call({}).must_equal block.call({})
86
+ route[:compiled_path].to_s.must_equal /^\/hello-world$/.to_s
87
+ end
88
+
89
+ it 'must compile with params' do
90
+ path = '/hello/:name'
91
+ route = app.to_app.class.send :compile_filter, path, &block
92
+ route[:block].call({}).must_equal block.call({})
93
+ route[:compiled_path].to_s.must_equal /^\/hello\/([^\/?#]+)$/.to_s
94
+
95
+ path = '/say/:something/to/:someone'
96
+ route = app.to_app.class.send :compile_filter, path, &block
97
+ route[:block].call({}).must_equal block.call({})
98
+ route[:compiled_path].to_s.must_equal /^\/say\/([^\/?#]+)\/to\/([^\/?#]+)$/.to_s
99
+ end
100
+
101
+ it 'must compile with . and params' do
102
+ path = '/route/:id.json'
103
+ route = app.to_app.class.send :compile_filter, path, &block
104
+ route[:block].call({}).must_equal block.call({})
105
+ route[:compiled_path].to_s.must_equal /^\/route\/([^\/?#]+).json$/.to_s
106
+ end
107
+ end
108
+
58
109
  it 'must call before and after filters' do
59
110
  get '/'
60
111
  last_response.must_be :ok?
@@ -98,4 +149,4 @@ EOS
98
149
  last_request.env['hobbit.after'].must_equal 'this will match'
99
150
  end
100
151
  end
101
- end
152
+ end
@@ -1 +1 @@
1
- <h1>Hello World!</h1>
1
+ <h1>Hello World!</h1>
@@ -6,4 +6,4 @@
6
6
  <body>
7
7
  <%= yield %>
8
8
  </body>
9
- </html>
9
+ </html>
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.2.0
4
+ version: 0.2.1
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-08-05 00:00:00.000000000 Z
11
+ date: 2013-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
190
  version: '0'
191
191
  requirements: []
192
192
  rubyforge_project:
193
- rubygems_version: 2.0.0
193
+ rubygems_version: 2.0.3
194
194
  signing_key:
195
195
  specification_version: 4
196
196
  summary: Contributed Hobbit extensions