module Grape::DSL::RequestResponse::ClassMethods
def content_type(key, val)
Specify additional content-types, e.g.:
def content_type(key, val) namespace_stackable(:content_types, key.to_sym => val) end
def content_types
def content_types c_types = namespace_stackable_with_hash(:content_types) Grape::ContentTypes.content_types_for c_types end
def default_error_formatter(new_formatter_name = nil)
def default_error_formatter(new_formatter_name = nil) return namespace_inheritable(:default_error_formatter) unless new_formatter_name new_formatter = Grape::ErrorFormatter.formatter_for(new_formatter_name) namespace_inheritable(:default_error_formatter, new_formatter) end
def default_error_status(new_status = nil)
def default_error_status(new_status = nil) namespace_inheritable(:default_error_status, new_status) end
def default_format(new_format = nil)
Specify the default format for the API's serializers.
def default_format(new_format = nil) namespace_inheritable(:default_format, new_format.nil? ? nil : new_format.to_sym) end
def error_formatter(format, options)
def error_formatter(format, options) formatter = if options.is_a?(Hash) && options.key?(:with) options[:with] else options end namespace_stackable(:error_formatters, format.to_sym => formatter) end
def extract_with(options)
def extract_with(options) return unless options.key?(:with) with_option = options.delete(:with) return with_option if with_option.instance_of?(Proc) return with_option.to_sym if with_option.instance_of?(Symbol) || with_option.instance_of?(String) raise ArgumentError, "with: #{with_option.class}, expected Symbol, String or Proc" end
def format(new_format = nil)
Specify the format for the API's serializers.
def format(new_format = nil) return namespace_inheritable(:format) unless new_format symbolic_new_format = new_format.to_sym namespace_inheritable(:format, symbolic_new_format) namespace_inheritable(:default_error_formatter, Grape::ErrorFormatter.formatter_for(symbolic_new_format)) content_type = content_types[symbolic_new_format] raise Grape::Exceptions::MissingMimeType.new(new_format) unless content_type namespace_stackable(:content_types, symbolic_new_format => content_type) end
def formatter(content_type, new_formatter)
def formatter(content_type, new_formatter) namespace_stackable(:formatters, content_type.to_sym => new_formatter) end
def parser(content_type, new_parser)
def parser(content_type, new_parser) namespace_stackable(:parsers, content_type.to_sym => new_parser) end
def represent(model_class, options)
(**options)
-
:with
(Class
) -- The entity class that will represent the model.
Parameters:
-
model_class
(Class
) -- The model class that will be represented.
def represent(model_class, options) raise Grape::Exceptions::InvalidWithOptionForRepresent.new unless options[:with].is_a?(Class) namespace_stackable(:representations, model_class => options[:with]) end
def rescue_from(*args, &block)
-
handler
(Proc
) -- Execution proc to handle the given exception as an -
options
(Hash
) -- Options for the rescue usage. -
block
(Block
) -- Execution block to handle the given exception. -
exception_classes
(Array
) -- A list of classes that you want to rescue, or
Options Hash:
(**options)
-
:rescue_subclasses
(Boolean
) -- Also rescue subclasses of exception classes -
:backtrace
(Boolean
) -- Include a backtrace in the rescue response.
Overloads:
-
rescue_from(*exception_classes, **options)
Other tags:
- Example: Rescue from custom exceptions -
def rescue_from(*args, &block) if args.last.is_a?(Proc) handler = args.pop elsif block handler = block end options = args.extract_options! raise ArgumentError, 'both :with option and block cannot be passed' if block && options.key?(:with) handler ||= extract_with(options) if args.include?(:all) namespace_inheritable(:rescue_all, true) namespace_inheritable(:all_rescue_handler, handler) elsif args.include?(:grape_exceptions) namespace_inheritable(:rescue_all, true) namespace_inheritable(:rescue_grape_exceptions, true) namespace_inheritable(:grape_exceptions_rescue_handler, handler) else handler_type = case options[:rescue_subclasses] when nil, true :rescue_handlers else :base_only_rescue_handlers end namespace_reverse_stackable(handler_type, args.to_h { |arg| [arg, handler] }) end namespace_stackable(:rescue_options, options) end