Class: DoneDone::Multipart::FileParam

Inherits:
Object
  • Object
show all
Defined in:
lib/donedone/multipart.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k, filename, content) ⇒ FileParam

Returns a new instance of FileParam.



62
63
64
65
66
# File 'lib/donedone/multipart.rb', line 62

def initialize(k, filename, content)
  @k = k
  @filename = filename
  @content = content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



60
61
62
# File 'lib/donedone/multipart.rb', line 60

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



60
61
62
# File 'lib/donedone/multipart.rb', line 60

def filename
  @filename
end

#kObject

Returns the value of attribute k.



60
61
62
# File 'lib/donedone/multipart.rb', line 60

def k
  @k
end

Instance Method Details

#to_multipartObject



68
69
70
71
72
73
74
75
# File 'lib/donedone/multipart.rb', line 68

def to_multipart
  # If we can tell the possible mime-type from the filename, use the
  # first in the list; otherwise, use "application/octet-stream"
  mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
  return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
    "Content-Transfer-Encoding: binary\r\n" +
    "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
end