class HTTP::FormData::Multipart

`multipart/form-data` form data.

Public Class Methods

new(data) click to toggle source

@param [#to_h, Hash] data form data key-value Hash

# File lib/http/form_data/multipart.rb, line 12
def initialize(data)
  @parts          = Param.coerce FormData.ensure_hash data
  @boundary       = ("-" * 21) << SecureRandom.hex(21)
  @content_length = nil
end

Public Instance Methods

content_length() click to toggle source

Returns form data content size to be used for HTTP request `Content-Length` header.

@return [Fixnum]

# File lib/http/form_data/multipart.rb, line 36
def content_length
  unless @content_length
    @content_length  = head.bytesize + tail.bytesize
    @content_length += @parts.map(&:size).reduce(:+)
    @content_length += (glue.bytesize * (@parts.count - 1))
  end

  @content_length
end
content_type() click to toggle source

Returns MIME type to be used for HTTP request `Content-Type` header.

@return [String]

# File lib/http/form_data/multipart.rb, line 28
def content_type
  "multipart/form-data; boundary=#{@boundary}"
end
to_s() click to toggle source

Returns content to be used for HTTP request body.

@return [String]

# File lib/http/form_data/multipart.rb, line 21
def to_s
  head + @parts.map(&:to_s).join(glue) + tail
end

Private Instance Methods

glue() click to toggle source

@return [String]

# File lib/http/form_data/multipart.rb, line 54
def glue
  @glue ||= "#{CRLF}--#{@boundary}#{CRLF}"
end
head() click to toggle source

@return [String]

# File lib/http/form_data/multipart.rb, line 49
def head
  @head ||= "--#{@boundary}#{CRLF}"
end
tail() click to toggle source

@return [String]

# File lib/http/form_data/multipart.rb, line 59
def tail
  @tail ||= "#{CRLF}--#{@boundary}--"
end