class PollterGeist::Poller

Constants

LOGGER

Public Class Methods

new(options) click to toggle source
# File lib/pollter_geist/poller.rb, line 8
def initialize options
  @options = options
end

Public Instance Methods

session(&block) click to toggle source
# File lib/pollter_geist/poller.rb, line 12
def session &block
  start(&block)
end

Private Instance Methods

logger() click to toggle source
# File lib/pollter_geist/poller.rb, line 43
def logger
  LOGGER
end
start() { |commands| ... } click to toggle source
# File lib/pollter_geist/poller.rb, line 17
def start &block
  logger.debug "connecting to #{@options[:host]}"
  imap = Net::IMAP.new(@options[:host])
  logger.debug 'connected to imap server'

  imap.login(@options[:username], @options[:password])
  logger.debug 'authenticated to the server'

  begin
    commands = IMAPCommands.new(imap, logger)

    # preselecting mailbox if present in the options
    commands.select_mailbox(@options[:mailbox]) unless @options[:mailbox].nil?

    # let client code work their magic
    yield(commands)
  rescue Exception => e
    logger.error "#{e.class} : #{e.message} \n #{e.backtrace}"
    raise e
  ensure
    imap.logout
    logger.debug 'logoff'
  end
end