Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom formatters to be set with API settings. #277

Merged
merged 1 commit into from
Nov 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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