blib/lib/Plagger/Plugin/Notify/Slack.pm

filter: Statementstmt 56.4% 22 / 39 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 43.9% 29 / 66 CC 8 SCAR 31.9
CC
8
Cov
42%
publish
56.0
initialize
6.0
register
2.0
CRAP 24.3
↑ Summary
1 package Plagger::Plugin::Notify::Slack;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( Plagger::Plugin );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = "0.03";
12  
13 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod undocumented
14 0 my ( $self, $context ) = @_;
15 0 $context->register_hook(
16 $self,
17 'publish.entry' => $self->can('publish'),
18 'plugin.init' => $self->can('initialize'),
19 );
20 }
21  
22 0 ▶sub initialize {
Subroutine initialize: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my ( $self, $context, $args ) = @_;
24  
25 0 ▶ $self->{remote} = $self->conf->{webhook_url} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"remote"} = $self->conf->{"webhook_url"} or return 0 0 0
26 }
27  
28 0 ▶sub publish {
Subroutine publish: not called CC=7 SCAR=40.3
Pod undocumented
29 0 my ( $self, $context, $args ) = @_;
30  
31 0 $context->log( info => "Notifying " . $args->{entry}->title . " to Slack" );
32  
33 0 my $text = $self->templatize( 'notify.tt', $args );
34 0 ▶ Encode::_utf8_off($text) if Encode::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($text) 0 0 0
35  
36 0 my $payload = +{ text => $text };
37 0 ▶ $payload->{username} = $self->conf->{username} if exists $self->conf->{username};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->conf->{"username"} 0 0 0
38 0 ▶ $payload->{icon_url} = $self->conf->{icon_url} if exists $self->conf->{icon_url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->conf->{"icon_url"} 0 0 0
39 0 ▶ $payload->{icon_emoji} = $self->conf->{icon_emoji} if exists $self->conf->{icon_emoji};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->conf->{"icon_emoji"} 0 0 0
40 0 ▶ $payload->{channel} = $self->conf->{channel} if exists $self->conf->{channel};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->conf->{"channel"} 0 0 0
41  
42 0 my $ua = LWP::UserAgent->new;
43 0 my $res = $ua->post( $self->{remote}, [ payload => encode_json($payload) ] );
44  
45 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
46 0 $context->log( error => "Notiying to Slack failed: " . $res->status_line );
47 }
48 }
49  
50 1 1;
51 __END__
↑ Summary