Class: Candid::Internal::Multipart::FormData Private

Inherits:
Object
  • Object
show all
Defined in:
lib/candid/internal/multipart/multipart_form_data.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormData

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of FormData.



14
15
16
17
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 14

def initialize
  @encoder = Encoder.new
  @parts = []
end

Instance Attribute Details

#partsArray<FormDataPart> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The parts in this multipart form data.

Returns:

  • (Array<FormDataPart>)

    The parts in this multipart form data.



9
10
11
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 9

def parts
  @parts
end

Instance Method Details

#add(name:, value:, content_type: nil) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a new part to the multipart form data.

Parameters:

  • name (String)

    The name of the form field

  • value (String, Integer, Float, Boolean, #read)

    The value of the field

  • content_type (String, nil) (defaults to: nil)

    Optional content type

Returns:

  • (self)

    Returns self for chaining



25
26
27
28
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 25

def add(name:, value:, content_type: nil)
  headers = content_type ? { "Content-Type" => content_type } : nil
  add_part(FormDataPart.new(name:, value:, headers:))
end

#add_file(name:, file:, filename: nil, content_type: nil) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a file to the multipart form data.

Parameters:

  • name (String)

    The name of the form field

  • file (#read)

    The file or readable object

  • filename (String, nil) (defaults to: nil)

    Optional filename (defaults to basename of path for File objects)

  • content_type (String, nil) (defaults to: nil)

    Optional content type (e.g. “image/png”)

Returns:

  • (self)

    Returns self for chaining



37
38
39
40
41
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 37

def add_file(name:, file:, filename: nil, content_type: nil)
  headers = content_type ? { "Content-Type" => content_type } : nil
  filename ||= filename_for(file)
  add_part(FormDataPart.new(name:, value: file, filename:, headers:))
end

#add_part(part) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a pre-created part to the multipart form data.

Parameters:

Returns:

  • (self)

    Returns self for chaining



47
48
49
50
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 47

def add_part(part)
  @parts << part
  self
end

#content_typeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the content type string including the boundary.

Returns:

  • (String)

    The content type with boundary.



55
56
57
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 55

def content_type
  @encoder.content_type
end

#encodeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encode the multipart form data into a multipart/form-data payload.

Returns:

  • (String)

    The encoded body.



62
63
64
# File 'lib/candid/internal/multipart/multipart_form_data.rb', line 62

def encode
  @encoder.encode(self)
end