lotus-controller 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/LICENSE.md +1 -1
- data/README.md +21 -1
- data/lib/lotus/action.rb +4 -0
- data/lib/lotus/action/callbacks.rb +54 -8
- data/lib/lotus/action/glue.rb +22 -1
- data/lib/lotus/action/head.rb +40 -0
- data/lib/lotus/action/params.rb +26 -4
- data/lib/lotus/action/rack.rb +39 -24
- data/lib/lotus/action/rack/callable.rb +47 -0
- data/lib/lotus/action/throwable.rb +33 -5
- data/lib/lotus/controller/configuration.rb +13 -0
- data/lib/lotus/controller/version.rb +1 -1
- data/lib/lotus/http/status.rb +12 -0
- data/lotus-controller.gemspec +2 -2
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ca9d4aeba96621f489499f6dc67a58b62b5dd66
|
4
|
+
data.tar.gz: f86c1e2d695ac2fb3bc595a6fb6b587edd4f6bc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5597d8bc2fe5a15577a6698577adb1f9895c5c37353faed252b045974010b351994cfadcadf3d49215a76b5477aab5d95487d84e570b8913a8a0669ae0dee473
|
7
|
+
data.tar.gz: a8e9f5b71b887a6db599e93e7eb555bb1fa556d33ac0ae5f9c095c18f2b55b29bdf53822b9fa459615c03cedf965d564ef4ee52e347d56bc412aaa1e3ff98133
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,20 @@
|
|
1
1
|
# Lotus::Controller
|
2
2
|
Complete, fast and testable actions for Rack
|
3
3
|
|
4
|
+
## v0.3.2 - 2015-01-30
|
5
|
+
### Added
|
6
|
+
- [Alfonso Uceda Pompa] Callbacks: introduced `append_before` (alias of `before`), `append_after` (alias of `after`), `prepend_before` and `prepend_after`.
|
7
|
+
- [Alfonso Uceda Pompa] Introduced `Lotus::Action::Params#raw` which returns unfiltered data as it comes from an HTTP request.
|
8
|
+
- [Alfonso Uceda Pompa] `Lotus::Action::Rack.use` now fully supports Rack middleware, by mounting an internal `Rack::Builder` instance.
|
9
|
+
- [Simone Carletti] `Lotus::Action::Throwable#halt` now accepts an optional message. If missing it falls back to the corresponding HTTP status message.
|
10
|
+
- [Steve Hodgkiss] Nested params validation
|
11
|
+
|
12
|
+
### Fixed
|
13
|
+
- [Luca Guidi] Ensure HEAD requests will return empty body
|
14
|
+
- [Stefano Verna] Ensure HTTP status codes with empty body won't send body and non-entity headers.
|
15
|
+
- [Luca Guidi] Only dump exceptions in `rack.errors` if handling is turned off, or the raised exception is not managed.
|
16
|
+
- [Luca Guidi] Ensure params will return coerced values
|
17
|
+
|
4
18
|
## v0.3.1 - 2015-01-08
|
5
19
|
### Added
|
6
20
|
- [Lasse Skindstad Ebert] Introduced `Action#request` which returns an instance a `Rack::Request` compliant object: `Lotus::Action::Request`.
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -446,6 +446,26 @@ action = Show.new
|
|
446
446
|
action.call({}) # => [401, {}, ["Unauthorized"]]
|
447
447
|
```
|
448
448
|
|
449
|
+
Alternatively, you can specify a custom message.
|
450
|
+
|
451
|
+
```ruby
|
452
|
+
class Show
|
453
|
+
include Lotus::Action
|
454
|
+
|
455
|
+
def call(params)
|
456
|
+
DroidRepository.find(params[:id]) or not_found
|
457
|
+
end
|
458
|
+
|
459
|
+
private
|
460
|
+
def not_found
|
461
|
+
halt 404, "This is not the droid you're looking for"
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
action = Show.new
|
466
|
+
action.call({}) # => [404, {}, ["This is not the droid you're looking for"]]
|
467
|
+
```
|
468
|
+
|
449
469
|
### Cookies
|
450
470
|
|
451
471
|
Lotus::Controller offers convenient access to cookies.
|
@@ -1064,4 +1084,4 @@ __Lotus::Controller__ uses [Semantic Versioning 2.0.0](http://semver.org)
|
|
1064
1084
|
|
1065
1085
|
## Copyright
|
1066
1086
|
|
1067
|
-
Copyright 2014 Luca Guidi – Released under MIT License
|
1087
|
+
Copyright © 2014-2015 Luca Guidi – Released under MIT License
|
data/lib/lotus/action.rb
CHANGED
@@ -6,6 +6,7 @@ require 'lotus/action/exposable'
|
|
6
6
|
require 'lotus/action/throwable'
|
7
7
|
require 'lotus/action/callbacks'
|
8
8
|
require 'lotus/action/validatable'
|
9
|
+
require 'lotus/action/head'
|
9
10
|
require 'lotus/action/callable'
|
10
11
|
|
11
12
|
module Lotus
|
@@ -36,6 +37,7 @@ module Lotus
|
|
36
37
|
#
|
37
38
|
# @see Lotus::Action::Rack
|
38
39
|
# @see Lotus::Action::Mime
|
40
|
+
# @see Lotus::Action::Http
|
39
41
|
# @see Lotus::Action::Redirect
|
40
42
|
# @see Lotus::Action::Exposable
|
41
43
|
# @see Lotus::Action::Throwable
|
@@ -53,6 +55,7 @@ module Lotus
|
|
53
55
|
include Callbacks
|
54
56
|
include Validatable
|
55
57
|
include Configurable
|
58
|
+
include Head
|
56
59
|
prepend Callable
|
57
60
|
end
|
58
61
|
end
|
@@ -75,6 +78,7 @@ module Lotus
|
|
75
78
|
# @see Lotus::Action::Session#finish
|
76
79
|
# @see Lotus::Action::Cookies#finish
|
77
80
|
# @see Lotus::Action::Cache#finish
|
81
|
+
# @see Lotus::Action::Head#finish
|
78
82
|
def finish
|
79
83
|
end
|
80
84
|
end
|
@@ -17,7 +17,7 @@ module Lotus
|
|
17
17
|
# @since 0.1.0
|
18
18
|
# @api private
|
19
19
|
#
|
20
|
-
# @see http://www.ruby-doc.org/core
|
20
|
+
# @see http://www.ruby-doc.org/core/Module.html#method-i-included
|
21
21
|
def self.included(base)
|
22
22
|
base.class_eval do
|
23
23
|
extend ClassMethods
|
@@ -38,7 +38,7 @@ module Lotus
|
|
38
38
|
# @since 0.1.0
|
39
39
|
# @api private
|
40
40
|
#
|
41
|
-
# @see http://www.ruby-doc.org/core
|
41
|
+
# @see http://www.ruby-doc.org/core/Module.html#method-i-extended
|
42
42
|
def self.extended(base)
|
43
43
|
base.class_eval do
|
44
44
|
include Utils::ClassAttribute
|
@@ -63,7 +63,9 @@ module Lotus
|
|
63
63
|
#
|
64
64
|
# @return [void]
|
65
65
|
#
|
66
|
-
# @since 0.
|
66
|
+
# @since 0.3.2
|
67
|
+
#
|
68
|
+
# @see Lotus::Action::Callbacks::ClassMethods#append_after
|
67
69
|
#
|
68
70
|
# @example Method names (symbols)
|
69
71
|
# require 'lotus/controller'
|
@@ -111,10 +113,13 @@ module Lotus
|
|
111
113
|
# # 1. authentication
|
112
114
|
# # 2. set the article
|
113
115
|
# # 3. #call
|
114
|
-
def
|
115
|
-
before_callbacks.
|
116
|
+
def append_before(*callbacks, &blk)
|
117
|
+
before_callbacks.append(*callbacks, &blk)
|
116
118
|
end
|
117
119
|
|
120
|
+
# @since 0.1.0
|
121
|
+
alias_method :before, :append_before
|
122
|
+
|
118
123
|
# Define a callback for an Action.
|
119
124
|
# The callback will be executed **after** the action is called, in the
|
120
125
|
# order they are added.
|
@@ -127,11 +132,52 @@ module Lotus
|
|
127
132
|
#
|
128
133
|
# @return [void]
|
129
134
|
#
|
135
|
+
# @since 0.3.2
|
136
|
+
#
|
137
|
+
# @see Lotus::Action::Callbacks::ClassMethods#append_before
|
138
|
+
def append_after(*callbacks, &blk)
|
139
|
+
after_callbacks.append(*callbacks, &blk)
|
140
|
+
end
|
141
|
+
|
130
142
|
# @since 0.1.0
|
143
|
+
alias_method :after, :append_after
|
144
|
+
|
145
|
+
# Define a callback for an Action.
|
146
|
+
# The callback will be executed **before** the action is called.
|
147
|
+
# It will add the callback at the beginning of the callbacks' chain.
|
148
|
+
#
|
149
|
+
# @param callbacks [Symbol, Array<Symbol>] a single or multiple symbol(s)
|
150
|
+
# each of them is representing a name of a method available in the
|
151
|
+
# context of the Action.
|
152
|
+
#
|
153
|
+
# @param blk [Proc] an anonymous function to be executed
|
154
|
+
#
|
155
|
+
# @return [void]
|
156
|
+
#
|
157
|
+
# @since 0.3.2
|
158
|
+
#
|
159
|
+
# @see Lotus::Action::Callbacks::ClassMethods#prepend_after
|
160
|
+
def prepend_before(*callbacks, &blk)
|
161
|
+
before_callbacks.prepend(*callbacks, &blk)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Define a callback for an Action.
|
165
|
+
# The callback will be executed **after** the action is called.
|
166
|
+
# It will add the callback at the beginning of the callbacks' chain.
|
167
|
+
#
|
168
|
+
# @param callbacks [Symbol, Array<Symbol>] a single or multiple symbol(s)
|
169
|
+
# each of them is representing a name of a method available in the
|
170
|
+
# context of the Action.
|
171
|
+
#
|
172
|
+
# @param blk [Proc] an anonymous function to be executed
|
173
|
+
#
|
174
|
+
# @return [void]
|
175
|
+
#
|
176
|
+
# @since 0.3.2
|
131
177
|
#
|
132
|
-
# @see Lotus::Action::Callbacks::ClassMethods#
|
133
|
-
def
|
134
|
-
after_callbacks.
|
178
|
+
# @see Lotus::Action::Callbacks::ClassMethods#prepend_before
|
179
|
+
def prepend_after(*callbacks, &blk)
|
180
|
+
after_callbacks.prepend(*callbacks, &blk)
|
135
181
|
end
|
136
182
|
end
|
137
183
|
|
data/lib/lotus/action/glue.rb
CHANGED
@@ -2,6 +2,9 @@ module Lotus
|
|
2
2
|
module Action
|
3
3
|
# Glue code for full stack Lotus applications
|
4
4
|
#
|
5
|
+
# This includes missing rendering logic that it makes sense to include
|
6
|
+
# only for web applications.
|
7
|
+
#
|
5
8
|
# @api private
|
6
9
|
# @since 0.3.0
|
7
10
|
module Glue
|
@@ -11,12 +14,30 @@ module Lotus
|
|
11
14
|
# @since 0.3.0
|
12
15
|
ENV_KEY = 'lotus.action'.freeze
|
13
16
|
|
17
|
+
# @api private
|
18
|
+
# @since 0.3.2
|
19
|
+
ADDITIONAL_HTTP_STATUSES_WITHOUT_BODY = Set.new([301, 302]).freeze
|
20
|
+
|
14
21
|
# Override Ruby's Module#included
|
15
22
|
#
|
16
23
|
# @api private
|
17
24
|
# @since 0.3.0
|
18
25
|
def self.included(base)
|
19
|
-
base.class_eval { expose
|
26
|
+
base.class_eval { expose(:format) if respond_to?(:expose) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Check if the current HTTP request is renderable.
|
30
|
+
#
|
31
|
+
# It verifies if the verb isn't HEAD and if the status demands to omit
|
32
|
+
# the body.
|
33
|
+
#
|
34
|
+
# @return [TrueClass,FalseClass] the result of the check
|
35
|
+
#
|
36
|
+
# @api private
|
37
|
+
# @since 0.3.2
|
38
|
+
def renderable?
|
39
|
+
!_requires_no_body? &&
|
40
|
+
!ADDITIONAL_HTTP_STATUSES_WITHOUT_BODY.include?(@_status)
|
20
41
|
end
|
21
42
|
|
22
43
|
protected
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Lotus
|
2
|
+
module Action
|
3
|
+
# Ensures to not send body or headers for HEAD requests and/or for status
|
4
|
+
# codes that doesn't allow them.
|
5
|
+
#
|
6
|
+
# @since 0.3.2
|
7
|
+
#
|
8
|
+
# @see http://www.ietf.org/rfc/rfc2616.txt
|
9
|
+
module Head
|
10
|
+
|
11
|
+
# Status codes that by RFC must not include a message body
|
12
|
+
#
|
13
|
+
# @since 0.3.2
|
14
|
+
# @api private
|
15
|
+
HTTP_STATUSES_WITHOUT_BODY = Set.new((100..199).to_a << 204 << 205 << 304).freeze
|
16
|
+
|
17
|
+
# Ensures to not send body or headers for HEAD requests and/or for status
|
18
|
+
# codes that doesn't allow them.
|
19
|
+
#
|
20
|
+
# @since 0.3.2
|
21
|
+
# @api private
|
22
|
+
#
|
23
|
+
# @see Lotus::Action#finish
|
24
|
+
def finish
|
25
|
+
super
|
26
|
+
|
27
|
+
if _requires_no_body?
|
28
|
+
@_body = nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
# @since 0.3.2
|
34
|
+
# @api private
|
35
|
+
def _requires_no_body?
|
36
|
+
HTTP_STATUSES_WITHOUT_BODY.include?(@_status) || head?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/lotus/action/params.rb
CHANGED
@@ -80,8 +80,8 @@ module Lotus
|
|
80
80
|
#
|
81
81
|
# params = SignupParams.new({})
|
82
82
|
# params.valid? # => raise ArgumentError
|
83
|
-
def self.param(name, options = {})
|
84
|
-
attribute name, options
|
83
|
+
def self.param(name, options = {}, &block)
|
84
|
+
attribute name, options, &block
|
85
85
|
nil
|
86
86
|
end
|
87
87
|
|
@@ -91,12 +91,28 @@ module Lotus
|
|
91
91
|
defined_attributes.any?
|
92
92
|
end
|
93
93
|
|
94
|
+
# Overrides the method in Lotus::Validation to build a class that
|
95
|
+
# inherits from Params rather than only Lotus::Validations.
|
96
|
+
#
|
97
|
+
# @since 0.3.2
|
98
|
+
# @api private
|
99
|
+
def self.build_validation_class(&block)
|
100
|
+
kls = Class.new(Params)
|
101
|
+
kls.class_eval(&block)
|
102
|
+
kls
|
103
|
+
end
|
104
|
+
|
94
105
|
# @attr_reader env [Hash] the Rack env
|
95
106
|
#
|
96
107
|
# @since 0.2.0
|
97
108
|
# @api private
|
98
109
|
attr_reader :env
|
99
110
|
|
111
|
+
# @attr_reader raw [Lotus::Utils::Attributes] all request's attributes
|
112
|
+
#
|
113
|
+
# @since 0.3.2
|
114
|
+
attr_reader :raw
|
115
|
+
|
100
116
|
# Initialize the params and freeze them.
|
101
117
|
#
|
102
118
|
# @param env [Hash] a Rack env or an hash of params.
|
@@ -138,10 +154,16 @@ module Lotus
|
|
138
154
|
if self.class.whitelisting?
|
139
155
|
_whitelisted_params
|
140
156
|
else
|
141
|
-
@attributes =
|
157
|
+
@attributes = _raw
|
142
158
|
end
|
143
159
|
end
|
144
160
|
|
161
|
+
# @since 0.3.2
|
162
|
+
# @api private
|
163
|
+
def _raw
|
164
|
+
@raw ||= Utils::Attributes.new(_params)
|
165
|
+
end
|
166
|
+
|
145
167
|
# @since 0.3.1
|
146
168
|
# @api private
|
147
169
|
def _params
|
@@ -159,7 +181,7 @@ module Lotus
|
|
159
181
|
# @api private
|
160
182
|
def _whitelisted_params
|
161
183
|
{}.tap do |result|
|
162
|
-
|
184
|
+
_raw.to_h.each do |k, v|
|
163
185
|
next unless self.class.defined_attributes.include?(k.to_s)
|
164
186
|
|
165
187
|
result[k] = v
|
data/lib/lotus/action/rack.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'securerandom'
|
2
2
|
require 'lotus/action/request'
|
3
|
+
require 'lotus/action/rack/callable'
|
3
4
|
|
4
5
|
module Lotus
|
5
6
|
module Action
|
@@ -27,6 +28,18 @@ module Lotus
|
|
27
28
|
# @see Lotus::Action::Rack#request_id
|
28
29
|
DEFAULT_REQUEST_ID_LENGTH = 16
|
29
30
|
|
31
|
+
# The request method
|
32
|
+
#
|
33
|
+
# @since 0.3.2
|
34
|
+
# @api private
|
35
|
+
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
36
|
+
|
37
|
+
# HEAD request
|
38
|
+
#
|
39
|
+
# @since 0.3.2
|
40
|
+
# @api private
|
41
|
+
HEAD = 'HEAD'.freeze
|
42
|
+
|
30
43
|
# Override Ruby's hook for modules.
|
31
44
|
# It includes basic Lotus::Action modules to the given class.
|
32
45
|
#
|
@@ -41,11 +54,20 @@ module Lotus
|
|
41
54
|
end
|
42
55
|
|
43
56
|
module ClassMethods
|
44
|
-
#
|
57
|
+
# Build rack builder
|
45
58
|
#
|
46
|
-
#
|
47
|
-
|
48
|
-
|
59
|
+
# @return [Rack::Builder]
|
60
|
+
def rack_builder
|
61
|
+
@rack_builder ||= begin
|
62
|
+
extend Lotus::Action::Rack::Callable
|
63
|
+
rack_builder = ::Rack::Builder.new
|
64
|
+
rack_builder.run ->(env) { self.new.call(env) }
|
65
|
+
rack_builder
|
66
|
+
end
|
67
|
+
end
|
68
|
+
# Use a Rack middleware
|
69
|
+
#
|
70
|
+
# The middleware will be used as it is.
|
49
71
|
#
|
50
72
|
# At the runtime, the middleware be invoked with the raw Rack env.
|
51
73
|
#
|
@@ -53,12 +75,13 @@ module Lotus
|
|
53
75
|
# this method.
|
54
76
|
#
|
55
77
|
# @param middleware [#call] A Rack middleware
|
78
|
+
# @param args [Array] Array arguments for middleware
|
56
79
|
#
|
57
80
|
# @since 0.2.0
|
58
81
|
#
|
59
82
|
# @see Lotus::Action::Callbacks::ClassMethods#before
|
60
83
|
#
|
61
|
-
# @example
|
84
|
+
# @example Middleware
|
62
85
|
# require 'lotus/controller'
|
63
86
|
#
|
64
87
|
# module Sessions
|
@@ -71,29 +94,12 @@ module Lotus
|
|
71
94
|
# end
|
72
95
|
# end
|
73
96
|
# end
|
74
|
-
|
75
|
-
|
76
|
-
# require 'lotus/controller'
|
77
|
-
#
|
78
|
-
# module Sessions
|
79
|
-
# class Create
|
80
|
-
# include Lotus::Controller
|
81
|
-
# use XMiddleware.new('x', 123)
|
82
|
-
#
|
83
|
-
# def call(params)
|
84
|
-
# # ...
|
85
|
-
# end
|
86
|
-
# end
|
87
|
-
# end
|
88
|
-
def use(middleware)
|
89
|
-
before do |params|
|
90
|
-
middleware.call(params.env)
|
91
|
-
end
|
97
|
+
def use(middleware, *args)
|
98
|
+
rack_builder.use middleware, *args
|
92
99
|
end
|
93
100
|
end
|
94
101
|
|
95
102
|
protected
|
96
|
-
|
97
103
|
# Gets the headers from the response
|
98
104
|
#
|
99
105
|
# @return [Hash] the HTTP headers from the response
|
@@ -210,6 +216,15 @@ module Lotus
|
|
210
216
|
body = Array(body) unless body.respond_to?(:each)
|
211
217
|
@_body = body
|
212
218
|
end
|
219
|
+
|
220
|
+
# Check if the current request is a HEAD
|
221
|
+
#
|
222
|
+
# @return [TrueClass,FalseClass] the result of the check
|
223
|
+
#
|
224
|
+
# @since 0.3.2
|
225
|
+
def head?
|
226
|
+
@_env[REQUEST_METHOD] == HEAD
|
227
|
+
end
|
213
228
|
end
|
214
229
|
end
|
215
230
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
module Lotus
|
3
|
+
module Action
|
4
|
+
module Rack
|
5
|
+
module Callable
|
6
|
+
# Callable module for actions. With this module, actions with middlewares
|
7
|
+
# will be able to work with rack builder.
|
8
|
+
#
|
9
|
+
# @param env [Hash] the full Rack env or the params. This value may vary,
|
10
|
+
# see the examples below.
|
11
|
+
#
|
12
|
+
# @since x.x.x
|
13
|
+
#
|
14
|
+
# @see Lotus::Action::Rack::ClassMethods#rack_builder
|
15
|
+
# @see Lotus::Action::Rack::ClassMethods#use
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# require 'lotus/controller'
|
19
|
+
#
|
20
|
+
# class MyMiddleware
|
21
|
+
# def initialize(app)
|
22
|
+
# @app = app
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# def call(env)
|
26
|
+
# #...
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# class Show
|
31
|
+
# include Lotus::Action
|
32
|
+
# use MyMiddleware
|
33
|
+
#
|
34
|
+
# def call(params)
|
35
|
+
# # ...
|
36
|
+
# puts params # => { id: 23 } extracted from Rack env
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# Show.respond_to?(:call) # => true
|
41
|
+
def call(env)
|
42
|
+
rack_builder.call(env)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -62,24 +62,50 @@ module Lotus
|
|
62
62
|
|
63
63
|
protected
|
64
64
|
|
65
|
-
# Halt the action execution with the given HTTP status code.
|
65
|
+
# Halt the action execution with the given HTTP status code and message.
|
66
66
|
#
|
67
67
|
# When used, the execution of a callback or of an action is interrupted
|
68
68
|
# and the control returns to the framework, that decides how to handle
|
69
69
|
# the event.
|
70
70
|
#
|
71
|
-
#
|
72
|
-
#
|
71
|
+
# If a message is provided, it sets the response body with the message.
|
72
|
+
# Otherwise, it sets the response body with the default message associated
|
73
|
+
# to the code (eg 404 will set `"Not Found"`).
|
73
74
|
#
|
74
75
|
# @param code [Fixnum] a valid HTTP status code
|
76
|
+
# @param message [String] the response body
|
75
77
|
#
|
76
78
|
# @since 0.2.0
|
77
79
|
#
|
78
80
|
# @see Lotus::Controller#handled_exceptions
|
79
81
|
# @see Lotus::Action::Throwable#handle_exception
|
80
82
|
# @see Lotus::Http::Status:ALL
|
81
|
-
|
82
|
-
|
83
|
+
#
|
84
|
+
# @example Basic usage
|
85
|
+
# require 'lotus/controller'
|
86
|
+
#
|
87
|
+
# class Show
|
88
|
+
# def call(params)
|
89
|
+
# halt 404
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# # => [404, {}, ["Not Found"]]
|
94
|
+
#
|
95
|
+
# @example Custom message
|
96
|
+
# require 'lotus/controller'
|
97
|
+
#
|
98
|
+
# class Show
|
99
|
+
# def call(params)
|
100
|
+
# halt 404, "This is not the droid you're looking for."
|
101
|
+
# end
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# # => [404, {}, ["This is not the droid you're looking for."]]
|
105
|
+
def halt(code, message = nil)
|
106
|
+
message ||= Http::Status.message_for(code)
|
107
|
+
status(code, message)
|
108
|
+
|
83
109
|
throw :halt
|
84
110
|
end
|
85
111
|
|
@@ -112,6 +138,8 @@ module Lotus
|
|
112
138
|
# @since 0.2.0
|
113
139
|
# @api private
|
114
140
|
def _reference_in_rack_errors(exception)
|
141
|
+
return if configuration.handled_exception?(exception)
|
142
|
+
|
115
143
|
if errors = @_env[RACK_ERRORS]
|
116
144
|
errors.write(_dump_exception(exception))
|
117
145
|
errors.flush
|
@@ -194,6 +194,19 @@ module Lotus
|
|
194
194
|
@handled_exceptions.fetch(exception.class) { DEFAULT_ERROR_CODE }
|
195
195
|
end
|
196
196
|
|
197
|
+
# Check if the given exception is handled.
|
198
|
+
#
|
199
|
+
# @param exception [Exception] an exception
|
200
|
+
#
|
201
|
+
# @since 0.3.2
|
202
|
+
# @api private
|
203
|
+
#
|
204
|
+
# @see Lotus::Controller::Configuration#handle_exception
|
205
|
+
def handled_exception?(exception)
|
206
|
+
handled_exceptions &&
|
207
|
+
!!@handled_exceptions.fetch(exception.class) { false }
|
208
|
+
end
|
209
|
+
|
197
210
|
# Specify which is the default action module to be included when we use
|
198
211
|
# the `Lotus::Controller.action` method.
|
199
212
|
#
|
data/lib/lotus/http/status.rb
CHANGED
@@ -45,6 +45,18 @@ module Lotus
|
|
45
45
|
def self.for_code(code)
|
46
46
|
ALL.assoc(code)
|
47
47
|
end
|
48
|
+
|
49
|
+
# Return a message for the given status code
|
50
|
+
#
|
51
|
+
# @param code [Fixnum] a valid HTTP code
|
52
|
+
#
|
53
|
+
# @return [String] a message for the given status code
|
54
|
+
#
|
55
|
+
# @since 0.3.2
|
56
|
+
# @api private
|
57
|
+
def self.message_for(code)
|
58
|
+
for_code(code)[1]
|
59
|
+
end
|
48
60
|
end
|
49
61
|
end
|
50
62
|
end
|
data/lotus-controller.gemspec
CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.required_ruby_version = '>= 2.0.0'
|
21
21
|
|
22
22
|
spec.add_dependency 'rack', '~> 1.5'
|
23
|
-
spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.
|
24
|
-
spec.add_dependency 'lotus-validations', '~> 0.2', '>= 0.2.
|
23
|
+
spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.4'
|
24
|
+
spec.add_dependency 'lotus-validations', '~> 0.2', '>= 0.2.4'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
27
27
|
spec.add_development_dependency 'minitest', '~> 5'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lotus-controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-01-
|
12
|
+
date: 2015-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: '0.3'
|
35
35
|
- - ">="
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: 0.3.
|
37
|
+
version: 0.3.4
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: '0.3'
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.3.
|
47
|
+
version: 0.3.4
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: lotus-validations
|
50
50
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0.2'
|
55
55
|
- - ">="
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version: 0.2.
|
57
|
+
version: 0.2.4
|
58
58
|
type: :runtime
|
59
59
|
prerelease: false
|
60
60
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -64,7 +64,7 @@ dependencies:
|
|
64
64
|
version: '0.2'
|
65
65
|
- - ">="
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version: 0.2.
|
67
|
+
version: 0.2.4
|
68
68
|
- !ruby/object:Gem::Dependency
|
69
69
|
name: bundler
|
70
70
|
requirement: !ruby/object:Gem::Requirement
|
@@ -147,9 +147,11 @@ files:
|
|
147
147
|
- lib/lotus/action/exposable.rb
|
148
148
|
- lib/lotus/action/flash.rb
|
149
149
|
- lib/lotus/action/glue.rb
|
150
|
+
- lib/lotus/action/head.rb
|
150
151
|
- lib/lotus/action/mime.rb
|
151
152
|
- lib/lotus/action/params.rb
|
152
153
|
- lib/lotus/action/rack.rb
|
154
|
+
- lib/lotus/action/rack/callable.rb
|
153
155
|
- lib/lotus/action/redirect.rb
|
154
156
|
- lib/lotus/action/request.rb
|
155
157
|
- lib/lotus/action/session.rb
|
@@ -181,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
183
|
version: '0'
|
182
184
|
requirements: []
|
183
185
|
rubyforge_project:
|
184
|
-
rubygems_version: 2.
|
186
|
+
rubygems_version: 2.2.2
|
185
187
|
signing_key:
|
186
188
|
specification_version: 4
|
187
189
|
summary: Complete, fast and testable actions for Rack and Lotus
|