lib/email-fetch-and-process.rb
# Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job .
def run(jobs = [])
    @imap = imap_connection
    jobs.each do |job|
      msg_ids = if job.multiple_fetch_terms?
                  all_ids = []
                  job.fetch.each { |j| all_ids += @imap.search(j) }
                  all_ids
                else
                  @imap.search(job.fetch)
                end
      next if msg_ids.nil? || msg_ids.empty?
      begin
        msgs = @imap.fetch(msg_ids, %w[ENVELOPE RFC822])
        msg = msgs.max_by { |m| Time.parse(m['attr']['ENVELOPE'].date) }.attr['RFC822']
        @body_index = 1
      rescue StandardError => err
        puts "Error: #{err}\n#{err.backtrace.join("\n")}"
        @imap = imap_connection
        next
      end
      body = Mail.read_from_string msg
      handle_parts(body.attachments, job, msg_ids) unless body.attachments.empty?
    end
    @imap.close
  end
def run ( jobs = [ ] ) @imap = imap_connection jobs . each do | job | msg_ids = if job . multiple_fetch_terms? all_ids = [ ] job . fetch . each { | j | all_ids += @imap . search ( j ) } all_ids else @imap . search ( job . fetch ) end next if msg_ids . nil? || msg_ids . empty? begin msgs = @imap . fetch ( msg_ids , %w[ ENVELOPE RFC822 ] ) msg = msgs . max_by { | m | Time . parse ( m [ 'attr' ] [ 'ENVELOPE' ] . date ) } . attr [ 'RFC822' ] @body_index = 1 rescue StandardError => err puts "Error: #{err}\n#{err.backtrace.join("\n")}" @imap = imap_connection next end body = Mail . read_from_string msg handle_parts ( body . attachments , job , msg_ids ) unless body . attachments . empty? end @imap . close end
Class tp encapsulate a fetch and handle job.
Class tp encapsulate a fetch and handle job .
jobs
<not_specific>