Skip to content

Instantly share code, notes, and snippets.

@graza
Created December 3, 2010 19:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graza/727424 to your computer and use it in GitHub Desktop.
Save graza/727424 to your computer and use it in GitHub Desktop.
Monkey patch to get Savon to handle multipart responses
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