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
-
.parsers ⇒ Object
readonly
Returns the value of attribute parsers.
Class Method Summary collapse
-
.[](media_type) ⇒ #call?
The parser, or nil if none is registered.
- .register(matcher, parser) ⇒ Object
Class Attribute Details
.parsers ⇒ Object (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.
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
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 |