Skip to content

Commit d7b2dd5

Browse files
committed
Using Rack::Utils to parse query string.
1 parent c61a661 commit d7b2dd5

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Next Release
1313
* [#204](https://github.com/intridea/grape/pull/204): Added ability to declare shared parameters at namespace level - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
1414
* [#208](https://github.com/intridea/grape/pull/208): `Entity#serializable_hash` must also check if attribute is generated by a user supplied block - [@ppadron](https://github.com/ppadron).
1515
* [#234](https://github.com/intridea/grape/pull/234): Adds a DSL for creating entities via mixin - [@mbleigh](https://github.com/mbleigh).
16+
* [#240](https://github.com/intridea/grape/pull/240): Define API response format from a query string `format` parameter, if specified - [@neetiraj](https://github.com/neetiraj).
1617

1718
0.2.1 (7/11/2012)
1819
=================

README.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ By default, Grape supports _XML_, _JSON_, _Atom_, _RSS_, and _text_ content-type
538538
Serialization takes place automatically.
539539

540540
Your API can declare additional types to support. Response format is determined by the
541-
request's extension or `Accept` header.
541+
request's extension, an explicit `format` parameter in the query string, or `Accept` header.
542542

543543
``` ruby
544544
class Twitter::API < Grape::API
@@ -549,7 +549,8 @@ end
549549
You can also set the default format. The order for choosing the format is the following.
550550

551551
* Use the file extension, if specified. If the file is .json, choose the JSON format.
552-
* Use the format, if specified by the `format` option.
552+
* Use the value of the format parameter in the query string, if specified.
553+
* Use the format set by the `format` option, if specified.
553554
* Attempt to find an acceptable format from the `Accept` header.
554555
* Use the default format, if specified by the `default_format` option.
555556
* Default to `:txt` otherwise.

lib/grape/middleware/formatter.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ def format_from_extension
5151
end
5252

5353
def format_from_params
54-
if env['QUERY_STRING']
55-
format_query = env['QUERY_STRING'].split('&').reject{|q| !q.include?('format=')}
56-
return format_query[0].split('=').last.to_sym if (format_query && !format_query.empty?)
57-
else
58-
nil
59-
end
54+
fmt = Rack::Utils.parse_nested_query(env['QUERY_STRING'])["format"]
55+
fmt ? fmt.to_sym : nil
6056
end
6157

6258
def format_from_header

0 commit comments

Comments
 (0)