File: file.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (31 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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
        class << self
          def parse(input)
            return if input.nil?
            return InvalidValue.new unless parsed?(input)

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

          def parsed?(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
end