Sha256: 62df75023a63596a44884a2b533a6a5a58954fdf616026466be72de1f09bb4a7

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true

module Grape
  module Validations
    module Types
      # Implementation for parameters that are multipart file objects.
      # Actual handling of these objects is provided by +Rack::Request+;
      # this class is here only to assert that rack's handling has succeeded.
      class File
        def call(input)
          return if input.nil?
          return InvalidValue.new unless coerced?(input)

          # Processing of multipart file objects
          # is already taken care of by Rack::Request.
          # Nothing to do here.
          input
        end

        def coerced?(value)
          # Rack::Request creates a Hash with filename,
          # content type and an IO object. Do a bit of basic
          # duck-typing.
          value.is_a?(::Hash) && value.key?(:tempfile) && value[:tempfile].is_a?(Tempfile)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-1.3.1 lib/grape/validations/types/file.rb