Sha256: ddc519fd0e0aedd2456e1bf1c062e9821cac3ae8b4dbf68c2d3f276ad31f32b3

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require_relative "errors"

module Hanami
  module Middleware
    # HTTP request body parser
    class BodyParser
      # @api private
      # @since 1.3.0
      module ClassInterface
        # @api private
        # @since 1.3.0
        def for(parser)
          parser =
            case parser
            when String, Symbol
              require_parser(parser)
            when Class
              parser.new
            else
              parser
            end

          ensure_parser parser

          parser
        end

        private

        # @api private
        # @since 1.3.0
        PARSER_METHODS = %i[mime_types parse].freeze

        # @api private
        # @since 1.3.0
        def ensure_parser(parser)
          raise InvalidParserError.new(parser) unless PARSER_METHODS.all? { |method| parser.respond_to?(method) }
        end

        # @api private
        # @since 1.3.0
        def require_parser(parser)
          require "hanami/middleware/body_parser/#{parser}_parser"

          load_parser!("#{classify(parser)}Parser").new
        rescue LoadError, NameError
          raise UnknownParserError.new(parser)
        end

        def classify(parser)
          parser.to_s.split(/_/).map(&:capitalize).join
        end

        def load_parser!(class_name)
          Hanami::Middleware::BodyParser.const_get(class_name, false)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hanami-router-2.0.0.beta2 lib/hanami/middleware/body_parser/class_interface.rb
hanami-router-2.0.0.beta1 lib/hanami/middleware/body_parser/class_interface.rb
hanami-router-2.0.0.alpha6 lib/hanami/middleware/body_parser/class_interface.rb
hanami-router-2.0.0.alpha5 lib/hanami/middleware/body_parser/class_interface.rb
hanami-router-2.0.0.alpha4 lib/hanami/middleware/body_parser/class_interface.rb