Class: MultipartFormData
- Inherits:
-
Object
- Object
- MultipartFormData
- Defined in:
- lib/multipart_form_data.rb
Overview
Synopsis
MultipartFormData facilitates in building multipart/form-data encoded messages that can be POSTed using ThinHTTP. This class is a simple wrapper around the MIME::MultipartMedia::FormData class.
Caveats
According to RFC 1867 and 2388, each field of the form is to be sent in the order in which it occurs in the form. However, the Hash containing the form fields inherently does not maintain order. This is probably not a problem.
Examples
Simple usage of the class
The following params
Hash:
params = {
'text_field' => 'this is some text',
'image_field' => Pathname.new('/tmp/pic.jpg')
}
simulates the following HTML form:
<form>
<input type="text" name="text_field"/>
<input type="file" name="image_field"/>
</form>
Creating and using a MultipartFormData instance initialized with params
:
fd = MultipartFormData.new(params)
fd.content_type # outputs the content type header including boundary
fd.content # outputs the content (multipart/form-data encoded entities)
Constructing a multipart/form-data message and POSTing it via ThinHTTP
form_data = MultipartFormData.new(
:dog_owner => 'Mary Jane',
:pic_comment => 'These are my two black lab/pit mix puppies.',
:dog_pic1 => Pathname.new('/tmp/lexi.jpg'),
:dog_pic2 => Pathname.new('/tmp/simone.jpg')
)
th = ThinHTTP.new('petfotoz.com', 80)
th.post('/photo_album.cgi', form_data, form_data.content_type)
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
Instance Method Summary collapse
-
#initialize(params) ⇒ MultipartFormData
constructor
Returns new MultipartFormData instance initialized with
params
. -
#to_s ⇒ Object
(also: #content)
Return the multipart/form-data content.
Constructor Details
#initialize(params) ⇒ MultipartFormData
Returns new MultipartFormData instance initialized with params
. params
is a Hash of key/value pairs representing HTML input variable names and values. For HTML file type inputs, use a Pathname object.
63 64 65 66 67 |
# File 'lib/multipart_form_data.rb', line 63 def initialize params @form_data = MIME::MultipartMedia::FormData.new @content_type = @form_data.content_type initialize_form_data(params) end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
56 57 58 |
# File 'lib/multipart_form_data.rb', line 56 def content_type @content_type end |
Instance Method Details
#to_s ⇒ Object Also known as: content
Return the multipart/form-data content.
72 73 74 |
# File 'lib/multipart_form_data.rb', line 72 def to_s @form_data.body end |