Class: EMN::Monitor::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/emn/monitor/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mail

Returns a new instance of Mail.



6
7
8
9
10
# File 'lib/emn/monitor/mail.rb', line 6

def initialize(config)
  @config = config
  @logger = config.logger
  @api = EveAPI.new(config)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



4
5
6
# File 'lib/emn/monitor/mail.rb', line 4

def api
  @api
end

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/emn/monitor/mail.rb', line 4

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/emn/monitor/mail.rb', line 4

def logger
  @logger
end

Instance Method Details

#notificationsObject



16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/emn/monitor/mail.rb', line 16

def notifications
  notifications = {
    :template => "templates/mail.erb",
    :subject => "Eve Mail",
    :seen => config.seen,
    :data => {}
  }

  api.characters.each do |toon|
    logger.debug("Checking character %s" % toon.name)

    messages = api.mails(toon)

    logger.debug("Found %d messages for %s" % [messages.size, toon.name])

    messages.each do |mail|
      logger.debug("Processing email %s: from: %s subject: %s" % [mail.messageID, mail.senderName, mail.title])

      seen[:mail][toon.name] ||= "0"

      if seen[:mail][toon.name] < mail.messageID
        seen[:mail][toon.name] = mail.messageID

        if mail.senderName == toon.name
          logger.debug("Skipping mail %s as it's from the character" % mail.messageID)
          next
        end

        logger.debug("Adding email %s: %s to the queue" % [mail.messageID, mail.title])

        notifications[:data][toon.name] ||= []
        notifications[:data][toon.name] << {
          :id => mail.messageID,
          :from => mail.senderName,
          :date => mail.sentDate,
          :subject => mail.title
        }
      else
        logger.debug("Skipping email %s: it's been seen before (%s)" % [mail.messageID, seen[:mail][toon.name]])
      end
    end
  end

  notifications[:seen] = seen
  notifications
end

#seenObject



12
13
14
# File 'lib/emn/monitor/mail.rb', line 12

def seen
  @seen ||= {:mail => config.seen.fetch(:mail, {})}
end