class Stripe::MultipartEncoder

a boundary value).
placed in the ‘Content-Type` header of a subsequent request (which includes
the encoded result and `#content_type` to get the value that should be
Parameters should be added with `#encode`. When ready, use `#body` to get
This is most useful for transferring file-like objects.
2388:
Encodes parameters into a `multipart/form-data` payload as described by RFC

def self.encode(params)

type header of a subsequent request.
Returns an encoded body and the value that should be set in the content

result.
A shortcut for encoding a single set of parameters and finalizing a
def self.encode(params)
  encoder = MultipartEncoder.new
  encoder.encode(params)
  encoder.close
  [encoder.body, encoder.content_type]
end

def body

Gets the encoded body. `#close` must be called first.
def body
  raise "object must be closed before getting body" unless @closed
  @body
end

def close

Finalizes the object by writing the final boundary.
def close
  raise "object already closed" if @closed
  @body << "\r\n"
  @body << "--#{@boundary}--"
  @closed = true
  nil
end

def content_type

request's `Content-Type`.
Gets the value including boundary that should be put into a multipart
def content_type
  "#{MULTIPART_FORM_DATA}; boundary=#{@boundary}"
end

def encode(params)

`Util.flatten_params` first before handing it off to this method.
appropriately Stripe-encoded. Pass a complex structure through
that complex substructures like hashes and arrays have already been
Note that parameters are expected to be a hash, but a "flat" hash such

Encodes a set of parameters to the body.
def encode(params)
  raise "no more parameters can be written to closed object" if @closed
  params.each do |name, val|
    if val.is_a?(::File) || val.is_a?(::Tempfile)
      write_field(name, val.read, filename: ::File.basename(val.path))
    elsif val.respond_to?(:read)
      write_field(name, val.read, filename: "blob")
    else
      write_field(name, val, filename: nil)
    end
  end
  nil
end

def escape(str)

double-quoted string and replaces any linebreak characters with spaces.
Escapes double quotes so that the given value can be used in a
def escape(str)
ub('"', "%22").tr("\n", " ").tr("\r", " ")

def initialize

Initializes a new multipart encoder.
def initialize
  # Kind of weird, but required by Rubocop because the unary plus operator
  # is considered faster than `Stripe.new`.
  @body = +""
  # Chose the same number of random bytes that Go uses in its standard
  # library implementation. Easily enough entropy to ensure that it won't
  # be present in a file we're sending.
  @boundary = SecureRandom.hex(30)
  @closed = false
  @first_field = true
end

def write_field(name, data, filename:)

def write_field(name, data, filename:)
irst_field
y << "\r\n"
st_field = false
<< "--#{@boundary}\r\n"
ename
y << %(Content-Disposition: form-data) +
     %(; name="#{escape(name.to_s)}") +
     %(; filename="#{escape(filename)}"\r\n)
y << %(Content-Type: application/octet-stream\r\n)
y << %(Content-Disposition: form-data) +
     %(; name="#{escape(name.to_s)}"\r\n)
<< "\r\n"
<< data.to_s