Sha256: ce19f6f5f2714aa2b871e3073af42a571907edef3a3cbab60232c2ae49ec2e5a

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

module Fakeit
  module App
    class BodyParser
      class << self
        def parse(request)
          case request.media_type
          when %r{^application/.*json}
            { media_type: request.media_type, data: parse_json(request.body.read) }
          when 'multipart/form-data'
            { media_type: request.media_type, data: parse_form_data(request.params) }
          else
            { media_type: request.media_type, data: request.body.read }
          end
        end

        private

        def parse_json(body)
          JSON.parse(body)
        rescue StandardError
          raise Fakeit::Validation::ValidationError, 'Invalid json payload'
        end

        def parse_form_data(params)
          params.transform_values { |v| v.class == Hash && v[:tempfile] ? v[:tempfile].read : v }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fakeit-0.6.0 lib/fakeit/app/body_parser.rb