Class: MList::Email

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Util::EmailHelpers, Util::TMailReaders
Defined in:
lib/mlist/email.rb

Constant Summary

Constants included from Util::EmailHelpers

Util::EmailHelpers::BRACKETS_RE, Util::EmailHelpers::HTML_ESCAPE, Util::EmailHelpers::REGARD_RE

Instance Method Summary collapse

Methods included from Util::TMailReaders

#from_address, #html, #identifier, #mailer, #text, #text_plain_part

Methods included from Util::EmailHelpers

#bracket, #escape_once, #header_sanitizer, #html_to_text, #normalize_new_lines, #remove_brackets, #remove_regard, #sanitize_header, #subscriber_name_and_address, #text_to_html, #text_to_quoted

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Provide reader delegation to most of the underlying TMail::Mail methods, excluding those overridden by this Class and the [] method (an ActiveRecord method).



51
52
53
54
55
56
57
# File 'lib/mlist/email.rb', line 51

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

Instance Method Details

#been_here?(list) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mlist/email.rb', line 9

def been_here?(list)
  tmail.header_string('x-beenthere') == list.address
end

#bounce?Boolean

Answers true if this email is a bounce.

TODO Delegate to the email_server’s bounce detector.

Returns:

  • (Boolean)


35
36
37
# File 'lib/mlist/email.rb', line 35

def bounce?
  tmail.header_string('to') =~ /mlist-/
end

#dateObject



13
14
15
16
17
18
19
# File 'lib/mlist/email.rb', line 13

def date
  if date_from_email = super
    return date_from_email
  else
    self.created_at ||= Time.now
  end
end

#fromObject



21
22
23
# File 'lib/mlist/email.rb', line 21

def from
  tmail.header_string('from')
end

#list_addressesObject

Answers the usable destination addresses of the email.



27
28
29
# File 'lib/mlist/email.rb', line 27

def list_addresses
  bounce? ? tmail.header_string('to').match(/\Amlist-(.*)\Z/)[1] : recipient_addresses
end

#recipient_addressesObject

Answers the set of addresses found in the TO and CC fields of the email.



61
62
63
# File 'lib/mlist/email.rb', line 61

def recipient_addresses
  (Array(tmail.to) + Array(tmail.cc)).collect(&:downcase).uniq
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mlist/email.rb', line 65

def respond_to?(method)
  super || (method.to_s !~ /=\Z/ && tmail.respond_to?(method))
end

#tmailObject



44
45
46
# File 'lib/mlist/email.rb', line 44

def tmail
  @tmail ||= TMail::Mail.parse(source)
end

#tmail=(tmail) ⇒ Object



39
40
41
42
# File 'lib/mlist/email.rb', line 39

def tmail=(tmail)
  @tmail = tmail
  write_attribute(:source, tmail.port.read_all)
end