Module: Grape::DSL::RequestResponse::ClassMethods
- Defined in:
- lib/grape/dsl/request_response.rb
Instance Method Summary collapse
-
#content_type(key, val) ⇒ Object
Specify additional content-types, e.g.: content_type :xls, ‘application/vnd.ms-excel’.
-
#content_types ⇒ Object
All available content types.
-
#default_error_formatter(new_formatter_name = nil) ⇒ Object
Specify a default error formatter.
-
#default_error_status(new_status = nil) ⇒ Object
Specify the default status code for errors.
-
#default_format(new_format = nil) ⇒ Object
Specify the default format for the API’s serializers.
- #error_formatter(format, options) ⇒ Object
-
#format(new_format = nil) ⇒ Object
Specify the format for the API’s serializers.
-
#formatter(content_type, new_formatter) ⇒ Object
Specify a custom formatter for a content-type.
-
#parser(content_type, new_parser) ⇒ Object
Specify a custom parser for a content-type.
-
#represent(model_class, options) ⇒ Object
Allows you to specify a default representation entity for a class.
-
#rescue_from(*exception_classes, **options) ⇒ Object
Allows you to rescue certain exceptions that occur to return a grape error rather than raising all the way to the server level.
Instance Method Details
#content_type(key, val) ⇒ Object
Specify additional content-types, e.g.:
content_type :xls, 'application/vnd.ms-excel'
66 67 68 |
# File 'lib/grape/dsl/request_response.rb', line 66 def content_type(key, val) namespace_stackable(:content_types, key.to_sym => val) end |
#content_types ⇒ Object
All available content types.
71 72 73 74 |
# File 'lib/grape/dsl/request_response.rb', line 71 def content_types c_types = namespace_stackable_with_hash(:content_types) Grape::ContentTypes.content_types_for c_types end |
#default_error_formatter(new_formatter_name = nil) ⇒ Object
Specify a default error formatter.
45 46 47 48 49 50 51 52 |
# File 'lib/grape/dsl/request_response.rb', line 45 def default_error_formatter(new_formatter_name = nil) if new_formatter_name new_formatter = Grape::ErrorFormatter.formatter_for(new_formatter_name, **{}) namespace_inheritable(:default_error_formatter, new_formatter) else namespace_inheritable(:default_error_formatter) end end |
#default_error_status(new_status = nil) ⇒ Object
Specify the default status code for errors.
77 78 79 |
# File 'lib/grape/dsl/request_response.rb', line 77 def default_error_status(new_status = nil) namespace_inheritable(:default_error_status, new_status) end |
#default_format(new_format = nil) ⇒ Object
Specify the default format for the API’s serializers. May be ‘:json` or `:txt` (default).
13 14 15 |
# File 'lib/grape/dsl/request_response.rb', line 13 def default_format(new_format = nil) namespace_inheritable(:default_format, new_format.nil? ? nil : new_format.to_sym) end |
#error_formatter(format, options) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/grape/dsl/request_response.rb', line 54 def error_formatter(format, ) formatter = if .is_a?(Hash) && .key?(:with) [:with] else end namespace_stackable(:error_formatters, format.to_sym => formatter) end |
#format(new_format = nil) ⇒ Object
Specify the format for the API’s serializers. May be ‘:json`, `:xml`, `:txt`, etc.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/grape/dsl/request_response.rb', line 19 def format(new_format = nil) if new_format namespace_inheritable(:format, new_format.to_sym) # define the default error formatters namespace_inheritable(:default_error_formatter, Grape::ErrorFormatter.formatter_for(new_format, **{})) # define a single mime type mime_type = content_types[new_format.to_sym] raise Grape::Exceptions::MissingMimeType.new(new_format) unless mime_type namespace_stackable(:content_types, new_format.to_sym => mime_type) else namespace_inheritable(:format) end end |
#formatter(content_type, new_formatter) ⇒ Object
Specify a custom formatter for a content-type.
35 36 37 |
# File 'lib/grape/dsl/request_response.rb', line 35 def formatter(content_type, new_formatter) namespace_stackable(:formatters, content_type.to_sym => new_formatter) end |
#parser(content_type, new_parser) ⇒ Object
Specify a custom parser for a content-type.
40 41 42 |
# File 'lib/grape/dsl/request_response.rb', line 40 def parser(content_type, new_parser) namespace_stackable(:parsers, content_type.to_sym => new_parser) end |
#represent(model_class, options) ⇒ Object
Allows you to specify a default representation entity for a class. This allows you to map your models to their respective entities once and then simply call ‘present` with the model.
Note that Grape will automatically go up the class ancestry to try to find a representing entity, so if you, for example, define an entity to represent ‘Object` then all presented objects will bubble up and utilize the entity provided on that `represent` call.
154 155 156 157 158 |
# File 'lib/grape/dsl/request_response.rb', line 154 def represent(model_class, ) raise Grape::Exceptions::InvalidWithOptionForRepresent.new unless [:with].is_a?(Class) namespace_stackable(:representations, model_class => [:with]) end |
#rescue_from(*exception_classes, **options) ⇒ Object
Allows you to rescue certain exceptions that occur to return a grape error rather than raising all the way to the server level.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/grape/dsl/request_response.rb', line 101 def rescue_from(*args, &block) if args.last.is_a?(Proc) handler = args.pop elsif block handler = block end = args. raise ArgumentError, 'both :with option and block cannot be passed' if block && .key?(:with) handler ||= extract_with() 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) else handler_type = case [: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, ) end |