| 1 | package Terse::Plugin::UA; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.006; use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Terse::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; use URI; use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 8 | 0 | my ($self, $t) = @_; | |||||||||||||||||||||||||||
| 9 | 0 | my $connect = $self->SUPER::new(); | |||||||||||||||||||||||||||
| 10 | 0 | $connect->ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 0}); | |||||||||||||||||||||||||||
| 11 | 0 | $connect->ua->cookie_jar({}); | |||||||||||||||||||||||||||
| 12 | 0 | $connect->context = $t; | |||||||||||||||||||||||||||
| 13 | 0 | return $connect; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 18 | 0 | $self->_request( | |||||||||||||||||||||||||||
| 19 | method => 'post', | ||||||||||||||||||||||||||||
| 20 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||
| 21 | Accept => 'application/json', | ||||||||||||||||||||||||||||
| 22 | @_ | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 0 | $self->_request( | |||||||||||||||||||||||||||
| 29 | method => 'get', | ||||||||||||||||||||||||||||
| 30 | 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8', | ||||||||||||||||||||||||||||
| 31 | Accept => 'application/json', | ||||||||||||||||||||||||||||
| 32 | @_ | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $self->request_cb(\%args) if $self->can('request_cb'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $request_parameters = delete $args{params} // {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{"params"} // {}
MC/DC0%0/1
delete $args{"params"} // {}
delete $args{"params"}
Truth table0%0/2
A
A= delete $args{"params"}
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($self->context->{session} && $self->context->session->{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->context->{"session"} and $self->context->session->{"user"}
MC/DC0%0/2
$self->context->{"session"} and $self->context->session->{"user"}
$self->context->{"session"}
$self->context->session->{"user"}
Truth table0%0/3
A and B
A= $self->context->{"session"}B= $self->context->session->{"user"}
| |||||||||||||||||||||||||||||
| 41 | 0 | $request_parameters->{userName} = $self->context->session->user->userName; | |||||||||||||||||||||||||||
| 42 | 0 | $request_parameters->{hashedUserName} = $self->context->session->user->hashedUserName; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | my $url = URI->new(delete $args{path}); | |||||||||||||||||||||||||||
| 45 | 0 | my $method = delete $args{method}; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $view = $args{view} && $self->context->view(delete $args{view}); | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'view'} && $self->context->view(delete $args{'view'})
MC/DC0%0/2
$args{'view'} && $self->context->view(delete $args{'view'})
$args{'view'}
$self->context->view(delete $args{'view'})
Truth table0%0/3
A && B
A= $args{'view'}B= $self->context->view(delete $args{'view'})
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($self->can('params_cb')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | 0 | $self->req_params = $request_parameters = $self->params_cb($request_parameters,\%args, $view); | |||||||||||||||||||||||||||
| 49 | } elsif ($view) { | ||||||||||||||||||||||||||||
| 50 | 0 | $self->req_params = $request_parameters = $view->render($self->context, $request_parameters); | |||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||
| 52 | 0 | $self->req_params = $request_parameters; | |||||||||||||||||||||||||||
| 53 | 0 | $request_parameters = $self->req_params->serialize(); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | my @args; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($method =~ m/get/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $url->query_form(%{$self->req_params}); | |||||||||||||||||||||||||||
| 58 | 0 | @args = ( | |||||||||||||||||||||||||||
| 59 | $url, | ||||||||||||||||||||||||||||
| 60 | %args, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | @args = ( | |||||||||||||||||||||||||||
| 64 | $url, | ||||||||||||||||||||||||||||
| 65 | %args, | ||||||||||||||||||||||||||||
| 66 | Content => $request_parameters | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | my $res = $self->ua->$method(@args); | |||||||||||||||||||||||||||
| 70 | 0 | $res->{uri} = $url; | |||||||||||||||||||||||||||
| 71 | 0 | $self->_parse_response($res, $view); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 75 | 0 | my ($self, $res, $view) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | unless ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | my $response = eval { $self->graft('response', $res->decoded_content) }; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | return $self->_return_error($@ ? undef : $response, $res, $res->code()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($self->can('response_cb')) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 81 | 0 | return $self->response_cb($res); | |||||||||||||||||||||||||||
| 82 | ▶ | } elsif ($view && $view->can('parse')) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$view and $view->can("parse")
MC/DC0%0/2
$view and $view->can("parse")
$view
$view->can("parse")
Truth table0%0/3
A and B
A= $viewB= $view->can("parse")
| |||||||||||||||||||||||||||||
| 83 | 0 | return $view->parse($self->context, $res); | |||||||||||||||||||||||||||
| 84 | } elsif ($res->content_type eq 'application/json') { | ||||||||||||||||||||||||||||
| 85 | 0 | my $response = $self->graft('response', $res->decoded_content); | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | return $self->_return_error('Invalid JSON content returned', $res) if !$response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | return $response | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | return $self->_return_error('Invalid UA call', $res); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _return_error { | ||||||||||||||||||||||||||
|
Subroutine
_return_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self, $error, $res, $status) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $status ||= 500; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status ||= 500
MC/DC0%0/1
$status ||= 500
$status
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($self->can('error_cb')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | return $self->error_cb($error, $res, $status); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $self->context->logError($error || 'PAYMENT API call failed', 500); | ||||||||||||||||||||||||||
|
Condition0%0/2
$error || 'PAYMENT API call failed'
MC/DC0%0/1
$error || 'PAYMENT API call failed'
$error
Truth table0%0/2
A
A= $error
| |||||||||||||||||||||||||||||
| 99 | 0 | return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||