Skip to content

Commit

Permalink
Using Rack::Utils to parse query string.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Sep 23, 2012
1 parent c61a661 commit d7b2dd5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Next Release
* [#204](https://github.com/intridea/grape/pull/204): Added ability to declare shared parameters at namespace level - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
* [#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).
* [#234](https://github.com/intridea/grape/pull/234): Adds a DSL for creating entities via mixin - [@mbleigh](https://github.com/mbleigh).
* [#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).

0.2.1 (7/11/2012)
=================
Expand Down
5 changes: 3 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ By default, Grape supports _XML_, _JSON_, _Atom_, _RSS_, and _text_ content-type
Serialization takes place automatically.

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

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

* Use the file extension, if specified. If the file is .json, choose the JSON format.
* Use the format, if specified by the `format` option.
* Use the value of the format parameter in the query string, if specified.
* Use the format set by the `format` option, if specified.
* Attempt to find an acceptable format from the `Accept` header.
* Use the default format, if specified by the `default_format` option.
* Default to `:txt` otherwise.
Expand Down
8 changes: 2 additions & 6 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ def format_from_extension
end

def format_from_params
if env['QUERY_STRING']
format_query = env['QUERY_STRING'].split('&').reject{|q| !q.include?('format=')}
return format_query[0].split('=').last.to_sym if (format_query && !format_query.empty?)
else
nil
end
fmt = Rack::Utils.parse_nested_query(env['QUERY_STRING'])["format"]
fmt ? fmt.to_sym : nil
end

def format_from_header
Expand Down

0 comments on commit d7b2dd5

Please sign in to comment.