How to build a mail search engine using Nitro

By Kashia.

Part 4 - M as in Model

Now that we have a solid foundation to build on, we can actually go back a bit and look at Og, which we will use to store our emails. As promised earlier, this is one of the files which were missing.

app/mail/model_mail.rb:

#require 'mail_utils.rb'

# Class Mail
class IndexedMail
  include ModEMailParser                # (1.)
  
  property :id, String, :unique => true # (2.)
  property :email_from, String
  property :subject, String
  property :date, Time
  
  property :header, String
  property :body, String
  
  ann self, :ignore_fields => [ :idxfti ]# (3.)
  
  # .new(raw) parses a raw email message and stores its parts in instance variables
  def initialize(raw)
    @email_from, @id, @date, @subject, @header, @body = parse_mail(raw)
  end
  
  # .description() is used, to get a small excerpt of the body
  def description
    @body[0..200].squeeze("\s").gsub(/<[^>]+>/, '').gsub(/<.*$/, '') + '...'
  end
  
  # is used to get the rank, which will be put into the class automagically
  # by Og later, when using the self-made PostgreSQL search engine.
  def find_rank
    @rank
  end
  
end # end class IndexedMail

  1. Hah! Nice to have utility functions available. Now one can just call parse_mail(raw), like shown in def initialize.

  2. All these property tags get saved in the database later, with the appropriate type.

  3. Well, that field ("idxfti") it to be ignored by Og, we will take care of it manually and really don't want Og to be messing with it.

So, now we have the M of MVC already covered. Only 2 to go.

But, before we go on to the View, lets make sure there is actually something to see, would be boring otherwise, right?

Let's fill the database!

first
last