Class: PhisherPhinder::MailParser::ReceivedHeaders::TimestampParser

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

Instance Method Summary collapse

Instance Method Details

#parse(timestamp) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/phisher_phinder/mail_parser/received_headers/timestamp_parser.rb', line 7

def parse(timestamp)
  return {time: nil} unless timestamp

  require 'time'

  date_formats = [
    "%a, %d %b %Y %H:%M:%S %z (%Z)",
    "%a, %d %b %Y %H:%M:%S %z",
    "%d %b %Y %H:%M:%S %z"
  ]

  parsed_time = date_formats.inject(nil) do |parsed_time, pattern|
    begin
      parsed_time || Time.strptime(timestamp.strip, pattern)
    rescue ArgumentError
    end
  end

  raise "Could not match `#{timestamp}` with the available patterns" unless parsed_time

  {time: parsed_time}
end