class BreezyPDF::Uploads::FileFormData

Compose the form data for an HTTP POST of a file

Public Class Methods

new(fields, content_type, filename, file) click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 8
def initialize(fields, content_type, filename, file)
  @fields       = fields
  @content_type = content_type
  @filename     = filename
  @file         = file
end

Public Instance Methods

boundary() click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 23
def boundary
  @boundary ||= SecureRandom.hex
end
data() click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 15
def data
  @data ||= [
    field_data,
    file_data,
    closing_data
  ].join
end

Private Instance Methods

closing_data() click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 51
def closing_data
  [
    "\r\n--#{boundary}\r\n",
    %(Content-Disposition: form-data; name="submit"\r\n\r\n),
    %(Upload) + "\r\n",
    "--#{boundary}--"
  ].join
end
field_data() click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 29
def field_data
  field_data = []

  @fields.each do |key, value|
    field_data << "--#{boundary}\r\n"
    field_data << %(Content-Disposition: form-data; name="#{key}"\r\n\r\n)
    field_data << value.chomp + "\r\n"
  end

  field_data.join
end
file_data() click to toggle source
# File lib/breezy_pdf/uploads/file_form_data.rb, line 41
def file_data
  BreezyPDF.logger.info(%([BreezyPDF] Compressing file contents for #{@filename}))
  [
    "--#{boundary}\r\n",
    %(Content-Disposition: form-data; name="file"; filename="#{@filename}"\r\n),
    "Content-Type: #{@content_type}\r\n\r\n",
    BreezyPDF::Gzip.compress(@file.read)
  ].join
end