line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::FCM::HTTP; |
2
|
3
|
|
|
3
|
|
291588
|
use 5.008001; |
|
3
|
|
|
|
|
25
|
|
3
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
59
|
|
4
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
145
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1985
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
105525
|
|
|
3
|
|
|
|
|
122
|
|
9
|
3
|
|
|
3
|
|
1602
|
use LWP::Protocol::https; |
|
3
|
|
|
|
|
251133
|
|
|
3
|
|
|
|
|
137
|
|
10
|
3
|
|
|
3
|
|
1394
|
use LWP::ConnCache; |
|
3
|
|
|
|
|
3688
|
|
|
3
|
|
|
|
|
110
|
|
11
|
3
|
|
|
3
|
|
1251
|
use JSON qw(encode_json); |
|
3
|
|
|
|
|
17840
|
|
|
3
|
|
|
|
|
21
|
|
12
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
13
|
3
|
|
|
|
|
22
|
new => 0, |
14
|
|
|
|
|
|
|
rw => [qw/ua api_url api_key json/], |
15
|
3
|
|
|
3
|
|
1882
|
); |
|
3
|
|
|
|
|
3124
|
|
16
|
3
|
|
|
3
|
|
316
|
use Carp qw(croak); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
149
|
|
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
3
|
|
1238
|
use WWW::FCM::HTTP::Response; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2172
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $API_URL = 'https://fcm.googleapis.com/fcm/send'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
15
|
|
|
15
|
1
|
62480
|
my $class = shift; |
24
|
15
|
100
|
100
|
|
|
152
|
my %args = $_[0] && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; |
|
1
|
|
|
|
|
4
|
|
25
|
|
|
|
|
|
|
croak 'Usage: WWW::FCM::HTTP->new({ api_key => $api_key })' |
26
|
15
|
100
|
100
|
|
|
365
|
unless (exists $args{api_key} && defined $args{api_key}); |
27
|
|
|
|
|
|
|
|
28
|
13
|
|
66
|
|
|
92
|
$args{api_url} ||= $API_URL; |
29
|
13
|
|
66
|
|
|
176
|
$args{ua} ||= LWP::UserAgent->new( |
30
|
|
|
|
|
|
|
agent => __PACKAGE__.'/'.$VERSION, |
31
|
|
|
|
|
|
|
conn_cache => LWP::ConnCache->new, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
13
|
|
|
|
|
9679
|
bless { %args }, $class; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub send { |
38
|
10
|
|
|
10
|
1
|
44407
|
my ($self, $payload) = @_; |
39
|
10
|
100
|
|
|
|
459
|
croak 'Usage: $fcm->send(\%payload)' unless ref $payload eq 'HASH'; |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
64
|
my $res = $self->ua->request($self->build_request($payload)); |
42
|
6
|
|
|
|
|
138465
|
WWW::FCM::HTTP::Response->new($res, $payload->{registration_ids}); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub build_request { |
46
|
6
|
|
|
6
|
0
|
68
|
my ($self, $payload) = @_; |
47
|
6
|
50
|
|
|
|
53
|
croak 'Usage: $gcm->build_request(\%payload)' unless ref $payload eq 'HASH'; |
48
|
|
|
|
|
|
|
|
49
|
6
|
|
|
|
|
67
|
my $req = HTTP::Request->new(POST => $self->api_url); |
50
|
6
|
|
|
|
|
1860
|
$req->header(Authorization => 'key='.$self->api_key); |
51
|
6
|
|
|
|
|
1669
|
$req->header('Content-Type' => 'application/json; charset=UTF-8'); |
52
|
6
|
|
|
|
|
571
|
$req->content(encode_json $payload); |
53
|
|
|
|
|
|
|
|
54
|
6
|
|
|
|
|
389
|
return $req; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
__END__ |