File Coverage

blib/lib/POE/Component/IRC/Plugin/WWW/Reddit/TIL.pm
Criterion Covered Total %
statement 30 48 62.5
branch 2 6 33.3
condition 1 6 16.6
subroutine 8 11 72.7
pod 0 4 0.0
total 41 75 54.6


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::WWW::Reddit::TIL;
2              
3 1     1   28526 use 5.010;
  1         4  
  1         48  
4 1     1   10 use strict;
  1         3  
  1         37  
5 1     1   5 use warnings;
  1         5  
  1         66  
6              
7             our $VERSION = '0.07';
8              
9 1     1   1011 use POE::Component::IRC::Plugin qw( :ALL );
  1         471  
  1         154  
10 1     1   1021 use Mojo::UserAgent;
  1         366824  
  1         18  
11 1     1   1077 use WWW::Shorten::Simple;
  1         663  
  1         490  
12              
13             sub new {
14 1     1 0 683 my $package = shift;
15 1         7 return bless {}, $package;
16             }
17              
18             sub PCI_register {
19 0     0 0 0 my ($self, $irc) = splice @_, 0, 2;
20              
21 0         0 $irc->plugin_register($self, 'SERVER', qw(public));
22 0         0 return 1;
23             }
24              
25             # This is method is mandatory but we don't actually have anything to do.
26             sub PCI_unregister {
27 0     0 0 0 return 1;
28             }
29              
30             sub S_public {
31 0     0 0 0 my ($self, $irc) = splice @_, 0, 2;
32              
33             # Parameters are passed as scalar-refs including arrayrefs.
34 0         0 my $nick = (split /!/, ${$_[0]})[0];
  0         0  
35 0         0 my $channel = ${$_[1]}->[0];
  0         0  
36 0         0 my $msg = ${$_[2]};
  0         0  
37              
38 0   0     0 my $reply = $msg =~ /^!til\b/i && $self->_get_TIL;
39              
40 0 0       0 if ($reply) {
41 0         0 $irc->yield(privmsg => $channel => "$nick: $reply");
42 0         0 return PCI_EAT_PLUGIN;
43             }
44              
45             # Default action is to allow other plugins to process it.
46 0         0 return PCI_EAT_NONE;
47             }
48              
49             sub _get_TIL {
50 1     1   409 my $ua = Mojo::UserAgent->new;
51 1         11 my %posts;
52 1         3 foreach my $post (
  1         8  
53             @{$ua->get('http://www.reddit.com/r/todayilearned.json')->res->json->{data}{children}})
54             {
55 26         97829 my $title = $post->{data}{title};
56 26         39 my $link = $post->{data}{url};
57 26 50 33     160 $posts{$title} = $link if $title && $link;
58             }
59 1 50       300 return unless %posts;
60              
61 1         23 my $title = (keys %posts)[rand keys %posts];
62 1         15 my $short =
63             WWW::Shorten::Simple->new('Bitly', 'aggrolite', 'R_418414782c81e2c4444348e367201706');
64 0           my $link = $short->shorten($posts{$title});
65 0           return "$title $link";
66             }
67              
68             1;
69              
70             __END__