End of Line Problems
I'm trying to download images from my Gmail acco开发者_JS百科unt using the gmail
gem. It works fine, except the file downloaded through the gem has CR + LF
line endings and the actual file has LF
line endings.
Why is this happening? How can I fix it?
Are you on Mac? I suspect GMail gives you LF line endings seeing basing in your browser's User-Agent.
In any case, proper solution to the problem is to convert text using universal_newline
converter. See documentation for ruby built-in converters here: http://ruby-doc.org/core-1.9/classes/Encoding/Converter.html
If it's a text file then the line endings are specified by MIME and probably the encoding takes place by the sender of the file ( http://en.wikipedia.org/wiki/MIME#Content-Transfer-Encoding ).
As to how to fix it, Alex Lebedev had a good option. You could also use Ruby MIME gems to do the conversion.
If you are downloading images, however, the Base64 encoding should be resilient to end-of-line issues.
Hope that helps. The problem is a little vague to me as we're talking line endings and image files (which don't have CRLF endings as a normal part of their encoding). :)
The simplest fix may be to take the result and simply:
text.gsub! "\r\n", "\n"
精彩评论