Permalink
Browse files

Merge pull request #277 from tim-vandecasteele/formatters_via_settings

Allow custom formatters to be set with API settings.
  • Loading branch information...
2 parents 98a183b + 187da1d commit ba0901cbecb73e94052386821cfc6d4c79707621 @dblock dblock committed Nov 20, 2012
Showing with 31 additions and 2 deletions.
  1. +2 −1 CHANGELOG.markdown
  2. +3 −0 README.markdown
  3. +4 −0 lib/grape/api.rb
  4. +2 −1 lib/grape/endpoint.rb
  5. +20 −0 spec/grape/api_spec.rb
View
@@ -3,7 +3,8 @@
* [#265](https://github.com/intridea/grape/issues/264): Fix: Moved `ValidationError` into `Grape::Exceptions` - [@thepumpkin1979](https://github.com/thepumpkin1979).
* [#269](https://github.com/intridea/grape/pull/269): Fix: `LocalJumpError` will not be raised when using explict return in API methods - [@simulacre](https://github.com/simulacre).
-* [#86] (https://github.com/intridea/grape/issues/275): Fix Path-based versioning not recognizing '/' route - [@walski](https://github.com/walski).
+* [#86](https://github.com/intridea/grape/issues/275): Fix Path-based versioning not recognizing '/' route - [@walski](https://github.com/walski).
+* [#277](https://github.com/intridea/grape/pull/277): Added a DSL to declare `formatter` in API settings - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
* Your contribution here.
0.2.2
View
@@ -589,9 +589,12 @@ Serialization takes place automatically.
Your API can declare additional types to support. Response format is determined by the
request's extension, an explicit `format` parameter in the query string, or `Accept` header.
+Custom formatters for additional types can be defined with a proc or by method pointer.
+
``` ruby
class Twitter::API < Grape::API
content_type :xls, "application/vnd.ms-excel"
+ formatter :xls, lambda { |object| object.to_fancy_xls }
end
```
View
@@ -130,6 +130,10 @@ def format(new_format = nil)
new_format ? set(:format, new_format.to_sym) : settings[:format]
end
+ def formatter(content_type, new_formatter)
+ settings.imbue(:formatters, content_type.to_sym => new_formatter)
+ end
+
# Specify the format for error messages.
# May be `:json` or `:txt` (default).
def error_format(new_format = nil)
View
@@ -395,7 +395,8 @@ def build_middleware
b.use Grape::Middleware::Formatter,
:format => settings[:format],
:default_format => settings[:default_format] || :txt,
- :content_types => settings[:content_types]
+ :content_types => settings[:content_types],
+ :formatters => settings[:formatters]
aggregate_setting(:middleware).each do |m|
m = m.dup
View
@@ -946,6 +946,26 @@ class CommunicationError < RuntimeError; end
end
end
+ describe ".formatter" do
+ context "multiple formatters" do
+ before :each do
+ subject.formatter :json, lambda { |object| "{\"custom_formatter\":\"#{object[:some]}\"}" }
+ subject.formatter :txt, lambda { |object| "custom_formatter: #{object[:some]}" }
+ subject.get :simple do
+ {:some => 'hash'}
+ end
+ end
+ it 'sets one formatter' do
+ get '/simple.json'
+ last_response.body.should eql '{"custom_formatter":"hash"}'
+ end
+ it 'sets another formatter' do
+ get '/simple.txt'
+ last_response.body.should eql 'custom_formatter: hash'
+ end
+ end
+ end
+
describe ".default_error_status" do
it 'should allow setting default_error_status' do
subject.rescue_from :all

0 comments on commit ba0901c

Please sign in to comment.