blib/lib/WWW/Weebly.pm

filter: Statementstmt 23.9% 46 / 192 Branchbran 7.8% 5 / 64 Conditioncond 10.3% 3 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 32.5% 13 / 40 Podpod 100% 14 / 14 totaltotal 22.7% 81 / 356 CC 49 SCAR 30.1
CC
49
Cov
19%
_make_request
72.0
_do_request
42.0
_sanitize_params
42.0
CRAP 20.2
↑ Summary
1 package WWW::Weebly;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all', NONFATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 WWW::Weebly - Perl interface to interact with the Weebly API.
19  
20 =head1 VERSION
21  
22 Version 0.03
23  
24 =cut
25  
26 3 our $VERSION = '0.03';
27  
28 =head1 SYNOPSIS
29  
30 This module provides you with an perl interface to interact with the Weebly API.
31  
32 use WWW::Weebly;
33 my $api = WWW::Weebly->new(
34 {
35 'tid_seed' => $seed_value_for_generating_tids,
36 'weebly_secret' => $weebly_secret,
37 'weebly_url' => $weebly_baseurl,
38 }
39 );
40 $api->new_user();
41 $api->login($params_for_login);
42 $api->enable_account($params_for_enable_account);
43 $api->disable_account($params_for_disable_account);
44 $api->delete_account($params_for_delete_account);
45 $api->undelete_account($params_for_undelete_account);
46 $api->upgrade_account($params_for_upgrade_account);
47 $api->downgrade_account($params_for_downgrade_account);
48 $api->has_service($params_for_has_service);
49  
50 =cut
51  
52 =head1 SUBROUTINES/METHODS
53  
54 =head2 new()
55  
56 B<Croaks> on errors.
57  
58 B<Input> takes a hashref that contains:
59  
60 weebly_secret => The secret key that is used to generate auth tokens.
61 weebly_url => Specify the base URL that we should be querying.
62  
63 One of the following:
64  
65 tid_seed => The value that will be appended to the current time() value to generate a transaction id.
66 tid_sub => If you wish to generate transaction ids in a different way, you can pass the coderef to the sub you wish to use here.
67  
68 Optional:
69 http_opts => Hashref of options that are passed on to the HTTP::Tiny object that is used internally.
70 Example value: { 'agent' => 'WWW-Weebly', 'timeout' => 20, 'verify_SSL' => 'false', 'SSL_options' => {'SSL_verify_mode' => '0x00'} }
71  
72 =cut
73  
74 1 ▶sub new {
Subroutine new: called CC=9 SCAR=28.3
Pod documented
75  
76 1 my ( $class, $opts ) = @_;
77  
78 1 my $self = {};
79 1 bless $self, $class;
80  
81 $self->{ weebly_secret } = $opts->{ weebly_secret }
82 1 ▶ or croak ( 'weebly_secret is a required parameter' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"weebly_secret"} = $opts->{"weebly_secret"} or croak("weebly_secret is a required parameter") 1 0 1
83  
84 # Configure the proper tid_sub for transaction_id generation
85 1 ▶ if ( defined $opts->{ tid_sub } and ref $opts->{ tid_sub } eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts->{'tid_sub'} and ref $opts->{'tid_sub'} eq 'CODE') { } 0 1 1
elsif (defined $opts->{'tid_seed'} and not ref $opts->{'tid_seed'}) { } 1 0 1
Condition33%1/3
defined $opts->{"tid_sub"} and ref $opts->{"tid_sub"} eq "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $opts->{"tid_sub"} and ref $opts->{"tid_sub"} eq "CODE"
defined $opts->{"tid_sub"} ref $opts->{"tid_sub"} eq "CODE"
Truth table33%1/3
A and B
A= defined $opts->{"tid_sub"}B= ref $opts->{"tid_sub"} eq "CODE"
ABresult
0X0
100
111
86 0 $self->{ tid_sub } = $opts->{ tid_sub };
87 ▶ } elsif ( defined $opts->{ tid_seed } and not ref $opts->{ tid_seed } ) {
Condition33%1/3
defined $opts->{"tid_seed"} and not ref $opts->{"tid_seed"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts->{"tid_seed"} and not ref $opts->{"tid_seed"}
defined $opts->{"tid_seed"} not ref $opts->{"tid_seed"}
Truth table33%1/3
A and not(B)
A= defined $opts->{"tid_seed"}B= not ref $opts->{"tid_seed"}
ABresult
0X0
100
111
88 0 ▶ $self->{ tid_sub } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 require Time::HiRes;
90 0 Time::HiRes->import(qw|gettimeofday|);
91 0 return md5_hex $opts->{ tid_seed }, gettimeofday();
92 1 };
93 } else {
94 0 croak ( 'Must specifiy either a coderef for tid_sub, or a value for tid_seed' );
95 }
96  
97 # Configure weebly_url
98 1 ▶ if ( defined $opts->{ weebly_url } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'weebly_url'}) { } 1 0 1
99 1 $self->{ weebly_url } = $opts->{ weebly_url };
100 } else {
101 0 croak ( 'Must specify which URL to query in weebly_url' );
102 }
103  
104 # Initiate the UA objects for the queries
105 1 my $http_opts = $opts->{ http_opts };
106 1 ▶ if (not (exists $http_opts->{agent} and defined $http_opts->{agent}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $http_opts->{"agent"} and defined $http_opts->{"agent"}) 0 1 1
Condition33%1/3
exists $http_opts->{"agent"} and defined $http_opts->{"agent"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $http_opts->{"agent"} and defined $http_opts->{"agent"}
exists $http_opts->{"agent"} defined $http_opts->{"agent"}
Truth table33%1/3
A and B
A= exists $http_opts->{"agent"}B= defined $http_opts->{"agent"}
ABresult
0X0
100
111
107 0 $http_opts->{agent} = 'WWW-Weebly'.$VERSION;
108 }
109 1 $self->{ _ua } = HTTP::Tiny->new ( %{ $http_opts } );
110  
111 1 return $self;
112 }
113  
114 =head2 new_user()
115  
116 Dispatches a newuser call to the URL specified in $self->{weebly_url}.
117  
118 B<Croaks> on errors.
119  
120 B<Input>: None.
121  
122 B<Returns> a hashref with the following keys otherwise:
123  
124 success => 1 or 0.
125 new_id => ID associated with the new account (only present on success).
126 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
127  
128 =cut
129  
130 0 ▶sub new_user {
Subroutine new_user: not called CC=1 SCAR=6.9
Pod documented
131 0 return shift->_do_request ( 'newuser' );
132 }
133  
134 =head2 login()
135  
136 Dispatches a login call to the URL specified in $self->{weebly_url}.
137  
138 Passes critical user account information to Weebly, such as the FTP info, account type (Basic/Premium), widget type, etc.
139  
140 Generates a one-time use login url that allows the client to log-in to the Weebly editor.
141  
142 B<Croaks> on errors.
143  
144 B<Input>: hashref that contains the following information:
145  
146 user_id => the user_id of the account.
147 ftp_url => FTP URL
148 ftp_username => FTP username
149 ftp_password => FTP password
150 ftp_path => FTP publish path
151 property_name => Property name used for the creating the website's foooter. Should be of the form: <a href='URL' target='_blank'>SITE_NAME</a>
152 upgrade_url => URL of the purchase manager
153 publish_domain => Published site's FQDN (ie www.domain.com)
154 platform => 'Windows' or 'Unix'
155 publish_upsell => Publish upsell URL (optional, placed in an 640px wide by 200px tall iframe on publish)
156  
157 B<Returns> a hashref with the following keys otherwise:
158  
159 success => 1 or 0.
160 login_url => one-time login url for the account.
161 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
162  
163 =cut
164  
165 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
166 0 my ( $self, $params ) = @_;
167 0 ▶ $self->_sanitize_params ( 'login', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("login", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
168 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
169 0 my $output = $self->_do_request ( 'login', $params );
170 0 ▶ if ( $output->{ success } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"success"}) 0 0 0
171 0 $output->{ login_url } = $self->get_weebly_url () . '/weebly/login.php?t=' . delete $output->{ token };
172 }
173 0 return $output;
174 }
175  
176 =head2 enable_account()
177  
178 Dispatches a enableaccount call to the URL specified in $self->{weebly_url}.
179  
180 This call enables a user account that has been previously disabled.
181  
182 B<Croaks> on errors.
183  
184 B<Input>: hashref that contains the following information:
185  
186 user_id => the user_id of the account.
187  
188 B<Returns> a hashref with the following keys:
189  
190 success => 1 or 0.
191 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
192  
193 =cut
194  
195 0 ▶sub enable_account {
Subroutine enable_account: not called CC=2 SCAR=17.9
Pod documented
196 0 my ( $self, $params ) = @_;
197 0 ▶ $self->_sanitize_params ( 'enableaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("enableaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
198 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
199 0 return $self->_do_request ( 'enableaccount', $params );
200 }
201  
202 =head2 disable_account()
203  
204 Dispatches a disableaccount call to the URL specified in $self->{weebly_url}.
205  
206 This call disables login to a user account. This account will be accounted for in user quota numbers.
207  
208 It is possible to restore login capabilities to this account using enable_account().
209  
210 B<Croaks> on errors.
211  
212 B<Input>: hashref that contains the following information:
213  
214 user_id => the user_id of the account.
215  
216 B<Returns> a hashref with the following keys:
217  
218 success => 1 or 0.
219 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
220  
221 =cut
222  
223 0 ▶sub disable_account {
Subroutine disable_account: not called CC=2 SCAR=17.9
Pod documented
224 0 my ( $self, $params ) = @_;
225 0 ▶ $self->_sanitize_params ( 'disableaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("disableaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
226 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
227 0 return $self->_do_request ( 'disableaccount', $params );
228 }
229  
230 =head2 delete_account()
231  
232 Dispatches a deleteaccount call to the URL specified in $self->{weebly_url}.
233  
234 This call deletes a user account. This account will not be accounted for in user quota numbers.
235  
236 It is possible to restore this account using the Admin interface or via the undelete_account() call.
237  
238 B<Croaks> on errors.
239  
240 B<Input>: hashref that contains the following information:
241  
242 user_id => the user_id of the account.
243  
244 B<Returns> a hashref with the following keys:
245  
246 success => 1 or 0.
247 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
248  
249 =cut
250  
251 0 ▶sub delete_account {
Subroutine delete_account: not called CC=2 SCAR=17.9
Pod documented
252 0 my ( $self, $params ) = @_;
253 0 ▶ $self->_sanitize_params ( 'deleteaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("deleteaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
254 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
255 0 return $self->_do_request ( 'deleteaccount', $params );
256 }
257  
258 =head2 undelete_account()
259  
260 Dispatches a undeleteaccount call to the URL specified in $self->{weebly_url}.
261  
262 This call restores a deleted user account.
263  
264 B<Croaks> on errors.
265  
266 B<Input>: hashref that contains the following information:
267  
268 user_id => the user_id of the account.
269  
270 B<Returns> a hashref with the following keys:
271  
272 success => 1 or 0.
273 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
274  
275 =cut
276  
277 0 ▶sub undelete_account {
Subroutine undelete_account: not called CC=2 SCAR=17.9
Pod documented
278 0 my ( $self, $params ) = @_;
279 0 ▶ $self->_sanitize_params ( 'undeleteaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("undeleteaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
280 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
281 0 return $self->_do_request ( 'undeleteaccount', $params );
282 }
283  
284 =head2 upgrade_account()
285  
286 Dispatches a upgradeaccount call to the URL specified in $self->{weebly_url}.
287  
288 This call upgrades a user account with a given service.
289  
290 B<Croaks> on errors.
291  
292 B<Input>: hashref that contains the following information:
293  
294 user_id => the user_id of the account.
295 service_id => the server_id to be added to the account.
296 Current service_ids that Weebly responds are: 'Weebly.proAccount', 'Weebly.eCommerce'
297 term => duration of the service in months.
298 price => price paid for the service.
299  
300 B<Returns> a hashref with the following keys:
301  
302 success => 1 or 0.
303 reason => If the request fails, this will contain a text explanation of the failure as returned by Weebly.
304  
305 =cut
306  
307 0 ▶sub upgrade_account {
Subroutine upgrade_account: not called CC=2 SCAR=17.9
Pod documented
308 0 my ( $self, $params ) = @_;
309 0 ▶ $self->_sanitize_params ( 'upgradeaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("upgradeaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
310 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
311 0 return $self->_do_request ( 'upgradeaccount', $params );
312 }
313  
314 =head2 downgrade_account()
315  
316 Dispatches a downgradeaccount call to the URL specified in $self->{weebly_url}.
317  
318 This call can be used to check if the specified user_id has Pro or Ecommerce features enabled.
319  
320 B<Croaks> on errors.
321  
322 B<Input>: hashref that contains the following information:
323  
324 user_id => the user_id of the account.
325 service_id => the server_id to be removed from the account.
326 Current service_ids that Weebly responds are: 'Weebly.proAccount', 'Weebly.eCommerce'
327  
328 B<Returns> a hashref with the following key:
329  
330 success => 1 or 0.
331  
332 =cut
333  
334 0 ▶sub downgrade_account {
Subroutine downgrade_account: not called CC=2 SCAR=17.9
Pod documented
335 0 my ( $self, $params ) = @_;
336 0 ▶ $self->_sanitize_params ( 'downgradeaccount', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("downgradeaccount", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
337 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
338 0 return $self->_do_request ( 'downgradeaccount', $params );
339 }
340  
341 =head2 has_service()
342  
343 Dispatches a hasservice call to the URL specified in $self->{weebly_url}.
344  
345 This call can be used to check if the specified user_id has Pro or Ecommerce features enabled.
346  
347 B<Croaks> on errors.
348  
349 B<Input>: hashref that contains the following information:
350 user_id => the user_id of the account.
351 service_id => the server_id to check.
352 Current service_ids that Weebly responds are: 'Weebly.proAccount', 'Weebly.eCommerce'
353  
354 B<Returns> a hashref with the following key:
355  
356 success => 1 or 0.
357  
358 =cut
359  
360 0 ▶sub has_service {
Subroutine has_service: not called CC=2 SCAR=17.9
Pod documented
361 0 my ( $self, $params ) = @_;
362 0 ▶ $self->_sanitize_params ( 'hasservice', $params )
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanitize_params("hasservice", $params) or $self->_error('Failed to sanitize params. Error: ' . $self->get_error, 1) 0 0 0
363 or $self->_error ( qq{Failed to sanitize params. Error: }.$self->get_error, 1 );
364 0 return $self->_do_request ( 'hasservice', $params );
365 }
366  
367 =head2 get_auth_token()
368  
369 B<Input>: transaction id, action param, and userid param.
370  
371 B<Returns> the md5_hex hash of the input params concatenated with $self->{weebly_secret},
372 to be used as the auth token in the API calls.
373  
374 =cut
375  
376 0 ▶sub get_auth_token {
Subroutine get_auth_token: not called CC=1 SCAR=6.9
Pod documented
377  
378 0 my ( $self, @params ) = @_;
379 0 @params = grep { defined } @params;
380 0 return md5_hex ( $self->get_weebly_secret (), @params );
381 }
382  
383 =head2 get_weebly_url()
384  
385 B<Returns> the base url that will be queried, which is stored in $self->{weebly_url}.
386  
387 =cut
388  
389 1 ▶sub get_weebly_url { return shift->{ 'weebly_url' }; }
Subroutine get_weebly_url: called CC=1 SCAR=0.0
Pod documented
390  
391 =head2 get_weebly_secret()
392  
393 B<Returns> $self->{weebly_secret}.
394  
395 =cut
396  
397 1 ▶sub get_weebly_secret { return shift->{ 'weebly_secret' }; }
Subroutine get_weebly_secret: called CC=1 SCAR=0.0
Pod documented
398  
399 =head2 get_error()
400  
401 B<Returns> $self->{'error'}
402  
403 =cut
404  
405 0 ▶sub get_error { return shift->{ 'error' }; }
Subroutine get_error: not called CC=1 SCAR=6.9
Pod documented
406  
407 =head1 Internal Subroutines
408  
409 The following are not meant to be used directly, but are available if 'finer' control is required.
410  
411 =cut
412  
413 =head2 _do_request()
414  
415 Wraps the call to _make_request and handles error checks.
416  
417 B<INPUT>
418  
419 Takes the 'action' and sanitized paramaters hashref as input.
420  
421 B<Output>
422  
423 Returns undef on failure (sets $self->{error} with the proper error).
424 Returns a hashref with the parsed data from the API server if successful.
425  
426 =cut
427  
428 0 ▶sub _do_request {
Subroutine _do_request: not called CC=6 SCAR=37.4
429  
430 0 my $self = shift;
431 0 my $action = shift;
432 0 my $params = shift;
433  
434 0 my $userid;
435 0 ▶ my $uri = $self->_get_url ( $action )
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $self->_get_url($action) or return $self->_error('Failed to fetch URL to query. Error: ' . $self->_get_error, 1) 0 0 0
436 or return $self->_error ( 'Failed to fetch URL to query. Error: ' . $self->_get_error, 1 );
437  
438 0 ▶ if ( $params and ref $params eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq "HASH") 0 0 0
Condition0%0/3
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params eq "HASH"
$params ref $params eq "HASH"
Truth table0%0/3
A and B
A= $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
439 0 $userid = $params->{ 'user_id' };
440 0 $uri .= _stringify_params ( $params );
441 }
442  
443 0 $uri = $self->_add_auth ( $uri, $action, $userid );
444  
445 0 my ( $output, $error ) = $self->_make_request ( $uri );
446 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
447 0 return $self->_error ( 'Failed to process "' . $action . '" request. HTTP request failed: ' . $error, 1 );
448 }
449  
450 0 ▶ return $self->_error ( 'No output returned from the Weebly API. Failed fetching results from the following URI: '
Branch: 1 branch
BranchTrueFalseTotal
unless $output 0 0 0
451 . $uri, 1 )
452 if not $output;
453  
454 0 $output = _parse_output ( $output );
455 0 return $output;
456 }
457  
458 =head2 _parse_output()
459  
460 Parses the output from Weebly's API call, and returns it as a hash.
461  
462 =cut
463  
464 0 ▶sub _parse_output {
Subroutine _parse_output: not called CC=5 SCAR=34.0
465  
466 0 my @keys = split /;/, shift;
467 0 my $output;
468 0 foreach my $key ( @keys ) {
469 0 my ( $k, $v ) = split /=/, $key, 2;
470 0 ▶ if ( $k eq 'status' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq 'status') { } 0 0 0
471 0 ▶ $output->{ 'success' } = ( $v eq 'success' or $v eq 'true' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v eq 'success' || $v eq 'true' ? : 0 0 0
Condition0%0/3
$v eq "success" or $v eq "true"
l!l&&r!l&&!r
000
MC/DC0%0/2
$v eq "success" or $v eq "true"
$v eq "success" $v eq "true"
Truth table0%0/3
A or B
A= $v eq "success"B= $v eq "true"
ABresult
1X1
011
000
472 } else {
473 0 $output->{ $k } = $v;
474 }
475 }
476 0 return $output;
477 }
478  
479 =head2 _get_url()
480  
481 Depending on the action passed, it will return part of the URL that you can use along with the _stringify_params method to generate the full GET url.
482  
483 B<Input>: action param
484  
485 B<Returns>: get_weebly_url().'/weebly/api.php?action='.$action
486  
487 =cut
488  
489 0 ▶sub _get_url {
Subroutine _get_url: not called CC=1 SCAR=6.9
490  
491 0 my $self = shift;
492 0 my $action = shift;
493  
494 0 return $self->get_weebly_url () . '/weebly/api.php?action=' . $action;
495 }
496  
497 =head2 _add_auth()
498  
499 Adds the neccessary transaction id (tid) and authentication tokens to the URI
500  
501 =cut
502  
503 0 ▶sub _add_auth {
Subroutine _add_auth: not called CC=1 SCAR=6.9
504  
505 0 my ( $self, $uri, $action, $userid ) = @_;
506 0 my $tid = $self->_get_tid ();
507 0 my $auth = $self->get_auth_token ( $tid, $action, $userid );
508 0 $uri .= '&tid=' . $tid . '&auth=' . $auth;
509 0 return $uri;
510 }
511  
512 =head2 _get_tid()
513  
514 B<Input>: None.
515  
516 B<Returns> the tid value generated by the sub referenced in $self->{tid_sub}.
517  
518 If a 'tid_seed' value was specified when the object was created,
519 then the value returned is the md5_hex hash of the tid_seed value
520 concatenated with time().
521  
522 =cut
523  
524 0 ▶sub _get_tid { return shift->{ tid_sub }->(); }
Subroutine _get_tid: not called CC=1 SCAR=6.9
525  
526 =head2 _make_request()
527  
528 Makes the HTTP request.
529  
530 B<Input> The full uri to perform the HTTP request on.
531  
532 B<Output> Returns an array containing the http response, and error.
533  
534 If the HTTP request was successful, then the error is blank.
535  
536 If the HTTP request failed, then the response is blank and the error is the status line from the HTTP response.
537  
538 =cut
539  
540 0 ▶sub _make_request {
Subroutine _make_request: not called CC=8 SCAR=42.8
541  
542 0 my $self = shift;
543 0 my $uri = shift;
544  
545 0 my $res = eval {
546 0 ▶ local $SIG{ ALRM } = sub { croak 'connection timeout' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
547 0 ▶ my $timeout = $self->{ _ua }->timeout () || '30';
Condition0%0/2
$self->{'_ua'}->timeout || '30'
l!l
00
MC/DC0%0/1
$self->{'_ua'}->timeout || '30'
$self->{'_ua'}->timeout
Truth table0%0/2
A
A= $self->{'_ua'}->timeout
Aresult
11
00
548 0 alarm $timeout;
549 0 $self->{ _ua }->get ( $uri );
550 };
551 0 alarm 0;
552  
553 ## no critic (EmptyQuotes BoundaryMatching DotMatchAnything)
554 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (not $res or $WWW::Weebly::EVAL_ERROR or ref $res and $res->{'content'} =~ /^could not connect/i) { } 0 0 0
elsif ($res->{'success'}) { } 0 0 0
Condition0%0/9
not $res or $WWW::Weebly::EVAL_ERROR or ref $res and $res->{"content"} =~ /^could not connect/i
l!l&&r!l&&!r
000
not $res or $WWW::Weebly::EVAL_ERROR
l!l&&r!l&&!r
000
ref $res and $res->{"content"} =~ /^could not connect/i
!ll&&!rl&&r
000
MC/DC0%0/4
not $res or $WWW::Weebly::EVAL_ERROR or ref $res and $res->{"content"} =~ /^could not connect/i
not $res $WWW::Weebly::EVAL_ERROR ref $res $res->{"content"} =~ /^could not connect/i
Truth table0%0/5
not(A) or B or C and D
A= not $resB= $WWW::Weebly::EVAL_ERRORC= ref $resD= $res->{"content"} =~ /^could not connect/i
ABCDresult
1XXX1
01XX1
00111
000X0
00100
555 # If $res is undef, then request() failed
556 !$res
557 # or if eval_error is set, then either the timeout alarm was triggered, or some other unforeseen error was caught.
558 || $EVAL_ERROR
559 # or if the previous checks were good, and $ref is an object, then check to see if the status_line says that the connection timed out.
560 || ( ref $res && $res->{content} =~ m/^could not connect/i )
561 ) {
562 # Return 'unable to connect' or whatever the eval_error was as the error.
563 0 ▶ return ( '', $EVAL_ERROR ? $EVAL_ERROR : 'Unable to connect to server' );
Branch: 1 branch
BranchTrueFalseTotal
$WWW::Weebly::EVAL_ERROR ? : 0 0 0
564 } elsif ( $res->{success} ) {
565 # If the response is successful, then return the content.
566 0 return ( $res->{content}, '' );
567 } else {
568 # If the response was not successful, and no evaled error was caught, then return the content as the error.
569 0 return ( '', $res->{content});
570 }
571 ## use critic
572 }
573  
574 =head2 _sanitize_params()
575  
576 Sanitizes the data in the hashref passed for the action specified.
577  
578 B<Input> The 'action', and a hashref that has the data that will be sanitized.
579  
580 B<Output> Boolean value indicating success. The hash is altered in place as needed.
581  
582 =cut
583  
584 0 ▶sub _sanitize_params {
Subroutine _sanitize_params: not called CC=6 SCAR=37.4
585  
586 0 my $self = shift;
587 0 my $action = shift;
588 0 my $params = shift;
589  
590 my $required_keys = {
591 login => {
592 0 map { ( $_ => 1 ) }
593 qw(user_id ftp_url ftp_username ftp_password ftp_path property_name upgrade_url publish_domain platform publish_upsell)
594 },
595 0 upgradeaccount => { map { ( $_ => 1 ) } qw(user_id service_id term price) },
596 0 hasservice => { map { ( $_ => 1 ) } qw(user_id service_id) },
597 0 enableaccount => { map { ( $_ => 1 ) } qw(user_id) },
598 };
599 0 $required_keys->{ downgradeaccount } = $required_keys->{ hasservice };
600 0 $required_keys->{ $_ } = $required_keys->{ enableaccount } for qw(disableaccount deleteaccount undeleteaccount);
601  
602 0 ▶ if ( not exists $required_keys->{ lc $action } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$required_keys{lc $action}) 0 0 0
603 0 $self->_error ( 'Unknown action specified: ' . $action );
604 0 return;
605 }
606  
607 0 _uri_escape_values ( $params );
608 0 _remove_unwanted_keys ( $params, $required_keys->{ lc $action } );
609  
610 # remove publish_upsell from the wanted list for 'login' calls - since its an optional param.
611 0 delete $required_keys->{ login }->{ publish_upsell };
612  
613 # check the params passed with the wanted list
614 0 ▶ if ( my $check = _check_required_keys ( $params, $required_keys->{ lc $action } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $check = _check_required_keys($params, $$required_keys{lc $action})) 0 0 0
615 0 my $error;
616 0 $error .= 'Missing required parameter(s): ' . join ( ', ', @{ $check->{ 'missing_params' } } ) . '; '
617 0 ▶ if $check->{ 'missing_params' };
Branch: 1 branch
BranchTrueFalseTotal
if $check->{"missing_params"} 0 0 0
618 0 $error .= 'Blank required parameter(s): ' . join ( ', ', @{ $check->{ 'blank_params' } } ) . '; '
619 0 ▶ if $check->{ 'blank_params' };
Branch: 1 branch
BranchTrueFalseTotal
if $check->{"blank_params"} 0 0 0
620 0 $self->_error ( $error );
621 0 return;
622 }
623  
624 0 return 1;
625 }
626  
627 0 ▶sub _uri_escape_values {
Subroutine _uri_escape_values: not called CC=3 SCAR=24.8
628  
629 0 my $params = shift;
630 0 foreach my $key ( keys %{ $params } ) {
631 0 ▶ if ( first { $key eq $_ } qw(property_name upgrade_url publish_upsell price) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { $key eq $_; } , "property_name", "upgrade_url", "publish_upsell", "price")) 0 0 0
632 0 $params->{ $key } = uri_escape ( $params->{ $key } );
633 }
634 }
635 0 return;
636 }
637  
638 =head2 _stringify_params()
639  
640 Stringifies the content of a hash such that the output can be used as the URI body of a GET request.
641  
642 B<Input> A hashref containing the sanatizied parameters for an API call.
643  
644 B<Output> String with the keys and values stringified as so '&key1=value1&key2=value2'
645  
646 =cut
647  
648 0 ▶sub _stringify_params {
Subroutine _stringify_params: not called CC=2 SCAR=17.9
649  
650 0 my $params = shift;
651 0 my $url;
652 0 foreach my $key ( keys %{ $params } ) {
653 ## no critic (NoisyQuotes)
654 0 $url .= '&' . $key . '=' . $params->{ $key };
655 ## use critic
656 }
657 0 return $url;
658 }
659  
660 =head2 _check_required_keys()
661  
662 B<Input>
663  
664 First arg: Hashref that contains the data to be checked.
665 Second arg: Hashref that holds the keys to check for.
666  
667 B<Output>
668  
669 A hash containing the 'missing_params' and 'blank_params', if any are found to be missing.
670  
671 If a required key is missing, it will be present in the missing_params array.
672 If a required key has a blank value, it will be present in the blank_params array.
673  
674 Returns undef, if no issues are found.
675  
676 =cut
677  
678 0 ▶sub _check_required_keys {
Subroutine _check_required_keys: not called CC=5 SCAR=34.0
679  
680 0 my $params_ref = shift;
681 0 my $wanted_ref = shift;
682 0 my $output;
683  
684 0 foreach my $wanted_key ( keys %{ $wanted_ref } ) {
685 0 ▶ if ( not ( exists $params_ref->{ $wanted_key } and defined $params_ref->{ $wanted_key } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $params_ref->{$wanted_key} && defined $params_ref->{$wanted_key}) { } 0 0 0
elsif (not length $params_ref->{$wanted_key}) { } 0 0 0
Condition0%0/3
exists $params_ref->{$wanted_key} && defined $params_ref->{$wanted_key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params_ref->{$wanted_key} && defined $params_ref->{$wanted_key}
exists $params_ref->{$wanted_key} defined $params_ref->{$wanted_key}
Truth table0%0/3
A && B
A= exists $params_ref->{$wanted_key}B= defined $params_ref->{$wanted_key}
ABresult
0X0
100
111
686 0 push @{ $output->{ 'missing_params' } }, $wanted_key;
687 } elsif ( not length $params_ref->{ $wanted_key } ) {
688 0 push @{ $output->{ 'blank_params' } }, $wanted_key;
689 }
690 }
691  
692 0 return $output;
693 }
694  
695 =head2 _remove_unwanted_keys()
696  
697 Deletes keys from the provided params hashref, if they are not listed in the hash for wanted keys.
698  
699 B<Input>
700  
701 First arg: Hashref that contains the data to be checked.
702 Second arg: Hashref that holds the keys to check for.
703  
704 B<Output>
705  
706 undef
707  
708 =cut
709  
710 0 ▶sub _remove_unwanted_keys {
Subroutine _remove_unwanted_keys: not called CC=3 SCAR=24.8
711  
712 0 my $params_ref = shift;
713 0 my $wanted_ref = shift;
714  
715 0 foreach my $key ( keys %{ $params_ref } ) {
716 0 ▶ if ( not $wanted_ref->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wanted_ref->{$key}) 0 0 0
717 0 delete $params_ref->{ $key };
718 }
719 }
720 0 return;
721 }
722  
723 =head2 _get_error()
724  
725 Returns $self->{'error'}
726  
727 =cut
728  
729 0 ▶sub _get_error { return shift->{ 'error' }; }
Subroutine _get_error: not called CC=1 SCAR=6.9
730  
731 =head2 _error()
732  
733 Internal method that is used to report and set $self->{'error'}.
734  
735 It will croak if called with a true second argument. Such as:
736  
737 $self->_error($msg, 1);
738  
739 =cut
740  
741 0 ▶sub _error {
Subroutine _error: not called CC=2 SCAR=17.9
742  
743 0 my ( $self, $msg, $croak ) = @_;
744 0 $self->{ 'error' } = $msg;
745 0 ▶ if ( $croak ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($croak) 0 0 0
746 0 croak $msg;
747 }
748 }
749  
750 =head1 AUTHOR
751  
752 Rishwanth Yeddula, C<< <ryeddula at cpan.org> >>
753  
754 =head1 ACKNOWLEDGMENTS
755  
756 Thanks to L<Hostgator.com|http://hostgator.com/> for funding the development of this module and providing test resources.
757  
758 =head1 BUGS
759  
760 Please report any bugs or feature requests to C<bug-www-weebly at rt.cpan.org>, or through
761 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Weebly>. I will be notified, and then you'll
762 automatically be notified of progress on your bug as I make changes.
763  
764 =head1 SUPPORT
765  
766 You can find documentation for this module with the perldoc command.
767  
768 perldoc WWW::Weebly
769  
770  
771 You can also look for information at:
772  
773 =over 4
774  
775 =item * RT: CPAN's request tracker (report bugs here)
776  
777 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Weebly>
778  
779 =item * AnnoCPAN: Annotated CPAN documentation
780  
781 L<http://annocpan.org/dist/WWW-Weebly>
782  
783 =item * CPAN Ratings
784  
785 L<http://cpanratings.perl.org/d/WWW-Weebly>
786  
787 =item * Search CPAN
788  
789 L<http://search.cpan.org/dist/WWW-Weebly/>
790  
791 =back
792  
793 =head1 ACKNOWLEDGEMENTS
794  
795 =head1 LICENSE AND COPYRIGHT
796  
797 Copyright 2013 Rishwanth Yeddula.
798  
799 This program is free software; you can redistribute it and/or modify it
800 under the terms of the the Artistic License (2.0). You may obtain a
801 copy of the full license at:
802  
803 L<http://www.perlfoundation.org/artistic_license_2_0>
804  
805 Any use, modification, and distribution of the Standard or Modified
806 Versions is governed by this Artistic License. By using, modifying or
807 distributing the Package, you accept this license. Do not use, modify,
808 or distribute the Package, if you do not accept this license.
809  
810 If your Modified Version has been derived from a Modified Version made
811 by someone other than you, you are nevertheless required to ensure that
812 your Modified Version complies with the requirements of this license.
813  
814 This license does not grant you the right to use any trademark, service
815 mark, tradename, or logo of the Copyright Holder.
816  
817 This license includes the non-exclusive, worldwide, free-of-charge
818 patent license to make, have made, use, offer to sell, sell, import and
819 otherwise transfer the Package with respect to any patent claims
820 licensable by the Copyright Holder that are necessarily infringed by the
821 Package. If you institute patent litigation (including a cross-claim or
822 counterclaim) against any party alleging that the Package constitutes
823 direct or contributory patent infringement, then this Artistic License
824 to you shall terminate on the date that such litigation is filed.
825  
826 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
827 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
828 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
829 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
830 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
831 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
832 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
833 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
834  
835  
836 =cut
837  
838 3 1; # End of WWW::Weebly
↑ Summary