Skip to content

Commit c0f15e8

Browse files
committed
prefer the html_part in a multipart email and throw away the host:port
part of the extracted urls.
1 parent a7c6c52 commit c0f15e8

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/pickle/email.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ def email_has_fields?(email, fields)
2020
end
2121

2222
def visit_in_email(email, link_text)
23-
visit(parse_email_for_link(email, link_text))
23+
link = parse_email_for_link(email, link_text)
24+
visit URI.parse(link).path
2425
end
2526

2627
def click_first_link_in_email(email)
2728
link = links_in_email(email).first
28-
visit link
29+
visit URI.parse(link).path
2930
end
3031

3132
protected
@@ -64,13 +65,23 @@ def parse_email_for_explicit_link(email, regex)
6465

6566
# e.g. Click here in <a href="http://confirm">Click here</a>
6667
def parse_email_for_anchor_text_link(email, link_text)
67-
if match_data = email.body.match(%r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>})
68+
if email.multipart?
69+
body = email.html_part.body
70+
else
71+
body = email.body
72+
end
73+
if match_data = body.match(%r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>})
6874
match_data[1]
6975
end
7076
end
7177

7278
def links_in_email(email, protos=['http', 'https'])
73-
URI.extract(email.body.to_s, protos)
79+
if email.multipart?
80+
body = email.html_part.body
81+
else
82+
body = email.body
83+
end
84+
URI.extract(body.to_s, protos)
7485
end
7586

7687
end

0 commit comments

Comments
 (0)