Class: Multipart::FileParam

Inherits:
Object
  • Object
show all
Defined in:
lib/mls/models/document.rb

Overview

Formats the contents of a file or string for inclusion with a multipart form post

Instance Method Summary collapse

Constructor Details

#initialize(k, file) ⇒ FileParam

Returns a new instance of FileParam.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mls/models/document.rb', line 125

def initialize(k, file)
  @k = k
  @file = file

  @filename = file.original_filename if file.respond_to?(:original_filename)
  @filename ||= File.basename(file.path)

  # If we can tell the possible mime-type from the filename, use the
  # first in the list; otherwise, use "application/octet-stream"
  @content_type = file.content_type if file.respond_to?(:content_type)
  @content_type ||= (MIME::Types.type_for(@filename)[0] || MIME::Types["application/octet-stream"][0]).simplified
end

Instance Method Details

#to_multipartObject



138
139
140
141
# File 'lib/mls/models/document.rb', line 138

def to_multipart
  return "Content-Disposition: form-data; name=\"#{@k}\"; filename=\"#{ @filename }\"\r\n" +
         "Content-Type: #{ @content_type }\r\n\r\n#{ @file.read }\r\n"
end