Class: MailRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/airmail/mail_router.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ MailRouter



3
4
5
# File 'lib/airmail/mail_router.rb', line 3

def initialize(mail)
  @mail = mail
end

Class Method Details

.info(msg) ⇒ Object



64
65
66
# File 'lib/airmail/mail_router.rb', line 64

def info msg
  @logger && @logger.info(msg)
end

.logger=(loggr) ⇒ Object



68
69
70
# File 'lib/airmail/mail_router.rb', line 68

def logger= loggr
  @logger = loggr
end

.receive(original) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/airmail/mail_router.rb', line 44

def receive( original )
  mail = Mail::Message.new( original )     

  record(mail, original)
  self.new(mail).route

  mail
end

.record(mail, original) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/airmail/mail_router.rb', line 53

def record(mail, original)
  Message.create!(
    from: mail.from.first,
    #reference_id: mail.
    to: mail.to,
    subject: mail.subject,
    original: original,
    taxonomy: 'log'
  )
end

Instance Method Details

#deliver(controller) ⇒ Object



38
39
40
# File 'lib/airmail/mail_router.rb', line 38

def deliver controller
  controller.new(@mail).receive
end

#from?(*args) ⇒ Boolean



30
31
32
# File 'lib/airmail/mail_router.rb', line 30

def from? *args
  from
end

#has_attachment?Boolean



34
35
36
# File 'lib/airmail/mail_router.rb', line 34

def has_attachment?
  @mail.attachments.size > 0
end

#routeObject

this is the app specific controller logic



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/airmail/mail_router.rb', line 7

def route #this is the app specific controller logic
  MailRouter.info("Receiving email '#{@mail.subject}' from #{@mail.from}, #{@mail.to} with #{@mail.attachments.count} attachments")

  @mail.attachments.each do |attach|
    MailRouter.info(">> #{attach.filename}")#(#{a.content_type.to_s})")
  end

  #TODO catch mailerdaemon stuff
  #TODO circular loops? throttle speed
  #if from? :mailerdaemon, :root

  (deliver MailDemoController and return) if @mail.to.join(" ") =~ /demo.*@amendment.io/

  if has_attachment? 
    #if not on the contact list, don't grant access to services
    (deliver ThanksForPlayingController and return) unless Contact.find_by_email(@mail.from.collect{|f| f.downcase})

    (deliver AttachmentController and return) 
  end

  deliver DefaultController and return
end