class EmailNugget
Attributes
envelope[RW]
id[RW]
message[RW]
Public Class Methods
new(args = {})
click to toggle source
Initialize a new email nugget.
# File lib/email_nugget.rb, line 12 def initialize(args = {}) self.envelope = EmailNugget::Envelope.new(args[:envelope] || args['envelope']) self.message = EmailNugget::Message.new(args[:message] || args['message']) self.id = args[:id] || args['id'] || generate_id end
new_from(file_path)
click to toggle source
Create a new email nugget from the specified file path.
# File lib/email_nugget.rb, line 59 def new_from(file_path) # Catch any errors opening or reading from the file. begin nugget_file = File.open(file_path, 'rb') # Envelope is the first line. envelope = JSON.parse(nugget_file.readline.chomp) # Checksum is the second line. checksum = nugget_file.readline.chomp return EmailNugget.new({'envelope' => envelope, 'message' => {'data_stream' => nugget_file}}) rescue => e # Unable to open or read nugget file. #puts "Error creating email nugget from #{file_path}: #{e.to_s}" return nil end end
Public Instance Methods
generate_id()
click to toggle source
Generate a new UUID
# File lib/email_nugget.rb, line 19 def generate_id UUID.new.generate end
to_hash()
click to toggle source
# File lib/email_nugget.rb, line 23 def to_hash { :ip => self.envelope.ip, :helo => self.envelope.helo, :mail_from => self.envelope.mail_from, :rcpt_to => self.envelope.rcpt_to, :date => self.envelope.date, :context => self.envelope.context, :misc => self.envelope.misc, } end
write_to(file_path)
click to toggle source
Save the current nugget to the specified file.
# File lib/email_nugget.rb, line 36 def write_to(file_path) # Catch any errors opening or writing to the file. begin nugget_file = File.open(file_path, "w") # Get a lock on the file. nugget_file.flock(File::LOCK_EX | File::LOCK_NB) # Save the envelope as JSON in the first line. nugget_file.syswrite(to_hash.to_json + "\n") # Save the checksum in the second line. nugget_file.syswrite(self.message.checksum + "\n") # Save the message contents after the second line. nugget_file.syswrite(self.message.data) rescue => e # Unable to save email nugget to file_path #puts "Error saving email nugget to #{file_path}: #{e.to_s}" return false end # Success return true end