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.
116 117 118 119 120 |
# File 'lib/mbox2csv.rb', line 116 def initialize @sender_counts = Hash.new(0) # Keeps count of emails per sender @recipient_counts = Hash.new(0) # Keeps count of emails per recipient @body_lengths = Hash.new { |hash, key| hash[key] = [] } # Stores body lengths per sender end |
Instance Method Details
#record_email(from, to, body_length) ⇒ Object
Records an email’s sender, recipients, and body length for statistical purposes.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mbox2csv.rb', line 127 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.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mbox2csv.rb', line 163 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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mbox2csv.rb', line 141 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 |