Sha256: 61d2b4747f009a551aa32a6de7845b8163611aa31836c30774ac620ce8ab0ab0

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module SanitizeEmail
  module EmailMatchers
    class UnexpectedMailType < StandardError; end

    def string_matching(matcher, part, mail_or_part)
      if mail_or_part.respond_to?(:=~) # Can we match a regex against it?
        mail_or_part =~ Regexp.new(Regexp.escape(matcher))
      else
        raise UnexpectedMailType, "Cannot match #{matcher} for #{part}"
      end
    end

    # Normalize arrays to strings
    def array_matching(matcher, part, mail_or_part)
      mail_or_part = mail_or_part.join(', ') if mail_or_part.respond_to?(:join)
      string_matching(matcher, part, mail_or_part)
    end

    def email_matching(matcher, part, mail_or_part)
      array_matching(matcher, part, mail_or_part.send(part))
    end

    if defined?(Rspec)
      [:from, :to, :cc, :bcc, :subject, :reply_to].each do |attribute|
        RSpec::Matchers.define "have_#{attribute}" do |matcher|
          match do |actual|
            email_matching(matcher, attribute, actual)
          end
        end
      end

      [:from, :to, :cc, :bcc, :subject, :reply_to].each do |attribute|
        RSpec::Matchers.define "be_#{attribute}" do |matcher|
          match do |actual|
            string_matching(matcher, attribute, actual)
          end
        end
      end

      RSpec::Matchers.define "have_to_username" do |matcher|
        def get_username(email_message)
          email_message.header.fields[3].value
        end
        match do |actual|
          string_matching(matcher, :to_username, get_username(actual))
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sanitize_email-1.0.2 lib/sanitize_email/email_matchers.rb