class HTTP::FormData::Multipart::Param

Utility class to represent multi-part chunks

Public Class Methods

coerce(data) click to toggle source

Flattens given `data` Hash into an array of `Param`'s. Nested array are unwinded. Behavior is similar to `URL.encode_www_form`.

@param [Hash] data @return [Array<FormData::MultiPart::Param>]

# File lib/http/form_data/multipart/param.rb, line 49
def self.coerce(data)
  params = []

  data.each do |name, values|
    Array(values).each do |value|
      params << new(name, value)
    end
  end

  params
end
new(name, value) click to toggle source

Initializes body part with headers and data.

@example With {FormData::File} value

Content-Disposition: form-data; name="avatar"; filename="avatar.png"
Content-Type: application/octet-stream

...data of avatar.png...

@example With non-{FormData::File} value

Content-Disposition: form-data; name="username"

ixti

@return [String] @param [#to_s] name @param [FormData::File, FormData::Part, to_s] value

# File lib/http/form_data/multipart/param.rb, line 30
def initialize(name, value)
  @name = name.to_s

  @part =
    if value.is_a?(FormData::Part)
      value
    else
      FormData::Part.new(value)
    end

  @io = CompositeIO.new [header, @part, footer]
end

Private Instance Methods

content_type() click to toggle source
# File lib/http/form_data/multipart/param.rb, line 77
def content_type
  @part.content_type
end
filename() click to toggle source
# File lib/http/form_data/multipart/param.rb, line 81
def filename
  @part.filename
end
header() click to toggle source
# File lib/http/form_data/multipart/param.rb, line 63
def header
  header = "".b
  header << "Content-Disposition: form-data; #{parameters}#{CRLF}"
  header << "Content-Type: #{content_type}#{CRLF}" if content_type
  header << CRLF
  header
end
parameters() click to toggle source
# File lib/http/form_data/multipart/param.rb, line 71
def parameters
  parameters = { :name => @name }
  parameters[:filename] = filename if filename
  parameters.map { |k, v| "#{k}=#{v.inspect}" }.join("; ")
end