Created
February 10, 2017 13:14
-
-
Save therod/f7e3738f69b51d98a29820224d1af412 to your computer and use it in GitHub Desktop.
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
require 'minitest/autorun' | |
require 'pry' | |
require 'mail' | |
require 'nokogiri' | |
class MailParser | |
class << self | |
def parse_email(email_path) | |
mail = Mail.read(email_path) | |
html_body = mail.parts.last.body.raw_source | |
body = Nokogiri::HTML(html_body) | |
date = body.xpath('//p//span//span')[11].text.split(" ")[2] | |
time_window = body.xpath('//p//span//span')[11].text.split(" ")[3] | |
# TODO: Unescape Umlauts in Customer Name | |
customer_name = body.xpath('//p//span//span')[12].children[2].text | |
street_name = body.xpath('//p//span//span')[12].children[6].text | |
zipcode = body.xpath('//p//span//span')[12].children[9].text | |
total = body.xpath('//b//span').last.text | |
return { | |
date: date, | |
time_window: time_window, | |
customer_name: customer_name, | |
street_name: street_name, | |
zipcode: zipcode, | |
order_total: total | |
} | |
end | |
end | |
end | |
class MailParserTest < Minitest::Test | |
def test_parser | |
expected_result = { date: '23.01.17', | |
time_window: '19:00-19:30', | |
customer_name: 'Christine Mika', | |
street_name: 'Bürglistrasse 26', | |
zipcode: '8002', | |
order_total: '53,00' } | |
assert_equal expected_result, MailParser.parse_email('email.eml') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment