Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.71 KB

README.md

File metadata and controls

52 lines (35 loc) · 1.71 KB

HTTP Multipart Parser

Build Status

Use this gem to parse multipart messages into coherent Ruby-flavoured pieces.

Usage

Imagine your API vendor returns you a multipart body that looks like this:

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Type: text/xml

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body /></S:Envelope>
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Id:<[email protected]>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

"first_name","last_name","preferred_name"
"david", "looi", "dave"

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50--

But you only care about that CSV data near the bottom. How on earth are you supposed to process this?

Like this!

input = %(
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Type: text/xml

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body /></S:Envelope>
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Id:<[email protected]>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

"first_name","last_name","preferred_name"
"david", "looi", "dave"

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50--
)

response = HttpMultipartParser::Response.new(input)
puts response.part_by_content_type('application/octet-stream').body

Reference

This gem is built based on this RFC1341(MIME).