Class: Popper::MailAccount
- Inherits:
-
Object
- Object
- Popper::MailAccount
- Defined in:
- lib/popper/mail_account.rb
Instance Method Summary collapse
-
#initialize(config) ⇒ MailAccount
constructor
A new instance of MailAccount.
- #match_rule?(mail) ⇒ Boolean
- #pop(conn) ⇒ Object
- #process_uidl_list(conn) ⇒ Object
- #run ⇒ Object
- #session_start(&block) ⇒ Object
Constructor Details
#initialize(config) ⇒ MailAccount
Returns a new instance of MailAccount.
7 8 9 |
# File 'lib/popper/mail_account.rb', line 7 def initialize(config) @config = config end |
Instance Method Details
#match_rule?(mail) ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'lib/popper/mail_account.rb', line 71 def match_rule?(mail) @config.rule_with_conditions_all? do |rule, mail_header, conditions| conditions.all? do |condition| mail.respond_to?(mail_header) && mail.send(mail_header).to_s.match(/#{condition}/) end end end |
#pop(conn) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/popper/mail_account.rb', line 21 def pop(conn) done_uidls = [] error_uidls = [] Popper.log.info "start popper #{@config.name}" process_uidl_list(conn).each do |m| begin mail = EncodeMail.new(m.mail) Popper.log.info "check mail:#{mail.date.to_s} #{mail.subject}" if rule = match_rule?(mail) Popper.log.info "do action:#{mail.subject}" Popper::Action::Git.run(@config.action_by_rule(rule), mail) if @config.action_by_rule(rule) end done_uidls << m.uidl rescue Net::POPError => e @complete_uidl_list += done_uidls Popper.log.warn "pop err write uidl" return rescue => e error_uidls << m.uidl Popper.log.warn e end end @complete_uidl_list = @current_uidl_list - error_uidls Popper.log.info "success popper #{@config.name}" end |
#process_uidl_list(conn) ⇒ Object
66 67 68 69 |
# File 'lib/popper/mail_account.rb', line 66 def process_uidl_list(conn) uidl_list = @current_uidl_list - @complete_uidl_list conn.mails.select {|_m|uidl_list.include?(_m.uidl)} end |
#run ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/popper/mail_account.rb', line 11 def run session_start do |conn| @current_uidl_list = conn.mails.map(&:uidl) @complete_uidl_list = @current_uidl_list unless @complete_uidl_list pop(conn) end rescue => e Popper.log.warn e end |
#session_start(&block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/popper/mail_account.rb', line 52 def session_start(&block) pop = Net::POP3.new(@config.login.server, @config.login.port || 110) pop.open_timeout = ENV['POP_TIMEOUT'] || 120 pop.read_timeout = ENV['POP_TIMEOUT'] || 120 pop.start( @config.login.user, @config.login.password ) do |pop| Popper.log.info "connect server #{@config.name}" block.call(pop) Popper.log.info "disconnect server #{@config.name}" end end |