Class: IncomingMail

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/sum/model/incoming_mail.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process!Object



4
5
6
7
8
# File 'lib/sum/model/incoming_mail.rb', line 4

def process!
  if $mail.config
    Fetcher.create($mail.config[:imap]).fetch
  end
end

Instance Method Details

#receive(mail) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sum/model/incoming_mail.rb', line 11

def receive(mail)
  return unless body = get_body(mail)
  emails = parse_emails(mail.subject, body.body)
  numbers = parse_numbers(mail.subject, body.body)
  start = parse_start(mail.subject, body.body)
  stop = parse_stop(mail.subject, body.body)
  
  if mail.from[0] && email = UserEmail.find_by_email(mail.from[0])
    user = email.user
  else
    return
  end
  
  if user
    emails.each do |email|
      user.add_email!(email, true)
    end
    numbers.each do |number|
      user.spend!(number)
    end
    email.activate! if start
    email.deactivate! if stop
  end
  
  [ emails, numbers, start, stop ]
end