Class: PhisherPhinder::MailParser::Body::BlockClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/phisher_phinder/mail_parser/body/block_classifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(line_end) ⇒ BlockClassifier

Returns a new instance of BlockClassifier.



7
8
9
# File 'lib/phisher_phinder/mail_parser/body/block_classifier.rb', line 7

def initialize(line_end)
  @line_end = line_end
end

Instance Method Details

#classify_block(contents) ⇒ Object



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
37
# File 'lib/phisher_phinder/mail_parser/body/block_classifier.rb', line 11

def classify_block(contents)
  lines = contents.split(@line_end)
  processing_block_headers = true

  output = {
    content_type: :text,
    character_set: :utf_8,
    content_transfer_encoding: nil
  }

  while processing_block_headers && lines.any? do
    line = lines.shift&.strip
    if line && line.empty?
      processing_block_headers = false
    elsif line && line =~ /\AContent-Type:/
      output.merge!(extract_content_type(line))

      output.merge!(extract_character_set(line))
    elsif line && line =~ /\AContent-Transfer-Encoding/
      output.merge!(extract_encoding(line))
    end
  end

  output[:content] = lines.join(@line_end)

  output
end

#classify_headers(headers) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/phisher_phinder/mail_parser/body/block_classifier.rb', line 39

def classify_headers(headers)
  output = {
    content_type: :text,
    character_set: :utf_8,
    content_transfer_encoding: nil
  }

  output.merge!(extract_content_type(headers[:content_type]))

  output.merge!(extract_character_set(headers[:content_type]))

  output.merge!(extract_encoding(headers[:content_transfer_encoding]))

  output
end