Class: CertBot::RssHandler
- Inherits:
-
Object
- Object
- CertBot::RssHandler
- Defined in:
- lib/cert_bot/rss_handler.rb
Overview
This class handles the fetching, reading and parsing of the BSI rss feed
Instance Attribute Summary collapse
-
#config_file ⇒ String
private
The string path to the config.json.
-
#config_path ⇒ Pathname
private
The pathname to the configuration file.
-
#debug_log ⇒ File
private
The file object to where debug output is written.
-
#rss_feed ⇒ String
private
The URI to the rss feed.
Instance Method Summary collapse
-
#contains_severity?(categories, severity_string) ⇒ Boolean
private
method to determine if severity is contained in the severity list.
-
#contains_values?(item_wid, item_timestamp, data) ⇒ Boolean
private
method to check if a advisory already has been mailed.
-
#contraints_fulfilled(item, csv_data, severities, is_updated) ⇒ Bool
private
method to determine if all constraints for sending a mail are fulfilled.
-
#init_csv_accessor(meta_path) ⇒ CertBot::CsvAccessor
private
method to initialize the csv and read the data from the meta data filepath.
-
#initialize(rss_feed, config_file) ⇒ RssHandler
constructor
initialization.
-
#process_item(item, config_file) ⇒ Object
private
method to generate the required output for a given item of the rss feed.
-
#read_feed(severities, is_updated) ⇒ Object
method to parse the feed and generate mails for the required items.
-
#write_debug_log(log_text) ⇒ Object
private
method to write a given debug text into the logfile if the debug parameter is set.
Constructor Details
#initialize(rss_feed, config_file) ⇒ RssHandler
initialization
19 20 21 22 23 24 25 26 27 |
# File 'lib/cert_bot/rss_handler.rb', line 19 def initialize(rss_feed, config_file) @config_path = Pathname.new(config_file).dirname. @rss_feed = rss_feed @config_file = config_file if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug]) @debug_log = File.open(@config_path.join("debug.log"), mode="a") end end |
Instance Attribute Details
#config_file ⇒ String (private)
Returns the string path to the config.json.
69 70 71 |
# File 'lib/cert_bot/rss_handler.rb', line 69 def config_file @config_file end |
#config_path ⇒ Pathname (private)
Returns the pathname to the configuration file.
63 64 65 |
# File 'lib/cert_bot/rss_handler.rb', line 63 def config_path @config_path end |
#debug_log ⇒ File (private)
Returns the file object to where debug output is written.
65 66 67 |
# File 'lib/cert_bot/rss_handler.rb', line 65 def debug_log @debug_log end |
#rss_feed ⇒ String (private)
Returns the URI to the rss feed.
67 68 69 |
# File 'lib/cert_bot/rss_handler.rb', line 67 def rss_feed @rss_feed end |
Instance Method Details
#contains_severity?(categories, severity_string) ⇒ Boolean (private)
method to determine if severity is contained in the severity list
95 96 97 |
# File 'lib/cert_bot/rss_handler.rb', line 95 def contains_severity?(categories, severity_string) categories.include?(CertBot::Data::Severity.get_mapping_for(severity_string)) end |
#contains_values?(item_wid, item_timestamp, data) ⇒ Boolean (private)
method to check if a advisory already has been mailed
75 76 77 78 79 80 |
# File 'lib/cert_bot/rss_handler.rb', line 75 def contains_values?(item_wid, , data) data.each { |line| return true if (line[0].eql?(item_wid) && Time.parse(line[1]).eql?()) } false end |
#contraints_fulfilled(item, csv_data, severities, is_updated) ⇒ Bool (private)
method to determine if all constraints for sending a mail are fulfilled
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cert_bot/rss_handler.rb', line 105 def contraints_fulfilled(item, csv_data, severities, is_updated) item_wid = item.link.split("=")[1] return false if (contains_values?(item_wid, item.pubDate.localtime, csv_data)) return false if (!contains_severity?(severities, item.category.content)) if (is_updated == nil) update_status = CertBot::AdvisoryParser.retrieve_update_status(item_wid) return false if (CertBot::Data::UpdateStatus.get_mapping_for(update_status) != :new) end true end |
#init_csv_accessor(meta_path) ⇒ CertBot::CsvAccessor (private)
method to initialize the csv and read the data from the meta data filepath
85 86 87 88 89 |
# File 'lib/cert_bot/rss_handler.rb', line 85 def init_csv_accessor() csv_accessor = CertBot::CsvAccessor.new(, ";") csv_accessor.read_csv if .file? csv_accessor end |
#process_item(item, config_file) ⇒ Object (private)
method to generate the required output for a given item of the rss feed
119 120 121 122 123 124 125 126 127 |
# File 'lib/cert_bot/rss_handler.rb', line 119 def process_item(item, config_file) if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:json] != nil) output_dir = Pathname.new(CertBot.parameter_handler.repository.parameters[:json]) CertBot::JsonGenerator.generate_json(item, output_dir.) else CertBot::MailAgent.send_mail(item, config_file) end nil end |
#read_feed(severities, is_updated) ⇒ Object
method to parse the feed and generate mails for the required items
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cert_bot/rss_handler.rb', line 32 def read_feed(severities, is_updated) write_debug_log("Starting rss parsing at #{Time.now}.") = Pathname.new(@config_path).join("meta_info"). csv_accessor = init_csv_accessor() URI(@rss_feed).open do |rss| feed = RSS::Parser.parse(rss) feed.items.each { |item| item_wid = item.link.split("=")[1] write_debug_log("Checking #{item_wid} (#{item.category.content}) at #{Time.now}") if (contraints_fulfilled(item, csv_accessor.data, severities, is_updated)) write_debug_log("Creating entry for #{item_wid} (#{item.category.content}) at #{Time.now}") csv_accessor.append_row([ item_wid, item.pubDate.localtime ]) process_item(item, config_file) end } days_oldest_entry = ((Time.now - feed.items.last.pubDate.localtime)/ (3600 *24)).ceil CertBot::CacheCleaner.delete_old_entries(, days_oldest_entry) end write_debug_log("Finishing rss parsing at #{Time.now}.\n") if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug]) @debug_log.close end nil end |
#write_debug_log(log_text) ⇒ Object (private)
method to write a given debug text into the logfile if the debug parameter is set
131 132 133 134 135 136 |
# File 'lib/cert_bot/rss_handler.rb', line 131 def write_debug_log(log_text) if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug]) @debug_log.puts(log_text) end nil end |