Skip to content

Commit 5f5cdce

Browse files
committed
Added both format and default_format.
1 parent d0644ec commit 5f5cdce

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ end
268268
You can also set the default format. The order for choosing the format is the following.
269269

270270
* Use the file extension, if specified. If the file is .json, choose the JSON format.
271-
* Use the default format, if specified by the `default_format` option.
271+
* Use the format, if specified by the `format` option.
272272
* Attempt to find an acceptable format from the `Accept` header.
273+
* Use the default format, if specified by the `default_format` option.
273274
* Default to `:txt` otherwise.
274275

275276
```ruby
276277
class Twitter::API < Grape::API
278+
format :json
277279
defalt_format :json
278280
end
279281
```

lib/grape/api.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def default_format(new_format = nil)
115115
new_format ? set(:default_format, new_format.to_sym) : settings[:default_format]
116116
end
117117

118+
# Specify the format for the API's serializers.
119+
# May be `:json` or `:txt`.
120+
def format(new_format = nil)
121+
new_format ? set(:format, new_format.to_sym) : settings[:format]
122+
end
123+
118124
# Specify the format for error messages.
119125
# May be `:json` or `:txt` (default).
120126
def error_format(new_format = nil)

lib/grape/endpoint.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def build_middleware
275275
end
276276

277277
b.use Grape::Middleware::Formatter,
278-
:format => settings[:default_format],
278+
:format => settings[:format],
279+
:default_format => settings[:default_format] || :txt,
279280
:content_types => settings[:content_types]
280281

281282
aggregate_setting(:middleware).each do |m|

lib/grape/middleware/formatter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Formatter < Base
77

88
def default_options
99
{
10+
:default_format => :txt,
1011
:formatters => {},
1112
:content_types => {},
1213
:parsers => {}
@@ -18,7 +19,7 @@ def headers
1819
end
1920

2021
def before
21-
fmt = format_from_extension || options[:format] || format_from_header || :txt
22+
fmt = format_from_extension || options[:format] || format_from_header || options[:default_format]
2223
if content_types.key?(fmt)
2324
if !env['rack.input'].nil? and (body = env['rack.input'].read).strip.length != 0
2425
parser = parser_for fmt

spec/grape/api_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ class CommunicationError < RuntimeError; end
10581058
context "format" do
10591059
context ":txt" do
10601060
before(:each) do
1061-
subject.default_format :txt
1061+
subject.format :txt
10621062
subject.get '/meaning_of_life' do
10631063
{ :meaning_of_life => 42 }
10641064
end
@@ -1078,7 +1078,7 @@ class CommunicationError < RuntimeError; end
10781078
end
10791079
context ":json" do
10801080
before(:each) do
1081-
subject.default_format :json
1081+
subject.format :json
10821082
subject.get '/meaning_of_life' do
10831083
{ :meaning_of_life => 42 }
10841084
end

0 commit comments

Comments
 (0)