Module: Grape::Formatter

Defined in:
lib/grape/formatter.rb,
lib/grape/formatter/txt.rb,
lib/grape/formatter/xml.rb,
lib/grape/formatter/json.rb,
lib/grape/formatter/serializable_hash.rb

Defined Under Namespace

Modules: Json, SerializableHash, Txt, Xml

Constant Summary collapse

DEFAULTS =
{
  json: Grape::Formatter::Json,
  jsonapi: Grape::Formatter::Json,
  serializable_hash: Grape::Formatter::SerializableHash,
  txt: Grape::Formatter::Txt,
  xml: Grape::Formatter::Xml
}.freeze
DEFAULT_LAMBDA_FORMATTER =
->(obj, _env) { obj }

Class Method Summary collapse

Class Method Details

.formatter_for(api_format, formatters) ⇒ Object



17
18
19
# File 'lib/grape/formatter.rb', line 17

def formatter_for(api_format, formatters)
  select_formatter(formatters, api_format) || DEFAULT_LAMBDA_FORMATTER
end

.select_formatter(formatters, api_format) ⇒ Object



21
22
23
# File 'lib/grape/formatter.rb', line 21

def select_formatter(formatters, api_format)
  formatters&.key?(api_format) ? formatters[api_format] : DEFAULTS[api_format]
end