Module: OpenapiParameters::ContentParsers

Defined in:
lib/openapi_parameters/content_parsers.rb

Overview

Registry of content parsers keyed by media type.

A parser is a callable that takes a raw string and returns the parsed value. It should ‘throw :skip, value` if the input cannot be parsed, so the parameter falls through gracefully at the location class level.

Built-in: ‘application/json` and any `*+json` subtype.

To register your own:

OpenapiParameters::ContentParsers.register('application/xml', ->(v) { ... })

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.parsersObject (readonly)

Returns the value of attribute parsers.



21
22
23
# File 'lib/openapi_parameters/content_parsers.rb', line 21

def parsers
  @parsers
end

Class Method Details

.[](media_type) ⇒ #call?

Returns the parser, or nil if none is registered.

Parameters:

  • media_type (String, nil)

Returns:

  • (#call, nil)

    the parser, or nil if none is registered.



32
33
34
35
36
37
38
39
# File 'lib/openapi_parameters/content_parsers.rb', line 32

def [](media_type)
  return nil if media_type.nil?

  parsers.each do |matcher, parser|
    return parser if match?(matcher, media_type)
  end
  nil
end

.register(matcher, parser) ⇒ Object

Parameters:

  • matcher (String, Regexp)

    exact media type or a pattern.

  • parser (#call)

    callable that takes the raw string and returns the parsed value.



25
26
27
28
# File 'lib/openapi_parameters/content_parsers.rb', line 25

def register(matcher, parser)
  parsers.reject! { |existing, _| existing == matcher }
  parsers << [matcher, parser]
end