File Coverage

blib/lib/Plagger/Plugin/Notify/Slack.pm
Criterion Covered Total %
statement 21 38 55.2
branch 0 14 0.0
condition n/a
subroutine 7 10 70.0
pod 0 3 0.0
total 28 65 43.0


line stmt bran cond sub pod time code
1             package Plagger::Plugin::Notify::Slack;
2 1     1   443 use 5.008001;
  1         2  
  1         34  
3 1     1   4 use strict;
  1         1  
  1         22  
4 1     1   4 use warnings;
  1         7  
  1         28  
5 1     1   3 use base qw( Plagger::Plugin );
  1         2  
  1         293  
6              
7 1     1   2803 use LWP::UserAgent;
  1         44271  
  1         28  
8 1     1   562 use Encode;
  1         8137  
  1         72  
9 1     1   7 use JSON qw(encode_json);
  1         2  
  1         7  
10              
11             our $VERSION = "0.02";
12              
13             sub register {
14 0     0 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             sub initialize {
23 0     0 0   my ( $self, $context, $args ) = @_;
24              
25 0 0         $self->{remote} = $self->conf->{webhook_url} or return;
26             }
27              
28             sub publish {
29 0     0 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 0         Encode::_utf8_off($text) if Encode::is_utf8($text);
35              
36 0           my $payload = +{ text => $text };
37 0 0         $payload->{username} = $self->conf->{username} if exists $self->conf->{username};
38 0 0         $payload->{icon_url} = $self->conf->{icon_url} if exists $self->conf->{icon_url};
39 0 0         $payload->{icon_emoji} = $self->conf->{icon_emoji} if exists $self->conf->{icon_emoji};
40 0 0         $payload->{channel} = $self->conf->{channel} if exists $self->conf->{channel};
41              
42 0           my $ua = LWP::UserAgent->new;
43 0           my $res = $ua->post( $self->{remote}, [ payload => encode_json($payload) ] );
44              
45 0 0         unless ( $res->is_success ) {
46 0           $context->log( error => "Notiying to Slack failed: " . $res->status_line );
47             }
48             }
49              
50             1;
51             __END__