lib/grape/api.rb in grape-0.2.3 vs lib/grape/api.rb in grape-0.2.4

- old
+ new

@@ -123,20 +123,30 @@ def default_format(new_format = nil) new_format ? set(:default_format, new_format.to_sym) : settings[:default_format] end # Specify the format for the API's serializers. - # May be `:json` or `:txt`. + # May be `:json`, `:xml`, `:txt`, etc. def format(new_format = nil) - new_format ? set(:format, new_format.to_sym) : settings[:format] + if new_format + set(:format, new_format.to_sym) + set(:default_error_formatter, Grape::ErrorFormatter::Base.formatter_for(new_format, {})) + else + settings[:format] + end end # Specify a custom formatter for a content-type. def formatter(content_type, new_formatter) settings.imbue(:formatters, content_type.to_sym => new_formatter) end + # Specify a default error formatter. + def default_error_formatter(new_formatter = nil) + new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter] + end + def error_formatter(format, new_formatter) settings.imbue(:error_formatters, format.to_sym => new_formatter) end # Specify additional content-types, e.g.: @@ -207,20 +217,22 @@ # endpoint within this namespace (and child namespaces). # # When called without a block, all known helpers within this scope # are included. # - # @param mod [Module] optional module of methods to include - # @param &block [Block] optional block of methods to include + # @param [Module] new_mod optional module of methods to include + # @param [Block] block optional block of methods to include # # @example Define some helpers. + # # class ExampleAPI < Grape::API # helpers do # def current_user # User.find_by_id(params[:token]) # end # end # end + # def helpers(new_mod = nil, &block) if block_given? || new_mod mod = settings.peek[:helpers] || Module.new if new_mod mod.class_eval do