lib/hanami/middleware/body_parser.rb in hanami-router-1.3.0.beta1 vs lib/hanami/middleware/body_parser.rb in hanami-router-1.3.0
- old
+ new
@@ -1,69 +1,68 @@
-require 'hanami/middleware/body_parser/parser'
require 'hanami/utils/hash'
+require_relative 'body_parser/class_interface'
module Hanami
module Middleware
# @since 1.3.0
# @api private
class BodyParser
# @since 1.3.0
# @api private
- CONTENT_TYPE = 'CONTENT_TYPE'.freeze
+ CONTENT_TYPE = 'CONTENT_TYPE'.freeze
# @since 1.3.0
# @api private
MEDIA_TYPE_MATCHER = /\s*[;,]\s*/.freeze
# @since 1.3.0
# @api private
- RACK_INPUT = 'rack.input'.freeze
+ RACK_INPUT = 'rack.input'.freeze
# @since 1.3.0
# @api private
ROUTER_PARAMS = 'router.params'.freeze
# @api private
ROUTER_PARSED_BODY = 'router.parsed_body'.freeze
# @api private
- FALLBACK_KEY = '_'.freeze
+ FALLBACK_KEY = '_'.freeze
+ extend ClassInterface
+
def initialize(app, parsers)
@app = app
@parsers = build_parsers(parsers)
end
def call(env)
body = env[RACK_INPUT].read
return @app.call(env) if body.empty?
- env[RACK_INPUT].rewind # somebody might try to read this stream
+ env[RACK_INPUT].rewind # somebody might try to read this stream
- env[ROUTER_PARAMS] ||= {} # prepare params
- env[ROUTER_PARSED_BODY] = _parse(env, body)
- env[ROUTER_PARAMS] = _symbolize(env[ROUTER_PARSED_BODY]).merge(env[ROUTER_PARAMS])
+ if (parser = @parsers[media_type(env)])
+ env[ROUTER_PARSED_BODY] = parser.parse(body)
+ env[ROUTER_PARAMS] = _symbolize(env[ROUTER_PARSED_BODY])
+ end
@app.call(env)
end
private
- def build_parsers(parsers)
- result = Hash.new
- args = Array(parsers)
- return result if args.empty?
+ def build_parsers(parser_names)
+ parser_names = Array(parser_names)
+ return {} if parser_names.empty?
- args.each do |arg|
- parser = Parser.for(arg)
+ parser_names.each_with_object({}) { |name, parsers|
+ parser = self.class.for(name)
parser.mime_types.each do |mime|
- result[mime] = parser
+ parsers[mime] = parser
end
- end
-
- result.default = Parser.new
- result
+ }
end
# @api private
def _symbolize(body)
if body.is_a?(Hash)