Class: HTTP::FormData::Multipart

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

Overview

multipart/form-data form data.

Defined Under Namespace

Classes: Param

Constant Summary collapse

DEFAULT_CONTENT_TYPE =

Default MIME type for multipart form data

"multipart/form-data"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Readable

#read, #rewind, #size, #to_s

Constructor Details

#initialize(data, boundary: self.class.generate_boundary, content_type: DEFAULT_CONTENT_TYPE) ⇒ Multipart

Creates a new Multipart form data instance

Examples:

Basic form data

Multipart.new({ foo: "bar" })

With custom content type

Multipart.new(parts, content_type: "multipart/related")

Parameters:

  • data (Enumerable, Hash, #to_h)

    form data key-value pairs

  • boundary (String) (defaults to: self.class.generate_boundary)

    custom boundary string

  • content_type (String) (defaults to: DEFAULT_CONTENT_TYPE)

    MIME type for the Content-Type header



39
40
41
42
43
# File 'lib/http/form_data/multipart.rb', line 39

def initialize(data, boundary: self.class.generate_boundary, content_type: DEFAULT_CONTENT_TYPE)
  @boundary     = boundary.to_s.freeze
  @content_type = content_type
  @io = CompositeIO.new(parts(data).flat_map { |part| [glue, part] } << tail)
end

Instance Attribute Details

#boundaryString (readonly)

Returns the multipart boundary string

Examples:

multipart.boundary # => "-----abc123"

Returns:

  • (String)


25
26
27
# File 'lib/http/form_data/multipart.rb', line 25

def boundary
  @boundary
end

Class Method Details

.generate_boundaryString

Generates a boundary string for multipart form data

Examples:

Multipart.generate_boundary # => "-----abc123..."

Returns:

  • (String)


52
53
54
# File 'lib/http/form_data/multipart.rb', line 52

def self.generate_boundary
  ("-" * 21) << SecureRandom.hex(21)
end

Instance Method Details

#content_typeString

Returns MIME type for the Content-Type header

Examples:

multipart.content_type
# => "multipart/form-data; boundary=-----abc123"

Returns:

  • (String)


64
65
66
# File 'lib/http/form_data/multipart.rb', line 64

def content_type
  "#{@content_type}; boundary=#{@boundary}"
end