Module: Hanami::Middleware::BodyParser::ClassInterface Private

Included in:
Hanami::Middleware::BodyParser
Defined in:
lib/hanami/middleware/body_parser/class_interface.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

Instance Method Summary collapse

Instance Method Details

#build(parser, **config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hanami/middleware/body_parser/class_interface.rb', line 37

def build(parser, **config)
  parser =
    case parser
    when String, Symbol
      build(parser_class(parser), **config)
    when Class
      parser.new(**config)
    else
      parser
    end

  ensure_parser parser

  parser
end

#build_parsers(parser_specs, registry = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hanami/middleware/body_parser/class_interface.rb', line 55

def build_parsers(parser_specs, registry = {})
  return DEFAULT_BODY_PARSERS if parser_specs.empty?

  parsers = Array(parser_specs).flatten(0)

  parsers.each_with_object(registry) do |spec, memo|
    if spec.is_a?(Hash) && spec.size > 1
      spec.each do |key, value|
        build_parsers([key => [value]], memo)
      end
    else
      name, *mime_types = Array(*spec).flatten(0)

      parser = build(name, mime_types: mime_types.flatten)

      parser.mime_types.each do |mime|
        memo[mime] = parser
      end
    end
  end
end

#new(app, parser_specs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate a new body parser instance and load its parsers

Examples:

Hanami::Middleware::BodyParser.new(->(env) { [200, {}, "app"] }, :json)

Hanami::Middleware::BodyParser.new(
  ->(env) { [200, {}, "app"] }, [json: "application/json+scim"]
)

Hanami::Middleware::BodyParser.new(
  ->(env) { [200, {}, "app"] }, [json: ["application/json+scim", "application/ld+json"]]
)

Parameters:

  • app (#call)
  • parser_specs (Symbol, Array<Hash>)

    parser name or name with mime-type(s)

Returns:

  • BodyParser

Since:

  • 2.0.0



31
32
33
# File 'lib/hanami/middleware/body_parser/class_interface.rb', line 31

def new(app, parser_specs)
  super(app, build_parsers(parser_specs))
end