File Coverage

blib/lib/POE/Component/IRC/Plugin/Donuts.pm
Criterion Covered Total %
statement 19 41 46.3
branch 1 8 12.5
condition 2 9 22.2
subroutine 6 11 54.5
pod 0 5 0.0
total 28 74 37.8


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