Class: FormData::Multipart

Inherits:
Object
  • Object
show all
Defined in:
lib/form_data/multipart.rb,
lib/form_data/multipart/param.rb

Overview

multipart/form-data form data.

Defined Under Namespace

Classes: Param

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Multipart


13
14
15
16
17
# File 'lib/form_data/multipart.rb', line 13

def initialize(data)
  @parts          = Param.coerce FormData.ensure_hash data
  @boundary       = ("-" * 21) << SecureRandom.hex(21)
  @content_length = nil
end

Instance Method Details

#content_lengthFixnum

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


37
38
39
40
41
42
43
44
45
# File 'lib/form_data/multipart.rb', line 37

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_typeString

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


29
30
31
# File 'lib/form_data/multipart.rb', line 29

def content_type
  "multipart/form-data; boundary=#{@boundary}"
end

#to_sString

Returns content to be used for HTTP request body.


22
23
24
# File 'lib/form_data/multipart.rb', line 22

def to_s
  head + @parts.map(&:to_s).join(glue) + tail
end