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

- old
+ new

@@ -1,10 +1,11 @@ require 'rack/mount' require 'rack/auth/basic' require 'rack/auth/digest/md5' require 'logger' require 'grape/util/deep_merge' +require 'grape/util/content_types' module Grape # The API class is the primary entry point for # creating Grape APIs.Users should subclass this # class in order to build an API. @@ -127,21 +128,31 @@ # Specify the format for the API's serializers. # May be `:json`, `:xml`, `:txt`, etc. def format(new_format = nil) if new_format set(:format, new_format.to_sym) + # define the default error formatters set(:default_error_formatter, Grape::ErrorFormatter::Base.formatter_for(new_format, {})) + # define a single mime type + mime_type = content_types[new_format.to_sym] + raise "missing mime type for #{new_format}" unless mime_type + settings.imbue(:content_types, new_format.to_sym => mime_type) 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 custom parser for a content-type. + def parser(content_type, new_parser) + settings.imbue(:parsers, content_type.to_sym => new_parser) + 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 @@ -153,10 +164,15 @@ # content_type :xls, 'application/vnd.ms-excel' def content_type(key, val) settings.imbue(:content_types, key.to_sym => val) end + # All available content types. + def content_types + Grape::ContentTypes.content_types_for(settings[:content_types]) + end + # Specify the default status code for errors. def default_error_status(new_status = nil) new_status ? set(:default_error_status, new_status) : settings[:default_error_status] end @@ -281,10 +297,9 @@ mounts = {mounts => '/'} unless mounts.respond_to?(:each_pair) mounts.each_pair do |app, path| if app.respond_to?(:inherit_settings) app.inherit_settings(settings.clone) end - endpoints << Grape::Endpoint.new(settings.clone, :method => :any, :path => path, :app => app )