Skip to content

Commit

Permalink
Merge pull request #950 from dabrorius/allow_symbols_for_status_codes
Browse files Browse the repository at this point in the history
Allow symbols for status codes
  • Loading branch information
dblock committed Mar 10, 2015
2 parents 81c986a + 244671c commit 95331d6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
* [#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).
* [#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).

* Your contribution here.

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ post do
end
```

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)

```ruby
post do
status :no_content
end
```

### Accept-Version Header

```ruby
Expand Down
8 changes: 7 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ def redirect(url, options = {})
#
# @param status [Integer] The HTTP Status Code to return for this request.
def status(status = nil)
if status
if status.is_a? Symbol
if Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
@status = Rack::Utils.status_code(status)
else
fail ArgumentError, "Status code :#{status} is invalid."
end
elsif status
@status = status
else
return @status if @status
Expand Down
10 changes: 10 additions & 0 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def initialize
subject.status 501
expect(subject.status).to eq 501
end

it 'accepts symbol for status' do
subject.status :see_other
expect(subject.status).to eq 303
end

it 'raises error if unknow symbol is passed' do
expect { subject.status :foo_bar }
.to raise_error(ArgumentError, 'Status code :foo_bar is invalid.')
end
end

describe '#header' do
Expand Down

0 comments on commit 95331d6

Please sign in to comment.