Class: Mbox2CSV::EmailStatistics
- Inherits:
-
Object
- Object
- Mbox2CSV::EmailStatistics
- Defined in:
- lib/mbox2csv.rb
Overview
The EmailStatistics class is responsible for gathering and writing statistics related to emails. It tracks sender frequency, recipient frequency, and calculates the average email body length per sender.
Instance Method Summary collapse
-
#initialize ⇒ EmailStatistics
constructor
A new instance of EmailStatistics.
-
#record_email(from, to, body_length) ⇒ Object
Records an email's sender, recipients, and body length for statistical purposes.
-
#save_recipient_statistics(csv_filename) ⇒ Object
Saves recipient statistics to a CSV file and prints them to the console.
-
#save_sender_statistics(csv_filename) ⇒ Object
Saves sender statistics to a CSV file and prints them to the console.
Constructor Details
#initialize ⇒ EmailStatistics
Returns a new instance of EmailStatistics.
273 274 275 276 277 |
# File 'lib/mbox2csv.rb', line 273 def initialize @sender_counts = Hash.new(0) @recipient_counts = Hash.new(0) @body_lengths = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#record_email(from, to, body_length) ⇒ Object
Records an email's sender, recipients, and body length for statistical purposes.
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/mbox2csv.rb', line 284 def record_email(from, to, body_length) return if from.empty? @sender_counts[from] += 1 @body_lengths[from] << body_length Array(to).each do |recipient| @recipient_counts[recipient] += 1 end end |
#save_recipient_statistics(csv_filename) ⇒ Object
Saves recipient statistics to a CSV file and prints them to the console.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/mbox2csv.rb', line 320 def save_recipient_statistics(csv_filename) sorted_recipients = @recipient_counts.sort_by { |_recipient, count| -count } CSV.open(csv_filename, 'w') do |csv| csv << ['Recipient', 'Email Count'] sorted_recipients.each do |recipient, count| csv << [recipient, count] end end puts "\nRecipient Email Statistics:" sorted_recipients.each do |recipient, count| puts "#{recipient}: #{count} emails" end end |
#save_sender_statistics(csv_filename) ⇒ Object
Saves sender statistics to a CSV file and prints them to the console.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/mbox2csv.rb', line 298 def save_sender_statistics(csv_filename) sorted_senders = @sender_counts.sort_by { |_sender, count| -count } average_body_lengths = @body_lengths.transform_values { |lengths| lengths.sum / lengths.size.to_f } CSV.open(csv_filename, 'w') do |csv| csv << ['Sender', 'Email Count', 'Average Body Length (chars)'] sorted_senders.each do |sender, count| avg_length = average_body_lengths[sender].round(2) csv << [sender, count, avg_length] end end puts "Sender Email Statistics:" sorted_senders.each do |sender, count| avg_length = average_body_lengths[sender].round(2) puts "#{sender}: #{count} emails, Average body length: #{avg_length} chars" end end |