blib/lib/WebService/CDNetworks/Purge.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 100% 18 / 18 Conditioncond 68.7% 11 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 93.7% 15 / 16 Podpod 100% 3 / 3 totaltotal 92.3% 145 / 157 CC 16 SCAR 13.2
CC
16
Cov
95%
purgeItems
7.0
listPADs
5.0
status
5.0
CRAP 3.7
↑ Summary
1 package WebService::CDNetworks::Purge;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use 5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 WebService::CDNetworks::Purge - A client for the CDNetworks's Cache Flush Open API
11  
12 =head1 SYNOPSIS
13  
14 my $service = WebService::CDNetworks::Purge -> new(
15 'username' => 'xxxxxxxx',
16 'password' => 'yyyyyyyy',
17 );
18  
19 my $listOfPADs = $service -> listPADs();
20  
21 my $purgeStatus = $service -> purgeItems('test.example.com', ['/a.html', '/images/b.png']);
22  
23 my $updatedStatus = $service -> status($purgeStatus -> [0] -> {'pid'});
24  
25 =cut
26  
27 5 our $VERSION = '0.23'; # VERSION
28  
29 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 has 'baseURL' => (
38 is => 'ro',
39 isa => 'Str',
40 13 ▶ default => sub { 'https://openapi.us.cdnetworks.com/purge/rest' }
Subroutine __ANON__: called CC=1 SCAR=0.0
41 5 );
42  
43 has 'ua' => (
44 is => 'ro',
45 isa => 'LWP::UserAgent',
46 required => 1,
47 lazy => 1,
48 0 ▶ default => sub { LWP::UserAgent -> new() }
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 5 );
50  
51 5 has 'username' => (
52 is => 'ro',
53 isa => 'Str',
54 required => 1
55 );
56  
57 5 has 'password' => (
58 is => 'ro',
59 isa => 'Str',
60 required => 1
61 );
62  
63 has 'pathsPerCall' => (
64 is => 'rw',
65 isa => 'Int',
66 12 ▶ default => sub { return 1000; },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 5 );
68  
69  
70 =head1 METHODS
71  
72 =cut
73  
74 =head2 listPADs
75  
76 Description: get the list of domains (or PADs) handled by user
77 Parameters: none
78 Returns: an array ref with the list of domains/PADs
79  
80 =cut
81  
82 3 ▶sub listPADs {
Subroutine listPADs: called CC=5 SCAR=16.1
Pod documented
83  
84 3 my ($self) = @_;
85  
86 3 my $requestPayload = {
87 'output' => 'json',
88 'user' => $self -> username,
89 'pass' => $self -> password,
90 };
91  
92 3 my $url = $self -> baseURL . '/padList';
93 3 $url .= '?' . join '&', map { $_ . '=' . uri_escape($requestPayload -> {$_}) } keys %$requestPayload;
94  
95 3 my $ua = $self -> ua;
96 3 $ua -> timeout(10);
97 3 $ua -> env_proxy;
98  
99 3 my $response = $ua -> get($url);
100  
101 3 ▶ unless ($response -> is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 2 3
102 1 die $response -> status_line;
103 }
104  
105 2 my $json = decode_json($response -> decoded_content);
106  
107 2 ▶ unless ($json -> {'resultCode'} && $json -> {'resultCode'} == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"resultCode"} and $json->{"resultCode"} == 200) 1 1 2
Condition66%2/3
$json->{"resultCode"} and $json->{"resultCode"} == 200
!ll&&!rl&&r
011
MC/DC50%1/2
$json->{"resultCode"} and $json->{"resultCode"} == 200
$json->{"resultCode"} $json->{"resultCode"} == 200
Truth table66%2/3
A and B
A= $json->{"resultCode"}B= $json->{"resultCode"} == 200
ABresult
0X0
100
111
108 1 ▶ die 'Invalid $json -> {resultCode}: ' . ($json -> {'resultCode'} || '<undef>');
Condition50%1/2
$json->{'resultCode'} || '<undef>'
l!l
10
MC/DC0%0/1
$json->{'resultCode'} || '<undef>'
$json->{'resultCode'}
Truth table50%1/2
A
A= $json->{'resultCode'}
Aresult
11
00
109 }
110  
111 1 return $json -> {'pads'};
112  
113 }
114  
115 # Description: private method used to purge a single chunk of paths from cache
116 # Parameters: PAD/domain and an arrayref with the list of paths to purge
117 # Returns: A hash ref with the parsed JSON response from service
118  
119 5 ▶sub _purgeItems {
Subroutine _purgeItems: called CC=2 SCAR=6.9
120  
121 5 my ($self, $pad, $paths) = @_;
122  
123 5 my $requestPayload = {
124 'output' => 'json',
125 'user' => $self -> username,
126 'pass' => $self -> password,
127 'type' => 'item',
128 'pad' => $pad,
129 'path' => $paths,
130 };
131  
132 5 my $url = $self -> baseURL . '/doPurge';
133  
134 5 my $ua = $self -> ua;
135 5 $ua -> timeout(10);
136 5 $ua -> env_proxy;
137  
138 5 my $response = $ua -> post($url, $requestPayload);
139  
140 5 ▶ unless ($response -> is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 4 5
141 1 die $response -> status_line;
142 }
143  
144 4 my $json = decode_json($response -> decoded_content);
145  
146 4 return $json;
147  
148 }
149  
150 =head2 purgeItems
151  
152 Description: Purges for a certain PAD/domain a list of paths.
153 If the list is two long it is split and the service is called with each chunk of paths.
154 Parameters: PAD/domain and an arrayref with the list of paths to purge
155 Returns: An array ref with the list of responses for each pack of paths.
156  
157 =cut
158  
159 9 ▶sub purgeItems {
Subroutine purgeItems: called CC=7 SCAR=19.5
Pod documented
160  
161 9 my ($self, $pad, $paths) = @_;
162  
163 9 ▶ unless ($pad) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pad) 2 7 9
164 2 croak 'No pad given!';
165 }
166  
167 7 ▶ unless ($paths && ref($paths) && ref($paths) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($paths and ref $paths and ref $paths eq "ARRAY") 2 5 7
Condition83%5/6
$paths and ref $paths and ref $paths eq "ARRAY"
!ll&&!rl&&r
205
$paths and ref $paths
!ll&&!rl&&r
115
MC/DC66%2/3
$paths and ref $paths and ref $paths eq "ARRAY"
$paths ref $paths ref $paths eq "ARRAY"
Truth table75%3/4
A and B and C
A= $pathsB= ref $pathsC= ref $paths eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
168 2 croak 'Invalid paths given!';
169 }
170  
171 5 ▶ if (scalar (@$paths) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$paths == 0) 1 4 5
172 1 carp 'Zero paths given!';
173 1 return;
174 }
175  
176 4 my $status;
177 4 my $statuses = [];
178  
179 4 ▶ while (scalar (@$paths) > $self -> pathsPerCall) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$paths > $self->pathsPerCall) 1 4 5
180 1 my @payload = splice(@$paths, 0, $self -> pathsPerCall);
181 1 $status = $self -> _purgeItems($pad, \@payload);
182 1 push @$statuses, $status;
183 }
184  
185 4 $status = $self -> _purgeItems($pad, $paths);
186 3 push @$statuses, $status;
187  
188 3 return $statuses;
189  
190 }
191  
192 =head2 status
193  
194 Description: Gets the current status of a certain purge request
195 Parameters: the purge request id
196 Returns: A hashref with the parsed JSON response from service
197  
198 =cut
199  
200 3 ▶sub status {
Subroutine status: called CC=5 SCAR=16.1
Pod documented
201  
202 3 my ($self, $pid) = @_;
203  
204 3 my $requestPayload = {
205 'output' => 'json',
206 'user' => $self -> username,
207 'pass' => $self -> password,
208 'pid' => $pid,
209 };
210  
211 3 my $url = $self -> baseURL . '/status';
212 3 $url .= '?' . join '&', map { $_ . '=' . uri_escape($requestPayload -> {$_}) } keys %$requestPayload;
213  
214 3 my $ua = $self -> ua;
215 3 $ua -> timeout(10);
216 3 $ua -> env_proxy;
217  
218 3 my $response = $ua -> get($url);
219  
220 3 ▶ unless ($response -> is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 2 3
221 1 die $response -> status_line;
222 }
223  
224 2 my $json = decode_json($response -> decoded_content);
225  
226 2 ▶ unless ($json -> {'resultCode'} && $json -> {'resultCode'} == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"resultCode"} and $json->{"resultCode"} == 200) 1 1 2
Condition66%2/3
$json->{"resultCode"} and $json->{"resultCode"} == 200
!ll&&!rl&&r
011
MC/DC50%1/2
$json->{"resultCode"} and $json->{"resultCode"} == 200
$json->{"resultCode"} $json->{"resultCode"} == 200
Truth table66%2/3
A and B
A= $json->{"resultCode"}B= $json->{"resultCode"} == 200
ABresult
0X0
100
111
227 1 ▶ die 'Invalid $json -> {resultCode}: ' . ($json -> {'resultCode'} || '<undef>');
Condition50%1/2
$json->{'resultCode'} || '<undef>'
l!l
10
MC/DC0%0/1
$json->{'resultCode'} || '<undef>'
$json->{'resultCode'}
Truth table50%1/2
A
A= $json->{'resultCode'}
Aresult
11
00
228 }
229  
230 1 return $json;
231  
232 }
233  
234 =head1 AUTHOR
235  
236 Jean Pierre Ducassou
237  
238 =head1 LICENSE
239  
240 This program is free software; you can redistribute it and/or modify it
241 under the terms of either: the GNU General Public License as published
242 by the Free Software Foundation; or the Artistic License.
243  
244 See http://dev.perl.org/licenses/ for more information.
245  
246 =head1 NO WARRANTY
247  
248 This software is provided "as-is," without any express or implied warranty. In no event shall the author be held liable for any damages arising from the use of the software.
249  
250 =cut
251  
252 5 __PACKAGE__ -> meta -> make_immutable;
253 5 1;
↑ Summary