File Coverage

blib/lib/Plagger/Plugin/Notify/Line.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package Plagger::Plugin::Notify::Line;
2              
3 1     1   13081 use strict;
  1         2  
  1         21  
4 1     1   3 use warnings;
  1         1  
  1         19  
5 1     1   13 use 5.008_001;
  1         4  
6 1     1   766 use parent qw(Plagger::Plugin);
  1         225  
  1         4  
7             use Encode;
8             use Furl;
9              
10             our $VERSION = '0.01';
11              
12             sub register {
13             my($self, $context) = @_;
14             $context->register_hook(
15             $self,
16             'publish.entry' => \¬ify_entry,
17             );
18             }
19              
20             sub initialize {
21             my($self, $context) = @_;
22             }
23              
24             sub notify_entry {
25             my($self, $context, $args) = @_;
26              
27             my $endpoint =
28             $self->conf->{endpoint_url}
29             || 'https://notify-api.line.me/api/notify';
30              
31             my $token = $self->conf->{access_token};
32             unless ($token) {
33             $context->log(
34             error => q|You must configure your LINE Notify's | .
35             q|personal access token as "access_token".|
36             );
37             return;
38             }
39              
40             my $message = $self->templatize('notify_line.tt', $args);
41             $message = encode_utf8($message);
42              
43             my $ua = Furl->new;
44              
45             my $res = $ua->post(
46             $endpoint,
47             [ 'Authorization' => 'Bearer ' . $token ],
48             [ 'message' => $message ],
49             );
50              
51             unless ($res->is_success) {
52             $context->log(
53             error => qq|LINE Notify failed:\n| .
54             qq|status: | . $res->status_line . qq|\n| .
55             qq|body: | . $res->content
56             );
57             return;
58             }
59              
60             $context->log(info => qq|LINE Notify Succeed:\nbody: | . $res->content);
61             }
62              
63             1;
64             __END__