File Coverage

blib/lib/WWW/Google/Cloud/Messaging.pm
Criterion Covered Total %
statement 41 42 97.6
branch 5 8 62.5
condition 4 6 66.6
subroutine 11 11 100.0
pod 2 2 100.0
total 63 69 91.3


line stmt bran cond sub pod time code
1             package WWW::Google::Cloud::Messaging;
2              
3 3     3   277928 use strict;
  3         8  
  3         114  
4 3     3   16 use warnings;
  3         6  
  3         87  
5 3     3   69 use 5.008_001;
  3         15  
  3         257  
6              
7 3     3   17 use Carp qw(croak);
  3         7  
  3         234  
8 3     3   23147 use LWP::UserAgent;
  3         169562  
  3         107  
9 3     3   55 use HTTP::Request;
  3         8  
  3         89  
10 3     3   2535 use JSON qw(encode_json);
  3         33005  
  3         23  
11             use Class::Accessor::Lite (
12 3         21 new => 0,
13             rw => [qw/ua api_url api_key/],
14 3     3   3607 );
  3         3268  
15              
16 3     3   2144 use WWW::Google::Cloud::Messaging::Response;
  3         8  
  3         986  
17              
18             our $VERSION = '0.04';
19              
20             our $API_URL = 'https://android.googleapis.com/gcm/send';
21              
22             sub new {
23 12     12 1 50532 my ($class, %args) = @_;
24 12 100       249 croak 'Usage: WWW::Google::Cloud::Messaging->new(api_key => $api_key)' unless defined $args{api_key};
25              
26 11   66     131 $args{ua} ||= LWP::UserAgent->new(agent => __PACKAGE__.'/'.$VERSION);
27 11   66     9605 $args{api_url} ||= $API_URL;
28              
29 11         82 bless { %args }, $class;
30             }
31              
32             sub send {
33 9     9 1 80988 my ($self, $payload) = @_;
34 9 100       549 croak 'Usage: $gcm->send(\%payload)' unless ref $payload;
35              
36 5 50       29 if (exists $payload->{delay_while_idle}) {
37 0 0       0 $payload->{delay_while_idle} = $payload->{delay_while_idle} ? JSON::true : JSON::false;
38             }
39              
40 5         25 my $req = HTTP::Request->new(POST => $self->api_url);
41 5         1855 $req->header(Authorization => 'key='.$self->api_key);
42 5         1025 $req->header('Content-Type' => 'application/json; charset=UTF-8');
43 5         492 $req->content(encode_json $payload);
44              
45 5         156 my $res = $self->ua->request($req);
46 5         234619 return WWW::Google::Cloud::Messaging::Response->new($res);
47             }
48              
49             1;
50             __END__