Class: ETL::Processor::ImapattachmentDownloaderProcessor

Inherits:
Processor show all
Defined in:
lib/etl/processor/imapattachment_downloader_processor.rb

Overview

Custom processor to download files via Imap Attachment

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ImapattachmentDownloaderProcessor) initialize(control, configuration)

configuration options include:

  • host - hostname or IP address of IMAP server (required)

  • ssl - activate encryption (default false)

  • port - port number for IMAP server (default: 220 or 993)

  • delete - delete message after reading (default false)

  • filters - filter mails (default [])

  • folder - folder to select mails from (default INBOX)

  • username - username for IMAP server authentication (default: anonymous)

  • password - password for IMAP server authentication (default: nil)

  • local_dir - local output directory to save downloaded files (default: '')



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 28

def initialize(control, configuration)
  @host = configuration[:host]
  @ssl = configuration[:ssl] || false
  @port = configuration[:port] || (@ssl ? 993 : 220 )
  @delete = configuration[:delete] || false
  @filters = configuration[:filters] || []
  @folder = configuration[:folder] || 'INBOX'
  @username = configuration[:username] || 'anonymous'
  @password = configuration[:password]
  @local_dir = configuration[:local_dir] || ''
end

Instance Attribute Details

- (Object) delete (readonly)

Returns the value of attribute delete



11
12
13
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 11

def delete
  @delete
end

- (Object) filters (readonly)

Returns the value of attribute filters



12
13
14
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 12

def filters
  @filters
end

- (Object) folder (readonly)

Returns the value of attribute folder



13
14
15
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 13

def folder
  @folder
end

- (Object) host (readonly)

Returns the value of attribute host



8
9
10
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 8

def host
  @host
end

- (Object) local_dir (readonly)

Returns the value of attribute local_dir



15
16
17
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 15

def local_dir
  @local_dir
end

- (Object) port (readonly)

Returns the value of attribute port



10
11
12
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 10

def port
  @port
end

- (Object) ssl (readonly)

Returns the value of attribute ssl



9
10
11
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 9

def ssl
  @ssl
end

- (Object) username (readonly)

Returns the value of attribute username



14
15
16
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 14

def username
  @username
end

Instance Method Details

- (Object) process



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/etl/processor/imapattachment_downloader_processor.rb', line 40

def process
  conn = Net::IMAP.new(@host, @port, @ssl)
  conn.(@username, @password)

  conn.select(@folder)
  conn.uid_search(["NOT", "DELETED"]).each do |msguuid|
    mail = TMail::Mail.parse( conn.uid_fetch(msguuid, 'RFC822').first.attr['RFC822'] )
    next if mail.attachments.blank?
    if applyfilter(mail, @filters)
      mail.attachments.each do |attachment|
        filename = attachment.original_filename
        File.open(local_file(filename), "w") {|f|
          f << attachment.gets(nil)
        }
      end

      conn.store(msguuid, "+FLAGS", [:Deleted]) if @delete
    end
  end
  conn.expunge
  conn.close
end