Sha256: f95a8da677295f96808ffb4cdeaa2855879aafd26e1cc855554c7c9bc3332fe2

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

module MList
  
  class Email < ActiveRecord::Base
    set_table_name 'mlist_emails'
    
    include MList::Util::EmailHelpers
    include MList::Util::TMailReaders
    
    def been_here?(list)
      tmail.header_string('x-beenthere') == list.address
    end
    
    def date
      if date_from_email = super
        return date_from_email
      else
        self.created_at ||= Time.now
      end
    end
    
    def from
      tmail.header_string('from')
    end
    
    # Answers the usable destination addresses of the email.
    #
    def list_addresses
      bounce? ? tmail.header_string('to').match(/\Amlist-(.*)\Z/)[1] : recipient_addresses
    end
    
    # Answers true if this email is a bounce.
    #
    # TODO Delegate to the email_server's bounce detector.
    #
    def bounce?
      tmail.header_string('to') =~ /mlist-/
    end
    
    def tmail=(tmail)
      @tmail = tmail
      write_attribute(:source, tmail.port.read_all)
    end
    
    def tmail
      @tmail ||= TMail::Mail.parse(source)
    end
    
    # Provide reader delegation to *most* of the underlying TMail::Mail
    # methods, excluding those overridden by this Class and the [] method (an
    # ActiveRecord method).
    def method_missing(symbol, *args, &block) # :nodoc:
      if symbol.to_s !~ /=\Z/ && symbol != :[] && symbol != :source && tmail.respond_to?(symbol)
        tmail.__send__(symbol, *args, &block)
      else
        super
      end
    end
    
    # Answers the set of addresses found in the TO and CC fields of the email.
    #
    def recipient_addresses
      (Array(tmail.to) + Array(tmail.cc)).collect(&:downcase).uniq
    end
    
    def respond_to?(method)
      super || (method.to_s !~ /=\Z/ && tmail.respond_to?(method))
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
aiwilliams-mlist-0.1.7 lib/mlist/email.rb
aiwilliams-mlist-0.1.8 lib/mlist/email.rb
mlist-0.1.9 lib/mlist/email.rb