Exobrain/Agent/Action/BeeInbox.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 57 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Exobrain::Agent::Action::BeeInbox;
2 use Exobrain;
3 use Method::Signatures;
4 use Moose;
5  
6 with 'Exobrain::Agent::Run';
7  
8 use constant DEBUG => 0;
9  
10 # ABSTRACT: Watch inbox and send low-water marks to beeminder
11 our $VERSION = '1.08'; # VERSION
12  
13  
14 method run() {
15  
16 my $exobrain = $self->exobrain;
17 my $cache = $self->cache;
18  
19 # Load our dependencies first so we fail quickly if they're
20 # not installed.
21  
22 # TODO: Replace these with a real way of signalling dependencies (GH #44).
23  
24 $exobrain->_load_component('Measurement::Mailbox');
25 $exobrain->_load_component('Intent::Beeminder');
26  
27 # Our inbox goals are 'server => beeminder goal' pairs.
28 my $inbox_goals = $self->config;
29  
30 $exobrain->watch_loop(
31 class => 'Measurement::Mailbox',
32 filter => sub {
33 $_->mailbox eq 'INBOX' and $inbox_goals->{ $_->server }
34 },
35 then => sub {
36 my $event = shift;
37  
38 my $server = $event->server;
39 my $goal = $inbox_goals->{$server};
40 my $count = $event->count;
41 my $key = ['INBOX', $server];
42  
43 warn "$server has $count messages for $goal\n" if DEBUG;
44  
45 my $old_count = $cache->get($key);
46  
47 # If we don't have a count yet, or if our new count is
48 # smaller, then cache it, and send it to beeminder!
49  
50 # Our cache expires at midnight, so we always push a
51 # new measurement each day.
52  
53 if (! $old_count or $count < $old_count) {
54 $exobrain->intent('Beeminder',
55 goal => $goal,
56 value => $count,
57 comment => "Submitted via exobrain",
58 );
59  
60 $exobrain->notify(
61 "Updated bmndr/$goal to $count msgs",
62 priority => -1,
63 );
64  
65 say "Updating beeminder goal $goal to $count msgs" if DEBUG;
66  
67 $cache->set($key, $count, seconds_until_tomorrow());
68 }
69 }
70 );
71 }
72  
73 sub seconds_until_tomorrow {
74 my ($s, $m, $h) = localtime;
75 return 86400 - ($h * 60 * 60) - ($m * 60) - $s;
76 }
77  
78 1;
79  
80 __END__