module EmailSpec::Helpers
Constants
- A_TAG_BEGIN_REGEX
- A_TAG_END_REGEX
Attributes
last_email_address[R]
Public Instance Methods
click_email_link_matching(regex, email = current_email)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
Should be able to accept String or Regexp options.
# 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)
click to toggle source
# File lib/email_spec/helpers.rb, line 87 def links_in_email(email) links = URI::Parser.new.extract(email.default_part_body.to_s, ['http', 'https']) links.map{|url| HTMLEntities.new.decode(url) }.uniq end
open_email(address, opts={})
click to toggle source
# File lib/email_spec/helpers.rb, line 32 def open_email(address, opts={}) set_current_email(find_email!(address, opts)) end
Also aliased as: open_email_for
open_last_email()
click to toggle source
# File lib/email_spec/helpers.rb, line 38 def open_last_email set_current_email(last_email_sent) end
open_last_email_for(address)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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 = '')
click to toggle source
# 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
Private Instance Methods
convert_address(address)
click to toggle source
# File lib/email_spec/helpers.rb, line 173 def convert_address(address) @last_email_address = (address || current_email_address) AddressConverter.instance.convert(@last_email_address) end
current_email_address()
click to toggle source
Overwrite this method to set default email address, for example: last_email_address
|| @current_user.email
# File lib/email_spec/helpers.rb, line 180 def current_email_address last_email_address end
email_spec_deprecate(text)
click to toggle source
# File lib/email_spec/helpers.rb, line 189 def email_spec_deprecate(text) puts "" puts "DEPRECATION: #{text.split.join(' ')}" puts "" end
email_spec_hash()
click to toggle source
# File lib/email_spec/helpers.rb, line 94 def email_spec_hash @email_spec_hash ||= {:read_emails => {}, :unread_emails => {}, :current_emails => {}, :current_email => nil} end
find_email!(address, opts={})
click to toggle source
# File lib/email_spec/helpers.rb, line 98 def find_email!(address, opts={}) email = find_email(address, opts) if current_email_address.nil? raise EmailSpec::NoEmailAddressProvided, "No email address has been provided. Make sure current_email_address is returning something." elsif email.nil? error = "#{opts.keys.first.to_s.gsub("_", " ").downcase unless opts.empty?} #{('"' + opts.values.first.to_s + '"') unless opts.empty?}" raise EmailSpec::CouldNotFindEmailError, "Could not find email #{error} in the mailbox for #{current_email_address}. \n Found the following emails:\n\n #{all_emails.to_s}" end email end
mailbox_for(address)
click to toggle source
Calls superclass method
# File lib/email_spec/helpers.rb, line 185 def mailbox_for(address) super(convert_address(address)) # super resides in Deliveries end
parse_email_count(amount)
click to toggle source
# File lib/email_spec/helpers.rb, line 160 def parse_email_count(amount) case amount when "no" 0 when "an" 1 else amount.to_i end end
parse_email_for_anchor_text_link(email, link_text)
click to toggle source
e.g. Click here in <a href=“Click”>confirm“>Click here</a>
# File lib/email_spec/helpers.rb, line 147 def parse_email_for_anchor_text_link(email, link_text) if textify_images(email.default_part_body) =~ %r{#{A_TAG_BEGIN_REGEX}#{link_text}#{A_TAG_END_REGEX}} URI.split($1)[5..-1].compact!.join("?").gsub("&", "&") # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002) else return nil end end
parse_email_for_explicit_link(email, regex)
click to toggle source
e.g. confirm in confirm
# File lib/email_spec/helpers.rb, line 140 def parse_email_for_explicit_link(email, regex) regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp) url = links_in_email(email).detect { |link| link =~ regex } request_uri(url) end
parse_email_for_link(email, text_or_regex)
click to toggle source
# File lib/email_spec/helpers.rb, line 118 def parse_email_for_link(email, text_or_regex) matcher = EmailSpec::Matchers::HaveBodyText.new(text_or_regex) if defined?(RSpec) RSpec::Expectations::PositiveExpectationHandler.handle_matcher(email, matcher) else assert_must matcher, email end url = parse_email_for_explicit_link(email, text_or_regex) url ||= parse_email_for_anchor_text_link(email, text_or_regex) raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url url end
request_uri(link)
click to toggle source
# File lib/email_spec/helpers.rb, line 133 def request_uri(link) return unless link url = URI::parse(link) url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri end
set_current_email(email)
click to toggle source
# File lib/email_spec/helpers.rb, line 109 def set_current_email(email) return unless email [email.to, email.cc, email.bcc].compact.flatten.each do |to| read_emails_for(to) << email email_spec_hash[:current_emails][to] = email end email_spec_hash[:current_email] = email end
textify_images(email_body)
click to toggle source
# File lib/email_spec/helpers.rb, line 156 def textify_images(email_body) email_body.to_s.gsub(%r{<img[^>]*alt=['"]?([^'"]*)['"]?[^>]*?/>}) { $1 } end