Class: EmSpamc::ReportParser

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

Constant Summary collapse

LINE_REGEXP =
/-$/
RULE_REGEXP =
/[0-9]*[.][0-9]\s\w*\s/

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/em_spamc/report_parser.rb', line 5

def self.parse(data)
  split_data = data.split(LINE_REGEXP)    

  # Only status line is back - no report returned, no rules
  if (split_data.length == 1)
    return [ ]
  end

  last_part = split_data[1].sub(/^[\n\r]./,'').chomp.chomp
  
  points_rules = last_part.gsub(RULE_REGEXP).collect do |sub|
    sub.chomp(' ')
  end

  rule_texts = last_part.split(RULE_REGEXP).collect do |text|
    text.delete("\n").squeeze.chomp(' ').sub(/^\s/, '')
  end

  rules = [ ]

  points_rules.each_with_index do |points_rule, i|
    split = points_rule.split(' ')

    rules << {
      :pts => split[0].to_f,
      :rule => split[1],
      :text => rule_texts[i + 1]
    }
  end

  rules
end