File tree Expand file tree Collapse file tree 5 files changed +15
-5
lines changed
Expand file tree Collapse file tree 5 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -268,12 +268,14 @@ end
268268You 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
276277class Twitter ::API < Grape ::API
278+ format :json
277279 defalt_format :json
278280end
279281```
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments