Skip to content

Commit 244671c

Browse files
committed
Accept symbols for HTTP status codes
1 parent 34f9e9f commit 244671c

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
77
* [#942](https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).
8+
* [#950](https://github.com/intridea/grape/pull/950): Status method can now accept one of Rack::Utils status code symbols (:ok, :found, :bad_request, etc.). - [@dabrorius](https://github.com/dabrorius).
89

910
* Your contribution here.
1011

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ post do
352352
end
353353
```
354354

355+
You can also use one of status codes symbols that are provided by [Rack utils](http://www.rubydoc.info/github/rack/rack/Rack/Utils#HTTP_STATUS_CODES-constant)
356+
357+
```ruby
358+
post do
359+
status :no_content
360+
end
361+
```
362+
355363
### Accept-Version Header
356364

357365
```ruby

lib/grape/dsl/inside_route.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ def redirect(url, options = {})
9494
#
9595
# @param status [Integer] The HTTP Status Code to return for this request.
9696
def status(status = nil)
97-
if status
97+
if status.is_a? Symbol
98+
if Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
99+
@status = Rack::Utils.status_code(status)
100+
else
101+
fail ArgumentError, "Status code :#{status} is invalid."
102+
end
103+
elsif status
98104
@status = status
99105
else
100106
return @status if @status

spec/grape/dsl/inside_route_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def initialize
107107
subject.status 501
108108
expect(subject.status).to eq 501
109109
end
110+
111+
it 'accepts symbol for status' do
112+
subject.status :see_other
113+
expect(subject.status).to eq 303
114+
end
115+
116+
it 'raises error if unknow symbol is passed' do
117+
expect { subject.status :foo_bar }
118+
.to raise_error(ArgumentError, 'Status code :foo_bar is invalid.')
119+
end
110120
end
111121

112122
describe '#header' do

0 commit comments

Comments
 (0)