Sha256: 9bdc256255df3812e4210ab54c3851d2713d521e67b0a2a5e6a2b0c3197efda4

Contents?: true

Size: 873 Bytes

Versions: 3

Compression:

Stored size: 873 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)
          body.empty? ? {} : 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

3 entries across 3 versions & 1 rubygems

Version Path
fakeit-0.6.3 lib/fakeit/app/body_parser.rb
fakeit-0.6.2 lib/fakeit/app/body_parser.rb
fakeit-0.6.1 lib/fakeit/app/body_parser.rb