blib/lib/POE/Component/IRC/Plugin/WWW/Reddit/TIL.pm

filter: Statementstmt 56.5% 26 / 46 Branchbran 25.0% 1 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 4 totaltotal 49.2% 34 / 69 CC 5 SCAR 13.9
CC
5
Cov
51%
S_public
12.0
_get_TIL
3.4
PCI_register
2.0
CRAP 4.0
↑ Summary
1 package POE::Component::IRC::Plugin::WWW::Reddit::TIL;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.09';
8  
9 1 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 1 my $package = shift;
14 1 return bless {}, $package;
15 }
16  
17 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
18 0 my ($self, $irc) = splice @_, 0, 2;
19  
20 0 $irc->plugin_register($self, 'SERVER', qw(public));
21 0 return 1;
22 }
23  
24 # This is method is mandatory but we don't actually have anything to do.
25 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
26 0 return 1;
27 }
28  
29 0 ▶sub S_public {
Subroutine S_public: not called CC=3 SCAR=24.8
Pod undocumented
30 0 my ($self, $irc) = splice @_, 0, 2;
31  
32 # Parameters are passed as scalar-refs including arrayrefs.
33 0 my $nick = (split /!/, ${$_[0]})[0];
34 0 my $channel = ${$_[1]}->[0];
35 0 my $msg = ${$_[2]};
36  
37 0 ▶ my $reply = $msg =~ /^!til\b/i && $self->_get_TIL;
Condition0%0/3
$msg =~ /^!til\b/i && $self->_get_TIL
!ll&&!rl&&r
000
MC/DC0%0/2
$msg =~ /^!til\b/i && $self->_get_TIL
$msg =~ /^!til\b/i $self->_get_TIL
Truth table0%0/3
A && B
A= $msg =~ /^!til\b/iB= $self->_get_TIL
ABresult
0X0
100
111
38  
39 0 ▶ if ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply) 0 0 0
40 0 $irc->yield(privmsg => $channel => "$nick: $reply");
41 0 return PCI_EAT_PLUGIN;
42 }
43  
44 # Default action is to allow other plugins to process it.
45 0 return PCI_EAT_NONE;
46 }
47  
48 1 ▶sub _get_TIL {
Subroutine _get_TIL: called CC=3 SCAR=12.3
49 1 my $ua = Mojo::UserAgent->new;
50 1 my @posts;
51  
52 1 my $json = $ua->get('https://www.reddit.com/r/todayilearned.json')->res->json;
53  
54 # Children are posts in reddit world.
55 1 foreach my $post (@{$json->{data}{children}}) {
56 my %info = (
57 url => 'https://reddit.com/' . $post->{data}{id},
58 title => $post->{data}{title},
59 0 );
60 0 push @posts, \%info;
61 }
62 1 ▶ unless (@posts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@posts) 1 0 1
63 1 warn q@Found no posts! Something's probably wrong.@;
64 1 return;
65 }
66  
67 0 my $random = $posts[rand @posts];
68 0 return $random->{title} . ' ' . $random->{url};
69 }
70  
71 1 1;
72  
73 __END__
↑ Summary