File Coverage

blib/lib/WWW/Google/Cloud/Messaging.pm
Criterion Covered Total %
statement 31 44 70.4
branch 2 10 20.0
condition 4 6 66.6
subroutine 10 12 83.3
pod 3 3 100.0
total 50 75 66.6


line stmt bran cond sub pod time code
1             package WWW::Google::Cloud::Messaging;
2              
3 2     2   38819 use strict;
  2         4  
  2         53  
4 2     2   10 use warnings;
  2         3  
  2         50  
5 2     2   37 use 5.008_001;
  2         11  
6              
7 2     2   10 use Carp qw(croak);
  2         3  
  2         146  
8 2     2   2012 use LWP::UserAgent;
  2         111897  
  2         63  
9 2     2   17 use HTTP::Request;
  2         4  
  2         55  
10 2     2   2027 use JSON qw(encode_json);
  2         24213  
  2         10  
11             use Class::Accessor::Lite (
12 2         14 new => 0,
13             rw => [qw/ua api_url api_key/],
14 2     2   1838 );
  2         1820  
15              
16 2     2   1299 use WWW::Google::Cloud::Messaging::Response;
  2         4  
  2         586  
17              
18             our $VERSION = '0.05';
19              
20             our $API_URL = 'https://android.googleapis.com/gcm/send';
21              
22             sub new {
23 3     3 1 4045 my ($class, %args) = @_;
24 3 100       180 croak 'Usage: WWW::Google::Cloud::Messaging->new(api_key => $api_key)' unless defined $args{api_key};
25              
26 2   66     17 $args{ua} ||= LWP::UserAgent->new(agent => __PACKAGE__.'/'.$VERSION);
27 2   66     3038 $args{api_url} ||= $API_URL;
28              
29 2         13 bless { %args }, $class;
30             }
31              
32             sub send {
33 0     0 1   my ($self, $payload) = @_;
34 0 0         croak 'Usage: $gcm->send(\%payload)' unless ref $payload eq 'HASH';
35              
36 0           my $res = $self->ua->request($self->build_request($payload));
37 0           return WWW::Google::Cloud::Messaging::Response->new($res);
38             }
39              
40             sub build_request {
41 0     0 1   my ($self, $payload) = @_;
42 0 0         croak 'Usage: $gcm->build_request(\%payload)' unless ref $payload eq 'HASH';
43              
44 0 0         if (exists $payload->{delay_while_idle}) {
45 0 0         $payload->{delay_while_idle} = $payload->{delay_while_idle} ? JSON::true : JSON::false;
46             }
47              
48 0           my $req = HTTP::Request->new(POST => $self->api_url);
49 0           $req->header(Authorization => 'key='.$self->api_key);
50 0           $req->header('Content-Type' => 'application/json; charset=UTF-8');
51 0           $req->content(encode_json $payload);
52 0           return $req;
53             }
54              
55             1;
56             __END__