Class: PhisherPhinder::MailParser::ReceivedHeaders::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/phisher_phinder/mail_parser/received_headers/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(by_parser:, for_parser:, from_parser:, timestamp_parser:, classifier:) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
# File 'lib/phisher_phinder/mail_parser/received_headers/parser.rb', line 7

def initialize(by_parser:, for_parser:, from_parser:, timestamp_parser:, classifier:)
  @parsers = {
    by: by_parser,
    for: for_parser,
    from: from_parser,
    time: timestamp_parser,
  }
  @classifier = classifier
end

Instance Method Details

#parse(header) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/phisher_phinder/mail_parser/received_headers/parser.rb', line 17

def parse(header)
  require 'strscan'

  header_value, timestamp = header.split(';')

  parsers = [@by_parser, @for_parser, @from_parser, @starttls_parser, @timestamp_parser]
  components = extract_value_components(header_value).merge(time: timestamp)

  output = @parsers.inject({}) do |memo, (component_name, parser)|
    memo.merge(parser.parse(components[component_name])) do |key, old_value, new_value|
      if key == :starttls && old_value
        old_value
      else
        new_value
      end
    end
  end

  output.merge(@classifier.classify(output))
end