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,
sig/http.rbs

Overview

multipart/form-data form data.

Defined Under Namespace

Classes: Param

Constant Summary collapse

DEFAULT_CONTENT_TYPE =

Default MIME type for multipart form data

Returns:

  • (String)
"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

Parameters:

  • data (Object)
  • boundary: (_ToS) (defaults to: self.class.generate_boundary)
  • content_type: (_ToS) (defaults to: DEFAULT_CONTENT_TYPE)


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

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

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

Returns:

  • (String)


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

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

#glueString

Returns the boundary glue between parts

Returns:

  • (String)


83
84
85
# File 'lib/http/form_data/multipart.rb', line 83

def glue
  @glue ||= "--#{@boundary}#{CRLF}"
end

#parts(data) ⇒ Array[Param]

Coerces data into an array of Param objects

Parameters:

  • data (Object)

Returns:



99
100
101
102
103
# File 'lib/http/form_data/multipart.rb', line 99

def parts(data)
  FormData.ensure_data(data).flat_map do |name, values|
    Array(values).map { |value| Param.new(name, value) }
  end
end

#tailString

Returns the closing boundary tail

Returns:

  • (String)


91
92
93
# File 'lib/http/form_data/multipart.rb', line 91

def tail
  @tail ||= "--#{@boundary}--#{CRLF}"
end