File Coverage

blib/lib/LINE/Notify/Simple.pm
Criterion Covered Total %
statement 57 67 85.0
branch 4 14 28.5
condition n/a
subroutine 14 14 100.0
pod 2 2 100.0
total 77 97 79.3


line stmt bran cond sub pod time code
1             package LINE::Notify::Simple;
2              
3 2     2   149910 use strict;
  2         15  
  2         65  
4 2     2   11 use warnings;
  2         4  
  2         53  
5 2     2   1396 use utf8;
  2         31  
  2         11  
6 2     2   74 use feature qw(say);
  2         4  
  2         309  
7 2     2   1016 use parent qw(Class::Accessor);
  2         704  
  2         22  
8 2     2   7188 use JSON;
  2         28160  
  2         16  
9 2     2   1626 use Encode;
  2         30906  
  2         196  
10 2     2   1065 use Encode::Guess qw(euc-jp shiftjis 7bit-jis);
  2         8574  
  2         11  
11 2     2   18109 use LWP::UserAgent;
  2         96822  
  2         111  
12 2     2   1395 use HTTP::Request::Common;
  2         4954  
  2         175  
13 2     2   1210 use LINE::Notify::Simple::Response;
  2         5  
  2         16  
14              
15             __PACKAGE__->mk_accessors(qw(access_token));
16              
17             our $LINE_NOTIFY_URL = 'https://notify-api.line.me/api/notify';
18             our $VERSION = '1.02';
19              
20             sub notify {
21              
22 1     1 1 114 my($self, $message) = @_;
23              
24 1         4 my $data = { message => $message };
25 1         3 return $self->notify_detail($data);
26             }
27              
28             sub notify_detail {
29              
30 1     1 1 3 my($self, $data) = @_;
31              
32 1         5 my %headers = (
33             'Authorization' => sprintf('Bearer %s', $self->access_token)
34             );
35              
36             # drop utf8 flag
37 1         28 $self->_drop_utf8_flag_hashref($data);
38              
39 1 50       4 if (exists $data->{imageFile}) {
40 0 0       0 my $image_file = ref($data->{imageFile}) eq "ARRAY" ? $data->{imageFile}->[0] : $data->{imageFile};
41 0 0       0 if (!-e $image_file) {
42 0         0 die "$image_file is not exists.";
43             }
44 0         0 $data->{imageFile} = [$image_file];
45 0         0 $headers{'Content-type'} = "form-data";
46             } else {
47 1         3 $headers{'Content-type'} = "application/x-www-form-urlencoded";
48             }
49              
50 1         10 my $ua = LWP::UserAgent->new;
51 1         3055 my $req = POST $LINE_NOTIFY_URL, %headers, Content => [%{$data}];
  1         9  
52 1         8857 my $res = $ua->request($req);
53              
54 1         1837042 my $rate_limit_headers = {};
55 1         18 my @names = $res->header_field_names;
56 1         210 foreach my $name (@names) {
57 15 50       31 if ($name =~ /^X\-.*/) {
58 0         0 $rate_limit_headers->{lc($name)} = $res->header($name);
59             }
60             }
61              
62 1         45 my $ref = JSON->new->decode($res->content);
63              
64 1         158 return LINE::Notify::Simple::Response->new({ rate_limit_headers => $rate_limit_headers, status => $ref->{status}, message => $ref->{message}, status_line => $res->status_line });
65             }
66              
67             sub _drop_utf8_flag_hashref {
68              
69 1     1   3 my($self, $data) = @_;
70              
71 1         2 foreach my $key (keys %{$data}) {
  1         4  
72 1         2 my $val = $data->{$key};
73 1 50       5 if (ref($val)) {
74 0         0 next;
75             }
76 1 50       5 if (utf8::is_utf8($val)) {
77 0           my $enc = guess_encoding($val);
78 0 0         my $guess = ref($enc) ? $enc->name : "UTF-8";
79 0           $data->{$key} = encode($guess, $val);
80             }
81             }
82             }
83              
84             1;
85              
86             __END__