Class: Mbox2CSV::MboxParser
- Inherits:
-
Object
- Object
- Mbox2CSV::MboxParser
- Defined in:
- lib/mbox2csv.rb
Overview
Main class
Instance Method Summary collapse
-
#initialize(mbox_file, csv_file, stats_csv_file, recipient_stats_csv_file) ⇒ MboxParser
constructor
Initializes the MboxParser with file paths for the MBOX file, output CSV file, and statistics CSV files for sender statistics.
-
#parse ⇒ Object
Parses the MBOX file and writes the email data to the specified CSV file.
Constructor Details
#initialize(mbox_file, csv_file, stats_csv_file, recipient_stats_csv_file) ⇒ MboxParser
Initializes the MboxParser with file paths for the MBOX file, output CSV file, and statistics CSV files for sender statistics.
15 16 17 18 19 20 21 |
# File 'lib/mbox2csv.rb', line 15 def initialize(mbox_file, csv_file, stats_csv_file, recipient_stats_csv_file) @mbox_file = mbox_file @csv_file = csv_file @statistics = EmailStatistics.new @stats_csv_file = stats_csv_file @recipient_stats_csv_file = recipient_stats_csv_file end |
Instance Method Details
#parse ⇒ Object
Parses the MBOX file and writes the email data to the specified CSV file. It also saves sender and recipient statistics to separate CSV files.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mbox2csv.rb', line 25 def parse CSV.open(@csv_file, 'w') do |csv| # Write CSV header csv << ['From', 'To', 'Subject', 'Date', 'Body'] File.open(@mbox_file, 'r') do |mbox| buffer = "" mbox.each_line do |line| if line.start_with?("From ") process_email_block(buffer, csv) unless buffer.empty? buffer = "" # Reset buffer end buffer << line # Append line to buffer end process_email_block(buffer, csv) unless buffer.empty? # Process last email block end end puts "Parsing completed. Data saved to #{@csv_file}" # Save and print statistics after parsing @statistics.save_sender_statistics(@stats_csv_file) @statistics.save_recipient_statistics(@recipient_stats_csv_file) rescue => e puts "Error processing MBOX file: #{e.}" end |