| 1 | package Net::APNs::Extended; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use parent qw(Exporter Net::APNs::Extended::Base); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | use constant { | ||||||||||||||||||||||||||||||||
| 12 | 7 | NO_ERRORS => 0, | |||||||||||||||||||||||||||||||
| 13 | PROCESSING_ERROR => 1, | ||||||||||||||||||||||||||||||||
| 14 | MISSING_DEVICE_TOKEN => 2, | ||||||||||||||||||||||||||||||||
| 15 | MISSING_TOPIC => 3, | ||||||||||||||||||||||||||||||||
| 16 | MISSING_PAYLOAD => 4, | ||||||||||||||||||||||||||||||||
| 17 | INVALID_TOKEN_SIZE => 5, | ||||||||||||||||||||||||||||||||
| 18 | INVALID_TOPIC_SIZE => 6, | ||||||||||||||||||||||||||||||||
| 19 | INVALID_PAYLOAD_SIZE => 7, | ||||||||||||||||||||||||||||||||
| 20 | INVALID_TOKEN => 8, | ||||||||||||||||||||||||||||||||
| 21 | SHUTDOWN => 10, | ||||||||||||||||||||||||||||||||
| 22 | UNKNOWN_ERROR => 255, | ||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 7 | our @EXPORT_OK = qw{ | |||||||||||||||||||||||||||||||
| 26 | NO_ERRORS | ||||||||||||||||||||||||||||||||
| 27 | PROCESSING_ERROR | ||||||||||||||||||||||||||||||||
| 28 | MISSING_DEVICE_TOKEN | ||||||||||||||||||||||||||||||||
| 29 | MISSING_TOPIC | ||||||||||||||||||||||||||||||||
| 30 | MISSING_PAYLOAD | ||||||||||||||||||||||||||||||||
| 31 | INVALID_TOKEN_SIZE | ||||||||||||||||||||||||||||||||
| 32 | INVALID_TOPIC_SIZE | ||||||||||||||||||||||||||||||||
| 33 | INVALID_PAYLOAD_SIZE | ||||||||||||||||||||||||||||||||
| 34 | INVALID_TOKEN | ||||||||||||||||||||||||||||||||
| 35 | SHUTDOWN | ||||||||||||||||||||||||||||||||
| 36 | UNKNOWN_ERROR | ||||||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||||||
| 38 | 7 | our %EXPORT_TAGS = (constants => \@EXPORT_OK); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 7 | __PACKAGE__->mk_accessors(qw[ | |||||||||||||||||||||||||||||||
| 41 | max_payload_size | ||||||||||||||||||||||||||||||||
| 42 | command | ||||||||||||||||||||||||||||||||
| 43 | ]); | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 7 | my %default = ( | |||||||||||||||||||||||||||||||
| 46 | host_production => 'gateway.push.apple.com', | ||||||||||||||||||||||||||||||||
| 47 | host_sandbox => 'gateway.sandbox.push.apple.com', | ||||||||||||||||||||||||||||||||
| 48 | is_sandbox => 0, | ||||||||||||||||||||||||||||||||
| 49 | port => 2195, | ||||||||||||||||||||||||||||||||
| 50 | max_payload_size => 256, | ||||||||||||||||||||||||||||||||
| 51 | command => 1, | ||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | sub new { | ||||||||||||||||||||||||||||||||
| 55 | 5 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 56 | 5 | $class->SUPER::new(%default, %args); | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | sub send { | ||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my ($self, $device_token, $payload, $extra) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | croak 'Usage: $apns->send($device_token, \%payload [, \%extra ])' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $device_token and ref $payload eq "HASH"
MC/DC100%2/2
defined $device_token and ref $payload eq "HASH"
defined $device_token
ref $payload eq "HASH"
Truth table100%3/3
A and B
A= defined $device_tokenB= ref $payload eq "HASH"
| |||||||||||||||||||||||||||||||||
| 62 | unless defined $device_token && ref $payload eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | $extra ||= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$extra ||= {}
MC/DC100%1/1
$extra ||= {}
$extra
Truth table100%2/2
A
A= $extra
| |||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | $extra->{identifier} ||= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$extra->{"identifier"} ||= 0
MC/DC100%1/1
$extra->{"identifier"} ||= 0
$extra->{"identifier"}
Truth table100%2/2
A
A= $extra->{"identifier"}
| |||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | $extra->{expiry} ||= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$extra->{"expiry"} ||= 0
MC/DC100%1/1
$extra->{"expiry"} ||= 0
$extra->{"expiry"}
Truth table100%2/2
A
A= $extra->{"expiry"}
| |||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | my $data = $self->_create_send_data($device_token, $payload, $extra) || return 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_create_send_data($device_token, $payload, $extra) || (return 0)
MC/DC0%0/1
$self->_create_send_data($device_token, $payload, $extra) || (return 0)
$self->_create_send_data($device_token, $payload, $extra)
Truth table50%1/2
A
A= $self->_create_send_data($device_token, $payload, $extra)
| |||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | return $self->_send($data) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | sub send_multi { | ||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | my ($self, $datum) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
send_multi: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | croak 'Usage: $apns->send_multi(\@datum)' unless ref $datum eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 4 | my $data; | |||||||||||||||||||||||||||||||
| 76 | 4 | my $i = 0; | |||||||||||||||||||||||||||||||
| 77 | 4 | for my $stuff (@$datum) { | |||||||||||||||||||||||||||||||
| 78 | 6 | ▶ | croak 'Net::APNs::Extended: send data must be ARRAYREF' unless ref $stuff eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 5 | my ($device_token, $payload, $extra) = @$stuff; | |||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | croak 'Net::APNs::Extended: send data require $device_token and \%payload' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $device_token and ref $payload eq "HASH"
MC/DC50%1/2
defined $device_token and ref $payload eq "HASH"
defined $device_token
ref $payload eq "HASH"
Truth table66%2/3
A and B
A= defined $device_tokenB= ref $payload eq "HASH"
| |||||||||||||||||||||||||||||||||
| 81 | unless defined $device_token && ref $payload eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | $extra ||= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$extra ||= {}
MC/DC100%1/1
$extra ||= {}
$extra
Truth table100%2/2
A
A= $extra
| |||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | $extra->{identifier} ||= $i++; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$extra->{"identifier"} ||= $i++
MC/DC100%2/2
$extra->{"identifier"} ||= $i++
$extra->{"identifier"}
$i++
Truth table100%3/3
A ||= B
A= $extra->{"identifier"}B= $i++
| |||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | $extra->{expiry} ||= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$extra->{"expiry"} ||= 0
MC/DC100%1/1
$extra->{"expiry"} ||= 0
$extra->{"expiry"}
Truth table100%2/2
A
A= $extra->{"expiry"}
| |||||||||||||||||||||||||||||||||
| 85 | 4 | $data .= $self->_create_send_data($device_token, $payload, $extra); | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | return $self->_send($data) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | sub retrieve_error { | ||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
retrieve_error: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 92 | 3 | my $data = $self->_read; | |||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | return unless defined $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | if ($data eq '') { # connection closed | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 1 | $self->disconnect; | |||||||||||||||||||||||||||||||
| 97 | 1 | return $data; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 1 | my ($command, $status, $identifier) = unpack 'C C L', $data; | |||||||||||||||||||||||||||||||
| 101 | 1 | my $error = { | |||||||||||||||||||||||||||||||
| 102 | command => $command, | ||||||||||||||||||||||||||||||||
| 103 | status => $status, | ||||||||||||||||||||||||||||||||
| 104 | identifier => $identifier, | ||||||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 1 | $self->disconnect; | |||||||||||||||||||||||||||||||
| 108 | 1 | return $error; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | 7 | *retrive_error = *retrieve_error; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | sub _create_send_data { | ||||||||||||||||||||||||||||||||
| 113 | 7 | ▶ | my ($self, $device_token, $payload, $extra) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_create_send_data: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||
| 114 | 7 | my $chunk; | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 7 | ▶ | croak 'aps parameter must be HASHREF' unless ref $payload->{aps} eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | # numify | ||||||||||||||||||||||||||||||||
| 119 | 6 | ▶ | $payload->{aps}{badge} += 0 if exists $payload->{aps}{badge}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # trim alert body | ||||||||||||||||||||||||||||||||
| 122 | 6 | my $json = $self->json->encode($payload); | |||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | while (bytes::length($json) > $self->{max_payload_size}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 851 | ▶ | if (ref $payload->{aps}{alert} eq 'HASH' && exists $payload->{aps}{alert}{body}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
ref $payload->{"aps"}{"alert"} eq "HASH" and exists $payload->{"aps"}{"alert"}{"body"}
MC/DC50%1/2
ref $payload->{"aps"}{"alert"} eq "HASH" and exists $payload->{"aps"}{"alert"}{"body"}
ref $payload->{"aps"}{"alert"} eq "HASH"
exists $payload->{"aps"}{"alert"}{"body"}
Truth table66%2/3
A and B
A= ref $payload->{"aps"}{"alert"} eq "HASH"B= exists $payload->{"aps"}{"alert"}{"body"}
| |||||||||||||||||||||||||||||||||
| 125 | 429 | $payload->{aps}{alert}{body} = $self->_trim_alert_body($payload->{aps}{alert}{body}, $payload); | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | elsif (exists $payload->{aps}{alert}) { | ||||||||||||||||||||||||||||||||
| 128 | 422 | $payload->{aps}{alert} = $self->_trim_alert_body($payload->{aps}{alert}, $payload); | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||||||
| 131 | 0 | $self->_trim_alert_body(undef, $payload); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | 851 | $json = $self->json->encode($payload); | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 6 | my $command = $self->command; | |||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | if ($command == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 138 | 1 | $chunk = CORE::pack('C n/a* n/a*', $command, $device_token, $json); | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | elsif ($command == 1) { | ||||||||||||||||||||||||||||||||
| 141 | $chunk = CORE::pack('C L N n/a* n/a*', | ||||||||||||||||||||||||||||||||
| 142 | 5 | $command, $extra->{identifier}, $extra->{expiry}, $device_token, $json, | |||||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||
| 146 | 0 | croak "command($command) not support. shuled be 0 or 1"; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 6 | return $chunk; | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | sub _trim_alert_body { | ||||||||||||||||||||||||||||||||
| 153 | 851 | ▶ | my ($self, $body, $payload) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_trim_alert_body: called CC=3 SCAR=13.5
| |||||||||||||||||||||||||||||||||
| 154 | 851 | ▶ | if (!defined $body || length $body == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $body or length $body == 0
MC/DC0%0/2
not defined $body or length $body == 0
not defined $body
length $body == 0
Truth table33%1/3
not(A) or B
A= not defined $bodyB= length $body == 0
| |||||||||||||||||||||||||||||||||
| 155 | 0 | my $json = $self->json->encode($payload); | |||||||||||||||||||||||||||||||
| 156 | croak sprintf "over the payload size (current:%d > max:%d) : %s", | ||||||||||||||||||||||||||||||||
| 157 | 0 | bytes::length($json), $self->{max_payload_size}, $json; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | 851 | substr($body, -1, 1) = ''; | |||||||||||||||||||||||||||||||
| 160 | 851 | return $body; | |||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 7 | 1; | |||||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||||||