Skip to content

Instantly share code, notes, and snippets.

@trans
Created December 10, 2017 01:51
Show Gist options
  • Save trans/60bc08d74bba5f2afbce32819d0cdb7c to your computer and use it in GitHub Desktop.
Save trans/60bc08d74bba5f2afbce32819d0cdb7c to your computer and use it in GitHub Desktop.
Parse Email Crude
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