Class: MailParser
- Inherits:
-
Object
- Object
- MailParser
- Defined in:
- lib/mail_parser/mail_parser.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#data ⇒ Object
Returns the value of attribute data.
-
#index ⇒ Object
Returns the value of attribute index.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#stop ⇒ Object
Returns the value of attribute stop.
Instance Method Summary collapse
-
#initialize(pattern = nil) ⇒ MailParser
constructor
A new instance of MailParser.
- #match(field, *patterns) ⇒ Object
- #parse(body, *options, &block) ⇒ Object
- #parse_until(field, pattern) ⇒ Object (also: #match_until)
- #truncate_html(html) ⇒ Object
Constructor Details
#initialize(pattern = nil) ⇒ MailParser
Returns a new instance of MailParser.
4 5 6 |
# File 'lib/mail_parser/mail_parser.rb', line 4 def initialize(pattern=nil) @pattern = pattern end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
2 3 4 |
# File 'lib/mail_parser/mail_parser.rb', line 2 def body @body end |
#data ⇒ Object
Returns the value of attribute data.
2 3 4 |
# File 'lib/mail_parser/mail_parser.rb', line 2 def data @data end |
#index ⇒ Object
Returns the value of attribute index.
2 3 4 |
# File 'lib/mail_parser/mail_parser.rb', line 2 def index @index end |
#pattern ⇒ Object
Returns the value of attribute pattern.
2 3 4 |
# File 'lib/mail_parser/mail_parser.rb', line 2 def pattern @pattern end |
#stop ⇒ Object
Returns the value of attribute stop.
2 3 4 |
# File 'lib/mail_parser/mail_parser.rb', line 2 def stop @stop end |
Instance Method Details
#match(field, *patterns) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mail_parser/mail_parser.rb', line 32 def match(field, *patterns) pattern = patterns[0] if patterns.length == 2 invoked = false target_pattern = patterns[1] else invoked = true target_pattern = pattern end for line in @body[@index..@body.length] if @stop and line =~ @stop throw :halt end if !invoked and line =~ pattern invoked = true end if invoked and line =~ target_pattern @data[field] = target_pattern.match(line)[1] @index += 1 return end @index += 1 end end |
#parse(body, *options, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mail_parser/mail_parser.rb', line 12 def parse(body, *, &block) if .include? :html body = truncate_html(body) end @body = body.split("\n") @index = 0 @data = {} if block_given? yield self end #call the injected pattern if not @pattern.nil? and not @pattern.block.nil? @pattern.block.call self end @data end |
#parse_until(field, pattern) ⇒ Object Also known as: match_until
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mail_parser/mail_parser.rb', line 58 def parse_until(field,pattern) data = @data.clone collections = [] begin @data = {} @stop = pattern catch(:halt) do yield self if block_given? end @stop = nil collections << @data if @data != {} end until @body[@index] =~ pattern or @index >= @body.length @data = data.merge({field => collections }) end |
#truncate_html(html) ⇒ Object
8 9 10 |
# File 'lib/mail_parser/mail_parser.rb', line 8 def truncate_html(html) html.gsub(/<.*>/,"") end |