Module: EmailSpec::Helpers
Constant Summary
collapse
- A_TAG_BEGIN_REGEX =
%r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*>\s*(?:(?!</a>).)*?\s*}
- A_TAG_END_REGEX =
%r{\s*(?:(?!</a>).)*?\s*</a>}
Instance Method Summary
collapse
Methods included from Deliveries
#all_emails, #last_email_sent, #reset_mailer
Instance Method Details
#click_email_link_matching(regex, email = current_email) ⇒ Object
21
22
23
24
25
|
# File 'lib/email_spec/helpers.rb', line 21
def click_email_link_matching(regex, email = current_email)
url = links_in_email(email).detect { |link| link =~ regex }
raise "No link found matching #{regex.inspect} in #{email.default_part_body}" unless url
visit request_uri(url)
end
|
#click_first_link_in_email(email = current_email) ⇒ Object
27
28
29
30
|
# File 'lib/email_spec/helpers.rb', line 27
def click_first_link_in_email(email = current_email)
link = links_in_email(email).first
visit request_uri(link)
end
|
#current_email(address = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/email_spec/helpers.rb', line 46
def current_email(address=nil)
address = convert_address(address)
email = address ? email_spec_hash[:current_emails][address] : email_spec_hash[:current_email]
exception_class = if defined?(RSpec)
RSpec::Expectations::ExpectationNotMetError
else
StandardError
end
raise exception_class, "Expected an open email but none was found. Did you forget to call open_email?" unless email
email
end
|
#current_email_attachments(address = nil) ⇒ Object
58
59
60
|
# File 'lib/email_spec/helpers.rb', line 58
def current_email_attachments(address=nil)
current_email(address).attachments || Array.new
end
|
#find_email(address, opts = {}) ⇒ Object
Should be able to accept String or Regexp options.
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/email_spec/helpers.rb', line 72
def find_email(address, opts={})
address = convert_address(address)
if opts[:with_subject]
expected_subject = (opts[:with_subject].is_a?(String) ? Regexp.escape(opts[:with_subject]) : opts[:with_subject])
mailbox_for(address).find { |m| m.subject =~ Regexp.new(expected_subject) }
elsif opts[:with_text]
expected_text = (opts[:with_text].is_a?(String) ? Regexp.escape(opts[:with_text]) : opts[:with_text])
mailbox_for(address).find { |m| m.default_part_body =~ Regexp.new(expected_text) }
elsif opts[:from]
mailbox_for(address).find { |m| m.from.include? opts[:from] }
else
mailbox_for(address).first
end
end
|
#links_in_email(email) ⇒ Object
87
88
89
90
|
# File 'lib/email_spec/helpers.rb', line 87
def links_in_email(email)
links = URI::Parser.new.(email.default_part_body.to_s, ['http', 'https'])
links.map{|url| HTMLEntities.new.decode(url) }.uniq
end
|
#open_email(address, opts = {}) ⇒ Object
Also known as:
open_email_for
32
33
34
|
# File 'lib/email_spec/helpers.rb', line 32
def open_email(address, opts={})
set_current_email(find_email!(address, opts))
end
|
#open_last_email ⇒ Object
38
39
40
|
# File 'lib/email_spec/helpers.rb', line 38
def open_last_email
set_current_email(last_email_sent)
end
|
#open_last_email_for(address) ⇒ Object
42
43
44
|
# File 'lib/email_spec/helpers.rb', line 42
def open_last_email_for(address)
set_current_email(mailbox_for(address).last)
end
|
#read_emails_for(address) ⇒ Object
67
68
69
|
# File 'lib/email_spec/helpers.rb', line 67
def read_emails_for(address)
email_spec_hash[:read_emails][convert_address(address)] ||= []
end
|
#unread_emails_for(address) ⇒ Object
62
63
64
65
|
# File 'lib/email_spec/helpers.rb', line 62
def unread_emails_for(address)
read_message_ids = read_emails_for(address).map(&:message_id)
mailbox_for(address).reject { |m| read_message_ids.include?(m.message_id) }
end
|
#visit_in_email(link_text, address = '') ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/email_spec/helpers.rb', line 12
def visit_in_email(link_text, address = '')
if address.nil? || address.empty?
email = current_email
else
email = find_email!(address)
end
visit(parse_email_for_link(email, link_text))
end
|