blib/lib/Log/Dispatch/Slack.pm

filter: Statementstmt 90.7% 69 / 76 Branchbran 75.0% 12 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 3 totaltotal 80.3% 98 / 122 CC 11 SCAR 14.6
CC
11
Cov
85%
log_message
9.4
__ANON__
6.0
new
2.0
CRAP 4.3
↑ Summary
1 package Log::Dispatch::Slack;
2  
3 # ABSTRACT: Dispatch log events to Slack
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.0006';
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use WebService::Slack::WebApi;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Log::Dispatch::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON::XS qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use base qw( Log::Dispatch::Output );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Params::Validate qw(validate SCALAR BOOLEAN);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 Params::Validate::validation_options( allow_extra => 1 );
21  
22 0 ▶sub APPEND {0}
Subroutine APPEND: not called CC=1 SCAR=6.9
Pod undocumented
23  
24 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
25 4 my $proto = shift;
26 4 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
27  
28 4 my %p = @_;
29  
30 4 my $self = bless {}, $class;
31  
32 4 $self->_basic_init(%p);
33 4 $self->_make_handle;
34  
35 4 return $self;
36 }
37  
38 4 ▶sub _basic_init {
Subroutine _basic_init: called CC=1 SCAR=0.0
39 4 my $self = shift;
40  
41 4 $self->SUPER::_basic_init(@_);
42  
43 4 my %p = validate(
44 @_, {
45 token => { type => SCALAR },
46 channel => { type => SCALAR },
47 icon => { type => SCALAR, optional => 1 },
48 username => { type => SCALAR, optional => 1 },
49 die_on_error => { type => BOOLEAN, optional => 1, default => 1 },
50 utf8 => { type => BOOLEAN, optional => 1 },
51 }
52 );
53  
54 4 $self->{channel} = $p{channel};
55 4 $self->{token} = $p{token};
56 4 $self->{username} = $p{username};
57 4 $self->{icon} = $p{icon};
58 4 $self->{die_on_error} = $p{die_on_error};
59 4 $self->{utf8} = $p{utf8};
60  
61 4 return;
62 }
63  
64 2 ▶sub _make_handle {
Subroutine _make_handle: called CC=1 SCAR=2.0
65 2 my $self = shift;
66  
67 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 $self->{client} = WebService::Slack::WebApi->new(
69 token => $self->{token},
70 2 );
71 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
72 0 my $msg = "Error initialising Webservice::Slack::WebApi: $_";
73 0 ▶ if ($self->{die_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"die_on_error"}) 0 0 0
74 0 croak $msg;
75 }
76 0 carp $msg;
77 2 };
78 2 return;
79 }
80  
81 5 ▶sub log_message {
Subroutine log_message: called CC=9 SCAR=22.5
Pod undocumented
82 5 my $self = shift;
83 5 my %p = @_;
84  
85 my %post_params = (
86 text => $self->{utf8} ? encode_utf8($p{message}) : $p{message},
87 channel => $p{channel} || $self->{channel},
88 5 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'utf8'} ? : 1 4 5
Condition33%1/3
$p{'channel'} || $self->{'channel'}
l!l&&r!l&&!r
050
MC/DC0%0/2
$p{'channel'} || $self->{'channel'}
$p{'channel'} $self->{'channel'}
Truth table33%1/3
A || B
A= $p{'channel'}B= $self->{'channel'}
ABresult
1X1
011
000
89 5 ▶ if( $p{icon} ){
Branch: 2 branches
BranchTrueFalseTotal
if ($p{'icon'}) { } 0 5 5
elsif ($self->{'icon'}) { } 2 3 5
90 0 $post_params{icon_url} = $p{icon};
91 }elsif( $self->{icon} ){
92 2 $post_params{icon_url} = $self->{icon};
93 }
94  
95 5 ▶ if( $p{username} ){
Branch: 2 branches
BranchTrueFalseTotal
if ($p{'username'}) { } 0 5 5
elsif ($self->{'username'}) { } 2 3 5
96 0 $post_params{username} = $p{username};
97 }elsif( $self->{username} ){
98 2 $post_params{username} = $self->{username};
99 }else{
100 3 $post_params{as_user} = 1;
101 }
102  
103 5 my $response = $self->{client}->chat->post_message( %post_params );
104  
105 5 ▶ if( ! $response->{ok} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"ok"}) 2 3 5
106  
107 2 my $msg = sprintf( "Failed to send message to channel (%s): %s", $self->{channel}, $response->{error} );
108 2 ▶ if ($self->{die_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"die_on_error"}) 1 1 2
109 1 croak $msg;
110 }
111 1 carp $msg;
112 }
113  
114 4 return;
115 }
116  
117  
118 2 1;
119  
120 =head1 NAME
121  
122 Log::Dispatch::Slack
123  
124 =head1 DESCRIPTION
125  
126 Send log messages to Slack
127  
128 =head1 SYNOPSIS
129  
130 log4perl.appender.hipchat=Log::Dispatch::Slack
131 log4perl.appender.hipchat.auth_token=your-auth-token
132 log4perl.appender.hipchat.channel=channel-to-talk-to
133 log4perl.appender.hipchat.die_on_error=1
134  
135 log4perl.appender.hipchat.username=set-to-something-if-using-app-token
136  
137 =head1 COPYRIGHT
138  
139 Copyright 2016, Robin Clarke
140  
141 =head1 AUTHOR
142  
143 Robin Clarke <robin@robinclarke.net>
144  
145 =head1 CONTRIBUTORS
146  
147 Bartlomiej Fulanty <starlight@cpan.org>, Hurra.com
148  
↑ Summary