Module: CertBot::CacheCleaner

Defined in:
lib/cert_bot/io/cache_cleaner.rb

Overview

This module keeps the list with already processed advisories clean by checking the content of the file with this information and deleting entries that are older than 14 days. Normally the entries go back up to three days, so with regard to weekends and holidays a week should suffice that entries older than that are already removed from the rss feed.

Class Method Summary collapse

Class Method Details

.delete_old_entries(filepath, duration = 21) ⇒ Object

method to read a given file and clean up entries older than seven days

Parameters:

  • filepath (Filepath)

    the path to the file with the csv formatted advisory information

  • duration (Integer) (defaults to: 21)

    the duration the cache saves the entries



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cert_bot/io/cache_cleaner.rb', line 15

def self.delete_old_entries(filepath, duration = 21)
  cache_data = read_data(filepath)
  entries = Array.new()
  last_week = Time.now() - (3600 * 24 * duration) # substract the duration days
  cache_data.each { |entry|
    if (!entry.nil? && entry.length == 2)
      entries << entry if (last_week < Time.parse(entry[1]))
    end
  }
  write_data(entries, filepath)
  nil
end

.read_data(filepath) ⇒ Array

method to read and return the data of the given file

Parameters:

  • filepath (Filepath)

    the path to the file with the csv formatted advisory information

Returns:

  • (Array)

    the array with the advisory data



31
32
33
34
35
# File 'lib/cert_bot/io/cache_cleaner.rb', line 31

def self.read_data(filepath)
  csv_accessor = CertBot::CsvAccessor.new(filepath, ";")
  csv_accessor.read_csv if filepath.file?
  csv_accessor.data
end

.write_data(entries, filepath) ⇒ Object

method to write the checked data back into the file, csv formatted

Parameters:

  • entries (Array)

    the entries younger than 14 days, that should be stored

  • filepath (Filepath)

    the path to the file for the output



40
41
42
43
44
45
46
47
# File 'lib/cert_bot/io/cache_cleaner.rb', line 40

def self.write_data(entries, filepath)
  CSV.open(filepath, "w", col_sep: ";") do |csv|
    entries.each { |entry| 
      csv << entry
    }
    nil
  end
end