Class: Plaid::Internal::Multipart::FormData Private

Inherits:
Object
  • Object
show all
Defined in:
lib/plaid/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

#initialize ⇒ FormData

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/plaid/internal/multipart/multipart_form_data.rb', line 14

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

Instance Attribute Details

#parts ⇒ Array<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/plaid/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/plaid/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 (String, #read) —

    A path to a file on disk, or a file/readable object

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

    Optional filename (defaults to basename of the path for paths and File objects, and to the field name for readable objects without a path or name, such as StringIO)

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

    Optional content type (e.g. "image/png")

Returns:

  • (self) —

    Returns self for chaining



38
39
40
41
42
43
44
45
46
47
# File 'lib/plaid/internal/multipart/multipart_form_data.rb', line 38

def add_file(name:, file:, filename: nil, content_type: nil)
  headers = content_type ? { "Content-Type" => content_type } : nil
  if file.is_a?(::String)
    filename ||= ::File.basename(file)
    file = ::File.binread(file)
  else
    filename ||= filename_for(file) || name
  end
  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



53
54
55
56
# File 'lib/plaid/internal/multipart/multipart_form_data.rb', line 53

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

#content_type ⇒ String

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.



61
62
63
# File 'lib/plaid/internal/multipart/multipart_form_data.rb', line 61

def content_type
  @encoder.content_type
end

#encode ⇒ String

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.



68
69
70
# File 'lib/plaid/internal/multipart/multipart_form_data.rb', line 68

def encode
  @encoder.encode(self)
end