Class: ETL::Processor::Pop3attachmentDownloaderProcessor

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

Overview

Custom processor to download files via Pop3 Attachment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control, configuration) ⇒ Pop3attachmentDownloaderProcessor

configuration options include:

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

  • ssl - activate encryption (default false)

  • port - port number for POP3 server (default: Net::POP3.default_port or Net::POP3.default_pop3s_port)

  • delete - delete message after reading (default false)

  • filters - filter mails (default [])

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

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

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/etl/processor/pop3attachment_downloader_processor.rb', line 26

def initialize(control, configuration)
  @host = configuration[:host]
  @ssl = configuration[:ssl] || false
  @port = configuration[:port] || (@ssl ? Net::POP3.default_pop3s_port : Net::POP3.default_port )
  @delete = configuration[:delete] || false
  @filters = configuration[:filters] || []
  @username = configuration[:username] || 'anonymous'
  @password = configuration[:password]
  @local_dir = configuration[:local_dir] || ''
end

Instance Attribute Details

#deleteObject (readonly)

Returns the value of attribute delete



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

def delete
  @delete
end

#filtersObject (readonly)

Returns the value of attribute filters



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

def filters
  @filters
end

#hostObject (readonly)

Returns the value of attribute host



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

def host
  @host
end

#local_dirObject (readonly)

Returns the value of attribute local_dir



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

def local_dir
  @local_dir
end

#portObject (readonly)

Returns the value of attribute port



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

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl



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

def ssl
  @ssl
end

#usernameObject (readonly)

Returns the value of attribute username



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

def username
  @username
end

Instance Method Details

#processObject



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

def process
  Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if @ssl
  conn = Net::POP3.new(@host, @port)
  conn.start(@username, @password)
  if !conn.mails.empty?
    conn.each_mail do |message|
      stringmail = message.pop
      mail = TMail::Mail.parse(stringmail)
      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

        message.delete if @delete
      end
    end
  end

  conn.finish
end