Skip to content

Commit

Permalink
Merge pull request #277 from tim-vandecasteele/formatters_via_settings
Browse files Browse the repository at this point in the history
Allow custom formatters to be set with API settings.
  • Loading branch information
dblock committed Nov 20, 2012
2 parents 98a183b + 187da1d commit ba0901c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
4 changes: 4 additions & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba0901c

Please sign in to comment.