Class: EMN::Monitor::Pi

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Pi

Returns a new instance of Pi.



8
9
10
11
12
# File 'lib/emn/monitor/pi.rb', line 8

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

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



6
7
8
# File 'lib/emn/monitor/pi.rb', line 6

def api
  @api
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/emn/monitor/pi.rb', line 6

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/emn/monitor/pi.rb', line 6

def logger
  @logger
end

Instance Method Details

#notificationsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/emn/monitor/pi.rb', line 60

def notifications
  notifications = {
    :template => "templates/pi.erb",
    :subject => "Eve PI",
    :data => {}
  }

  pins do |toon, planet, pin|
    next if pin.expiryTime.year == 1

    time_till_expire = pin.expiryTime - Time.now.utc
    logger.debug("%s expires in %s / %s (%s)" % [planet.planetName, time_till_expire, seconds_to_human(time_till_expire), pin.expiryTime])

    seen[:planets][toon.name] ||= {}

    if (time_till_expire < 600) && (time_till_expire > -86400)
      if seen[:planets][toon.name][planet.planetName]
        next
      else
        seen[:planets][toon.name][planet.planetName] = true
      end

      notifications[:data][toon.name] ||= []
      notifications[:data][toon.name] << {
        :name => planet.planetName,
        :time_till_expire => seconds_to_human(time_till_expire),
        :expire_time => pin.expiryTime
      }
    else
      seen[:planets][toon.name].delete(planet.planetName)
    end
  end

  notifications[:seen] = seen
  notifications
end

#pinsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/emn/monitor/pi.rb', line 48

def pins
  api.characters.each do |toon|
    logger.debug("Processing toon %s" % toon.name)
    api.planets(toon).each do |planet|
      logger.debug("Processing planet %s" % planet.planetName)
      api.planet_pins(toon, planet).sort_by{|p| p.planetName}.each do |pin|
        yield(toon, planet, pin)
      end
    end
  end
end

#seconds_to_human(seconds) ⇒ Object



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
# File 'lib/emn/monitor/pi.rb', line 18

def seconds_to_human(seconds)
  if seconds < 0
    past = true
    seconds = seconds.abs
  end

  days = seconds.to_i / 86400
  seconds -= 86400 * days

  hours = seconds.to_i / 3600
  seconds -= 3600 * hours

  minutes = seconds.to_i / 60
  seconds -= 60 * minutes

  time = if days > 1
    "%d days %d hours %d minutes" % [days, hours, minutes]
  elsif days == 1
    "%d day %d hours %d minutes" % [days, hours, minutes]
  elsif hours > 0
    "%d hours %d minutes" % [hours, minutes]
  elsif minutes > 0
    "%d minutes" % [minutes]
  else
    "%d seconds" % [seconds]
  end

  past ? "%s ago" % time : time
end

#seenObject



14
15
16
# File 'lib/emn/monitor/pi.rb', line 14

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