Class: EmSpamc::HeaderParser

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

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/em_spamc/header_parser.rb', line 2

def self.parse(data)
  lines = data.split(/\r?\n/)
  headers = { }

  lines.each do |line|
    case (line)
    when /^SPAMD\/(\d+\.\d+) (.*)/
      code, message = $2.split(/\s+/)

      headers[:version] = $1
      headers[:code] = code.match(/\d/) ? code.to_i : code
      headers[:message] = message        
    when /^(\S+): (.*)\s;\s([0-9]*.[0-9]*)/
      headers[:spam] = $2 == 'True'
      headers[:score] = $3.to_f
    end
  end

  # Special case: if we only have one header line back, means score of 0
  if (lines.length == 1)
    headers.merge!(:score => 0.0, :spam => false)
  end

  headers
end