A tiny gem to create multiparts.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
Gemfile
Gemfile.lock
LICENSE
README.md
Rakefile
multiparty.gemspec

README.md

multiparty

Easily generate a multipart/form-data header and body.

Usage

You can add multiple values, corresponding to multiple statements:

multiparty = Multiparty.new
multiparty[:name] = "David Verhasselt"
multiparty[:state] = "awesome"
# or in one statement:
multiparty << {:name => "David Verhasselt", :state => "awesome"}

multiparty[:avatar] = {:filename => "avatar.jpg", :content => "...jpegdata..."}

# Retrieve the header and body like this:
multiparty.header
# Content-Type: multipart/form-data; boundary=multiparty-boundary-1342
multiparty.body
# --multiparty-boundary-1342
# Content-Disposition: form-data; name="name"
#
# David Verhasselt
# --multiparty-boundary-1342
# Content-Disposition: form-data; name="state"
# 
# awesome
# --multiparty-boundary-1342
# Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
# Content-Type: application/octet-stream
# Content-Transfer-Encoding: binary
#
# ...jpegdata...
# --multiparty-boundary-1342--

You can also add files:

multiparty[:your_avatar] = File.open("foo.txt")

You can specify an optional content-type. If you don't, Multiparty will try and detect the correct MIME-type based on the filename.

multiparty[:your_avatar] = {:filename => "foo.jpg", :content_type => "text/plain", :content => File.read("foo.txt")}
# -> Content-Type: text/plain
multiparty[:your_avatar] = {:filename => "foo.jpg", :content => "not really jpeg")}
# -> Content-Type: image/jpeg
multiparty[:your_avatar] = File.open("foo.jpg")
# -> Content-Type: image/jpeg

Files and Tempfiles are interchangable in Multiparty:

tempfile = Tempfile.new("foo")
tempfile.write("Hello World!")
tempfile.rewind

multiparty[:message] = tempfile
# is the same as
multiparty[:message] = File.open(tempfile.path)

Arrays are handled in the conventional way using "array[]" as name:

multiparty[:items] = [1, 2, 3]

# --AaB03x
# Content-Disposition: form-data; name="items[]"
# 
# 1
# --AaB03x
# Content-Disposition: form-data; name="items[]"
# 
# 2
# --AaB03x
# Content-Disposition: form-data; name="items[]"
# 
# 3
# --AaB03x--

Using the parts accessor you can easily modify parts:

multiparty[:items] = [1, 2, 3]
multiparty[:items] << 4

# multiparty[:items] == [1, 2, 3, 4]

Multiparty has the to_s method aliased to body so you can use it as a String:

puts "Hello World! My multipart body: #{multiparty}"

If the API you're interface with only supports :key => :value headers, use header_value:

headers["Content-Type"] = multiparty.header_value

Installation

$ gem install multiparty

Testing

$ bundle install
$ rake spec

Todo

  • Nested multiparts ("multipart/mixed") not yet supported

Author

David Verhasselt - david@crowdway.com