Class: EmailReplyParser::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/email_reply_parser/email_reply_parser.rb

Overview

An Email instance represents a parsed body String.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmail

Returns a new instance of Email.



64
65
66
# File 'lib/email_reply_parser/email_reply_parser.rb', line 64

def initialize
  @fragments = []
end

Instance Attribute Details

#fragmentsObject (readonly)

Emails have an Array of Fragments.



62
63
64
# File 'lib/email_reply_parser/email_reply_parser.rb', line 62

def fragments
  @fragments
end

Instance Method Details

#new_contentObject



75
76
77
# File 'lib/email_reply_parser/email_reply_parser.rb', line 75

def new_content
  fragments.select{|f| !f.quoted? && !f.hidden? && !f.signature?}.map{|f| f.to_s}.join("\n").rstrip
end

#read(text, from_address = "") ⇒ Object

Splits the given text into a list of Fragments. This is roughly done by reversing the text and parsing from the bottom to the top. This way we can check for ‘On <date>, <author> wrote:’ lines above quoted blocks.

text - A String email body. from_address - from address of the email (optional)

Returns this same Email instance.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/email_reply_parser/email_reply_parser.rb', line 87

def read(text, from_address = "")
  # parse out the from name if one exists and save for use later
  @from_name_raw = parse_raw_name_from_address(from_address)
  @from_name_normalized = normalize_name(@from_name_raw)
  @from_email = parse_email_from_address(from_address)

  text = normalize_text(text)

  # The text is reversed initially due to the way we check for hidden
  # fragments.
  text = text.reverse

  # This determines if any 'visible' Fragment has been found.  Once any
  # visible Fragment is found, stop looking for hidden ones.
  @found_visible = false

  # This instance variable points to the current Fragment.  If the matched
  # line fits, it should be added to this Fragment.  Otherwise, finish it
  # and start a new Fragment.
  @fragment = nil

  # Use the StringScanner to pull out each line of the email content.
  @scanner = StringScanner.new(text)
  while line = @scanner.scan_until(/\n/n)
    scan_line(line)
  end

  # Be sure to parse the last line of the email.
  if (last_line = @scanner.rest.to_s).size > 0
    scan_line(last_line, true)
  end

  # Finish up the final fragment.  Finishing a fragment will detect any
  # attributes (hidden, signature, reply), and join each line into a
  # string.
  finish_fragment

  @scanner = @fragment = nil

  # Now that parsing is done, reverse the order.
  @fragments.reverse!
  self
end

#visible_textObject

Public: Gets the combined text of the visible fragments of the email body.

Returns a String.



71
72
73
# File 'lib/email_reply_parser/email_reply_parser.rb', line 71

def visible_text
  fragments.select{|f| !f.hidden?}.map{|f| f.to_s}.join("\n").rstrip
end