File Coverage

blib/lib/LINE/Notify/Simple.pm
Criterion Covered Total %
statement 59 63 93.6
branch 2 6 33.3
condition n/a
subroutine 15 15 100.0
pod 2 3 66.6
total 78 87 89.6


line stmt bran cond sub pod time code
1             package LINE::Notify::Simple;
2              
3 2     2   154856 use strict;
  2         16  
  2         69  
4 2     2   10 use warnings;
  2         4  
  2         63  
5 2     2   1379 use utf8;
  2         31  
  2         11  
6 2     2   77 use feature qw(say);
  2         5  
  2         295  
7 2     2   982 use parent qw(Class::Accessor);
  2         727  
  2         12  
8 2     2   7069 use JSON;
  2         28486  
  2         15  
9 2     2   1750 use Encode;
  2         32581  
  2         231  
10 2     2   1109 use Encode::Guess qw(euc-jp shiftjis 7bit-jis);
  2         8372  
  2         13  
11 2     2   18874 use LWP::UserAgent;
  2         97970  
  2         125  
12 2     2   36 use HTTP::Request;
  2         5  
  2         65  
13 2     2   12 use URI::Escape;
  2         4  
  2         194  
14 2     2   1396 use LINE::Notify::Simple::Response;
  2         7  
  2         14  
15              
16             __PACKAGE__->mk_accessors(qw(access_token));
17              
18             our $LINE_NOTIFY_URL = 'https://notify-api.line.me/api/notify';
19             our $VERSION = '1.01';
20              
21             sub notify {
22              
23 1     1 1 128 my($self, $message) = @_;
24              
25 1         4 my $data = { message => $message };
26 1         6 return $self->notify_detail($data);
27             }
28              
29             sub notify_detail {
30              
31 1     1 1 4 my($self, $data) = @_;
32              
33 1         7 my $headers = [
34             'Content-Type', 'application/x-www-form-urlencoded',
35             'Authorization', sprintf('Bearer %s', $self->access_token)
36             ];
37              
38 1         31 my $content = $self->make_query($data);
39              
40 1         13 my $ua = LWP::UserAgent->new;
41 1         3488 my $req = HTTP::Request->new("POST", $LINE_NOTIFY_URL, $headers, $content);
42 1         9198 my $res = $ua->request($req);
43              
44 1         1514322 my $rate_limit_headers = {};
45 1         15 my @names = $res->header_field_names;
46 1         145 foreach my $name (@names) {
47 15 50       31 if ($name =~ /^X\-.*/) {
48 0         0 $rate_limit_headers->{lc($name)} = $res->header($name);
49             }
50             }
51              
52 1         29 my $ref = JSON->new->decode($res->content);
53              
54 1         39 return LINE::Notify::Simple::Response->new({ rate_limit_headers => $rate_limit_headers, status => $ref->{status}, message => $ref->{message}, status_line => $res->status_line });
55             }
56              
57              
58             sub make_query {
59              
60 1     1 0 4 my($self, $data) = @_;
61              
62 1         2 my @pairs;
63 1         2 foreach my $key(keys %{$data}) {
  1         5  
64              
65 1         2 my $val = $data->{$key};
66 1 50       5 if (utf8::is_utf8($val)) {
67 0         0 my $enc = guess_encoding($val);
68 0 0       0 my $guess = ref($enc) ? $enc->name : "UTF-8";
69 0         0 $val = Encode::encode($guess, $val);
70             }
71 1         10 push @pairs, sprintf("%s=%s", $key, uri_escape($val));
72             }
73 1         53 return join("&", @pairs);
74             }
75              
76             1;
77              
78             __END__