class MailRoom::MailboxWatcher

Watch a Mailbox @author Tony Pitale

Attributes

watching_thread[RW]

Public Class Methods

new(mailbox) click to toggle source

Watch a new mailbox @param mailbox [MailRoom::Mailbox] the mailbox to watch

# File lib/mail_room/mailbox_watcher.rb, line 11
def initialize(mailbox)
  @mailbox = mailbox

  @running = false
  @connection = nil
end

Public Instance Methods

quit() click to toggle source

stop running, cleanup connection

# File lib/mail_room/mailbox_watcher.rb, line 42
def quit
  @running = false

  if @connection
    @connection.quit
    @connection = nil
  end

  if self.watching_thread
    self.watching_thread.join
  end
end
run() click to toggle source

run the mailbox watcher

# File lib/mail_room/mailbox_watcher.rb, line 25
def run
  @running = true

  connection.on_new_message do |message|
    @mailbox.deliver(message)
  end

  self.watching_thread = Thread.start do
    while(running?) do
      connection.wait
    end
  end

  watching_thread.abort_on_exception = true
end
running?() click to toggle source

are we running? @return [Boolean]

# File lib/mail_room/mailbox_watcher.rb, line 20
def running?
  @running
end

Private Instance Methods

connection() click to toggle source
# File lib/mail_room/mailbox_watcher.rb, line 56
def connection
  @connection ||= Connection.new(@mailbox)
end