Sha256: c50c3759aaf077f9f38f3cbf134b081d19ece43bc7184bcd2b8a5c8a312f6dec

Contents?: true

Size: 951 Bytes

Versions: 7

Compression:

Stored size: 951 Bytes

Contents

module Fakeit
  module App
    module Helpers
      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.instance_of?(Hash) && v[:tempfile] ? v[:tempfile].read : v }
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fakeit-0.11.0 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.10.0 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.9.2 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.9.1 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.9.0 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.8.1 lib/fakeit/app/helpers/body_parser.rb
fakeit-0.8.0 lib/fakeit/app/helpers/body_parser.rb