Skip to content

Commit ba0901c

Browse files
committed
Merge pull request #277 from tim-vandecasteele/formatters_via_settings
Allow custom formatters to be set with API settings.
2 parents 98a183b + 187da1d commit ba0901c

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

CHANGELOG.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
* [#265](https://github.com/intridea/grape/issues/264): Fix: Moved `ValidationError` into `Grape::Exceptions` - [@thepumpkin1979](https://github.com/thepumpkin1979).
55
* [#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).
6-
* [#86] (https://github.com/intridea/grape/issues/275): Fix Path-based versioning not recognizing '/' route - [@walski](https://github.com/walski).
6+
* [#86](https://github.com/intridea/grape/issues/275): Fix Path-based versioning not recognizing '/' route - [@walski](https://github.com/walski).
7+
* [#277](https://github.com/intridea/grape/pull/277): Added a DSL to declare `formatter` in API settings - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
78
* Your contribution here.
89

910
0.2.2

README.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,12 @@ Serialization takes place automatically.
589589
Your API can declare additional types to support. Response format is determined by the
590590
request's extension, an explicit `format` parameter in the query string, or `Accept` header.
591591

592+
Custom formatters for additional types can be defined with a proc or by method pointer.
593+
592594
``` ruby
593595
class Twitter::API < Grape::API
594596
content_type :xls, "application/vnd.ms-excel"
597+
formatter :xls, lambda { |object| object.to_fancy_xls }
595598
end
596599
```
597600

lib/grape/api.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def format(new_format = nil)
130130
new_format ? set(:format, new_format.to_sym) : settings[:format]
131131
end
132132

133+
def formatter(content_type, new_formatter)
134+
settings.imbue(:formatters, content_type.to_sym => new_formatter)
135+
end
136+
133137
# Specify the format for error messages.
134138
# May be `:json` or `:txt` (default).
135139
def error_format(new_format = nil)

lib/grape/endpoint.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ def build_middleware
395395
b.use Grape::Middleware::Formatter,
396396
:format => settings[:format],
397397
:default_format => settings[:default_format] || :txt,
398-
:content_types => settings[:content_types]
398+
:content_types => settings[:content_types],
399+
:formatters => settings[:formatters]
399400

400401
aggregate_setting(:middleware).each do |m|
401402
m = m.dup

spec/grape/api_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,26 @@ class CommunicationError < RuntimeError; end
946946
end
947947
end
948948

949+
describe ".formatter" do
950+
context "multiple formatters" do
951+
before :each do
952+
subject.formatter :json, lambda { |object| "{\"custom_formatter\":\"#{object[:some]}\"}" }
953+
subject.formatter :txt, lambda { |object| "custom_formatter: #{object[:some]}" }
954+
subject.get :simple do
955+
{:some => 'hash'}
956+
end
957+
end
958+
it 'sets one formatter' do
959+
get '/simple.json'
960+
last_response.body.should eql '{"custom_formatter":"hash"}'
961+
end
962+
it 'sets another formatter' do
963+
get '/simple.txt'
964+
last_response.body.should eql 'custom_formatter: hash'
965+
end
966+
end
967+
end
968+
949969
describe ".default_error_status" do
950970
it 'should allow setting default_error_status' do
951971
subject.rescue_from :all

0 commit comments

Comments
 (0)