class Rack::Multipart::Generator - rack-1.6.2 Documentation

# class Rack::Multipart::Generator

### Public Class Methods

new(params, first = true) click to toggle source

    # File lib/rack/multipart/generator.rb, line 4
    def initialize(params, first = true)
      @params, @first = params, first

      if @first && !@params.is_a?(Hash)
        raise ArgumentError, "value must be a Hash"
      end
    end

### Public Instance Methods

dump() click to toggle source

    # File lib/rack/multipart/generator.rb, line 12
    def dump
      return nil if @first && !multipart?
      return flattened_params if !@first

      flattened_params.map do |name, file|
        if file.respond_to?(:original_filename)
          ::File.open(file.path, "rb") do |f|
            f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
            content_for_tempfile(f, file, name)
          end
        else
          content_for_other(file, name)
        end
      end.join + "--#{MULTIPART_BOUNDARY}--\r"
    end

### Private Instance Methods

content_for_other(file, name) click to toggle source

    # File lib/rack/multipart/generator.rb, line 83
          def content_for_other(file, name)
    <<-EOF
    --#{MULTIPART_BOUNDARY}\r
    Content-Disposition: form-data; name="#{name}"\r
    \r
    #{file}\r
    EOF
          end

content_for_tempfile(io, file, name) click to toggle source

    # File lib/rack/multipart/generator.rb, line 72
          def content_for_tempfile(io, file, name)
    <<-EOF
    --#{MULTIPART_BOUNDARY}\r
    Content-Disposition: form-data; name="#{name}"; filename="#{Utils.escape(file.original_filename)}"\r
    Content-Type: #{file.content_type}\r
    Content-Length: #{::File.stat(file.path).size}\r
    \r
    #{io.read}\r
    EOF
          end

flattened_params() click to toggle source

    # File lib/rack/multipart/generator.rb, line 47
    def flattened_params
      @flattened_params ||= begin
        h = Hash.new
        @params.each do |key, value|
          k = @first ? key.to_s : "[#{key}]"

          case value
          when Array
            value.map { |v|
              Multipart.build_multipart(v, false).each { |subkey, subvalue|
                h["#{k}[]#{subkey}"] = subvalue
              }
            }
          when Hash
            Multipart.build_multipart(value, false).each { |subkey, subvalue|
              h[k + subkey] = subvalue
            }
          else
            h[k] = value
          end
        end
        h
      end
    end

multipart?() click to toggle source

    # File lib/rack/multipart/generator.rb, line 29
    def multipart?
      multipart = false

      query = lambda { |value|
        case value
        when Array
          value.each(&query)
        when Hash
          value.values.each(&query)
        when Rack::Multipart::UploadedFile
          multipart = true
        end
      }
      @params.values.each(&query)

      multipart
    end