Class: ClassicOmah

Inherits:
Omah
  • Object
show all
Defined in:
lib/classic_omah.rb

Instance Method Summary collapse

Constructor Details

#initialize(user: 'user', filepath: '.', mail: {}, email_address: nil, options: {xslt: 'listing.xsl'}, plugins: []) ⇒ ClassicOmah

Returns a new instance of ClassicOmah.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/classic_omah.rb', line 14

def initialize(user: 'user', filepath: '.', mail: {}, \
             email_address: nil, options: {xslt: 'listing.xsl'}, plugins: [])
      
  @mail = {    address: '',
                  port: 110,
             user_name: '',
              password: '',
            enable_ssl: false }.merge mail

  field = %i(address user_name password).detect {|x| @mail[x].to_s.empty?}

  raise ClassicOmahException, "missing %s" % field if field        
  
  @variables = {user_name: @mail[:user_name] , \
                address: @mail[:address], email_address: email_address}
  
  super(user: user, filepath: filepath, plugins: plugins, options: options)    
  
  
end

Instance Method Details

#fetch_emailObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/classic_omah.rb', line 35

def fetch_email()
  
  mail = @mail
  Mail.defaults { retriever_method(:pop3, mail) }

  email = Mail.all

  return 'no new messages' unless email.any?

  messages = email.map.with_index.inject([]) do |r, x|
    
    msg, i = x

    begin
      r << {
        msg_id:     msg.message_id,
        from:       msg.from.is_a?(Array) ? msg.from.join(', ') : msg.from,
        to:         msg.to.is_a?(Array) ? msg.to.join(', ') : msg.to,
        subject:    msg.subject,
        date:       msg.date.to_s,
        body_text:  (msg.text_part ? msg.text_part.body.decoded : msg.body),
        body_html:  (msg.html_part ? msg.html_part.body.decoded : msg.body), 
        attachments: msg.attachments.map {|x| [x.filename, x.body.decoded] },
        raw_source: msg.raw_source
      }
    rescue
      puts 'warning: ' + ($!).inspect
    end
  r

  end
  
  # messages are stored in the file dynarexdaily.xml
  self.store messages

  Mail.delete_all

  messages.length.to_s + " new messages"
end