File Coverage

blib/lib/POE/Component/IRC/Plugin/WWW/KrispyKreme/HotLight.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::WWW::KrispyKreme::HotLight;
2              
3 1     1   26890 use 5.008_005;
  1         4  
  1         31  
4 1     1   787 use WWW::KrispyKreme::HotLight;
  1         832212  
  1         14  
5 1     1   1346 use IRC::Utils qw(parse_user);
  1         22220  
  1         148  
6 1     1   4197 use Carp::POE qw(croak);
  0            
  0            
7              
8             our $VERSION = '0.06';
9              
10             use POE::Component::IRC::Plugin qw( :ALL );
11              
12             our @channels;
13              
14             sub new {
15             my ($class, %args) = @_;
16             croak 'must supply geo as array ref i.e. ->new(geo => [35.045556,-85.267222])'
17             unless $args{geo}
18             and ref $args{geo}
19             and ref $args{geo} eq 'ARRAY';
20             $args{ping} = 0;
21             return bless \%args, $class;
22             }
23              
24             # list events we are interested in
25             sub PCI_register {
26             my ($self, $irc) = splice @_, 0, 2;
27             $irc->plugin_register($self, 'SERVER', qw(join ping));
28             return 1;
29             }
30              
31             # This is method is mandatory but we don't actually have anything to do.
32             sub PCI_unregister {
33             return 1;
34             }
35              
36             # trigger a donut search when:
37             # -we join a channel
38             # -we are pinged by the server (every 20 times)
39             sub S_join {
40             my ($self, $irc) = splice @_, 0, 2;
41             my $joiner = parse_user(${$_[0]});
42             my $nick = $irc->{nick};
43             my $channel = ${$_[1]};
44             push @channels, $channel;
45             return $joiner eq $nick ? $self->_donuts($channel, $irc) : PCI_EAT_NONE;
46             }
47              
48             sub S_ping {
49             my ($self, $irc) = splice @_, 0, 2;
50             $self->{ping}++;
51              
52             # ping interval on freenode is ~2 mins
53             my $ready = $self->{ping} % 20 == 0;
54              
55             return PCI_EAT_NONE unless @channels and $ready;
56             return $self->_donuts($_, $irc) for @channels;
57             }
58              
59             sub _donuts {
60             my ($self, $channel, $irc) = @_;
61              
62             my $donuts = WWW::KrispyKreme::HotLight->new(where => $self->{geo})->locations;
63             my $stores = join ', ', map $_->{title}, grep $_->{hotLightOn}, @$donuts;
64             $irc->yield( #
65             privmsg => $channel =>
66             "Fresh donuts available at the following Kripsy Kreme locations: $stores"
67             ) if $stores;
68             return PCI_EAT_PLUGIN;
69             }
70              
71             1;
72             __END__