Created
December 3, 2010 19:32
-
-
Save graza/727424 to your computer and use it in GitHub Desktop.
Monkey patch to get Savon to handle multipart responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Savon | |
class Response | |
MIME_HEADER_PART = /^\s*(\w+)\s*=\s*(["']?)(.+)\2$/ | |
def body | |
return @body if @body | |
@body = gzipped_body? ? decoded_body : @http.body | |
if @http['content-type'] =~ /^multipart\//i | |
# Parse the header to get the boundary | |
params = {} | |
@http['content-type'].split(/;/).each do |part| | |
if match = MIME_HEADER_PART.match(part) | |
params[match[1].downcase] = match[3] | |
end | |
end | |
# After much hacking, the following seems to work | |
# But it's only been tested where there's one attachment | |
parser = RMail::Parser::MultipartReader.new(StringIO.new(@body), params['boundary']) | |
parser.read | |
parser.next_part | |
#msg = parser.read | |
msg = RMail::Parser.read(parser.read) | |
if msg.header.match?(/content-id/i, params['start']) | |
# Bugs in RMail prevent msg.body from being used | |
# Manual split instead. | |
@body = msg.to_s.split(/\r\n\r\n/)[1] | |
end | |
end | |
return @body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment