Created
December 10, 2017 01:51
-
-
Save trans/60bc08d74bba5f2afbce32819d0cdb7c to your computer and use it in GitHub Desktop.
Parse Email Crude
This file contains 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
class Email | |
attr :headers | |
attr :body | |
def initialize | |
@headers = Multimap.new | |
@body = "" | |
end | |
def parse(io) | |
parse_lines(io.readlines) | |
end | |
def parse_lines(lines) | |
index = 0 | |
while index < lines.size | |
line = lines[index].strip | |
if line == "" | |
@body = lines[index..-1].strip.join("\n") | |
break | |
else | |
name, value = line.split(":") | |
@headers[name.strip] = value.strip | |
index += 1 | |
end | |
end | |
end | |
def [](name) | |
@headers[name] | |
end | |
def self.read(io) | |
e = Email.new | |
e.parse(io) | |
e | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment