File Coverage

blib/lib/POE/Component/IRC/Plugin/WWW/Reddit/TIL.pm
Criterion Covered Total %
statement 24 44 54.5
branch 1 4 25.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 0 4 0.0
total 32 65 49.2


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::WWW::Reddit::TIL;
2              
3 1     1   12764 use 5.010;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         15  
5 1     1   3 use warnings;
  1         1  
  1         33  
6              
7             our $VERSION = '0.08';
8              
9 1     1   429 use POE::Component::IRC::Plugin qw( :ALL );
  1         366  
  1         131  
10 1     1   468 use Mojo::UserAgent;
  1         212393  
  1         7  
11              
12             sub new {
13 1     1 0 418 my $package = shift;
14 1         3 return bless {}, $package;
15             }
16              
17             sub PCI_register {
18 0     0 0 0 my ($self, $irc) = splice @_, 0, 2;
19              
20 0         0 $irc->plugin_register($self, 'SERVER', qw(public));
21 0         0 return 1;
22             }
23              
24             # This is method is mandatory but we don't actually have anything to do.
25             sub PCI_unregister {
26 0     0 0 0 return 1;
27             }
28              
29             sub S_public {
30 0     0 0 0 my ($self, $irc) = splice @_, 0, 2;
31              
32             # Parameters are passed as scalar-refs including arrayrefs.
33 0         0 my $nick = (split /!/, ${$_[0]})[0];
  0         0  
34 0         0 my $channel = ${$_[1]}->[0];
  0         0  
35 0         0 my $msg = ${$_[2]};
  0         0  
36              
37 0   0     0 my $reply = $msg =~ /^!til\b/i && $self->_get_TIL;
38              
39 0 0       0 if ($reply) {
40 0         0 $irc->yield(privmsg => $channel => "$nick: $reply");
41 0         0 return PCI_EAT_PLUGIN;
42             }
43              
44             # Default action is to allow other plugins to process it.
45 0         0 return PCI_EAT_NONE;
46             }
47              
48             sub _get_TIL {
49 1     1   242 my $ua = Mojo::UserAgent->new;
50 1         6 my @posts;
51              
52 1         5 my $json = $ua->get('https://www.reddit.com/r/todayilearned.json')->res->json;
53              
54             # Children are posts in reddit world.
55 1         54655 foreach my $post (@{$json->{data}{children}}) {
  1         7  
56             my %info = (
57             url => 'https://reddit.com/' . $post->{data}{id},
58             title => $post->{data}{title},
59 0         0 );
60 0         0 push @posts, \%info;
61             }
62 1 50       5 unless (@posts) {
63 1         19 warn q@Found no posts! Something's probably wrong.@;
64 1         6 return;
65             }
66              
67 0           my $random = $posts[rand @posts];
68 0           return $random->{title} . ' ' . $random->{url};
69             }
70              
71             1;
72              
73             __END__