blib/lib/Business/UPS.pm

filter: Statementstmt 100% 94 / 94 Branchbran 91.0% 51 / 56 Conditioncond 92.3% 12 / 13 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 96.1% 174 / 181 CC 35 SCAR 28.7
CC
35
Cov
96%
UPStrack
25.1
getUPS
11.0
BEGIN
1.0
CRAP 17.6
↑ Summary
1 package Business::UPS;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our @EXPORT = qw/ getUPS UPStrack /;
13  
14 # Copyright 2003 Justin Wheeler <upsmodule@datademons.com>
15 # Copyright 1998 Mark Solomon <msolomon@seva.net> (See GNU GPL)
16 # Started 01/07/1998 Mark Solomon
17  
18 3 our $VERSION = '2.04';
19  
20 7 ▶sub getUPS {
Subroutine getUPS: called CC=11 SCAR=24.0
Pod documented
21  
22 7 warnings::warnif( 'deprecated',
23 'getUPS() is deprecated: the UPS rate quoting endpoint (qcostcgi.cgi) '
24 . 'has been retired by UPS. This function will be removed in a future '
25 . 'release. See Business::UPS documentation for alternatives.' );
26  
27 my (
28 7 $product, $origin, $dest, $weight, $country, $rate_chart, $length,
29 $width, $height, $oversized, $cod
30 ) = @_;
31  
32 7 ▶ $country ||= 'US';
Condition100%2/2
$country ||= "US"
l!l
25
MC/DC100%1/1
$country ||= "US"
$country
Truth table100%2/2
A
A= $country
Aresult
11
00
33  
34 7 my $ups_cgi = 'https://www.ups.com/using/services/rave/qcostcgi.cgi';
35 7 my $workString = "?";
36 7 $workString .= "accept_UPS_license_agreement=yes&";
37 7 $workString .= "10_action=3&";
38 7 $workString .= "13_product=" . $product . "&";
39 7 $workString .= "15_origPostal=" . $origin . "&";
40 7 $workString .= "19_destPostal=" . $dest . "&";
41 7 $workString .= "23_weight=" . $weight;
42 7 ▶ $workString .= "&22_destCountry=" . $country if $country;
Branch: 1 branch
BranchTrueFalseTotal
if $country 7 0 7
43 7 ▶ $workString .= "&25_length=" . $length if $length;
Branch: 1 branch
BranchTrueFalseTotal
if $length 1 6 7
44 7 ▶ $workString .= "&26_width=" . $width if $width;
Branch: 1 branch
BranchTrueFalseTotal
if $width 1 6 7
45 7 ▶ $workString .= "&27_height=" . $height if $height;
Branch: 1 branch
BranchTrueFalseTotal
if $height 1 6 7
46 7 ▶ $workString .= "&29_oversized=1" if $oversized;
Branch: 1 branch
BranchTrueFalseTotal
if $oversized 1 6 7
47 7 ▶ $workString .= "&47_rate_chart=" . $rate_chart if $rate_chart;
Branch: 1 branch
BranchTrueFalseTotal
if $rate_chart 1 6 7
48 7 ▶ $workString .= "&30_cod=1" if $cod;
Branch: 1 branch
BranchTrueFalseTotal
if $cod 1 6 7
49 7 $workString = "${ups_cgi}${workString}";
50  
51 7 my $lwp = LWP::UserAgent->new(
52 agent => "Business-UPS/$VERSION",
53 timeout => 30,
54 );
55 7 my $result = $lwp->get($workString);
56  
57 7 ▶ croak("Failed fetching data.") unless $result->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->is_success 1 6 7
58  
59 6 my @ret = split( '%', $result->content );
60  
61 6 ▶ if ( !$ret[5] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ret[5]) { } 1 5 6
62  
63 # Error
64 1 return ( undef, undef, $ret[1] );
65 }
66 else {
67 # Good results
68 5 my $total_shipping = $ret[10];
69 5 my $ups_zone = $ret[6];
70 5 return ( $total_shipping, $ups_zone, undef );
71 }
72 }
73  
74 15 ▶sub UPStrack {
Subroutine UPStrack: called CC=25 SCAR=32.2
Pod documented
75 15 my $tracking_number = shift;
76 15 my %retValue;
77  
78 15 ▶ $tracking_number || croak("No tracking number provided to UPStrack()");
Branch: 1 branch
BranchTrueFalseTotal
$tracking_number or croak("No tracking number provided to UPStrack()") 13 2 15
79  
80 13 my $ups_url = 'https://www.ups.com/track/api/Track/GetStatus?loc=en_US';
81 13 my $payload = encode_json( {
82 Locale => 'en_US',
83 TrackingNumber => [$tracking_number],
84 } );
85  
86 13 my $lwp = LWP::UserAgent->new(
87 agent => "Business-UPS/$VERSION",
88 timeout => 30,
89 );
90 13 my $result = $lwp->post(
91 $ups_url,
92 'Content-Type' => 'application/json',
93 Content => $payload,
94 );
95  
96 13 ▶ croak("Cannot get tracking data from UPS") unless $result->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $result->is_success 1 12 13
97  
98 12 my $json;
99 12 eval { $json = decode_json( $result->content() ) };
100 12 ▶ croak("Cannot parse JSON response from UPS: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 11 12
101  
102 11 my $details = $json->{trackDetails};
103 11 ▶ croak("No tracking details returned from UPS") unless $details && ref($details) eq 'ARRAY' && @$details;
Branch: 1 branch
BranchTrueFalseTotal
unless $details and ref $details eq "ARRAY" and @$details 2 9 11
Condition83%5/6
$details and ref $details eq "ARRAY" and @$details
!ll&&!rl&&r
119
$details and ref $details eq "ARRAY"
!ll&&!rl&&r
1010
MC/DC66%2/3
$details and ref $details eq "ARRAY" and @$details
$details ref $details eq "ARRAY" @$details
Truth table75%3/4
A and B and C
A= $detailsB= ref $details eq "ARRAY"C= @$details
ABCresult
0XX0
10X0
1100
1111
104  
105 9 my $track = $details->[0];
106  
107 9 ▶ $retValue{'Current Status'} = $track->{packageStatus} if $track->{packageStatus};
Branch: 1 branch
BranchTrueFalseTotal
if $track->{"packageStatus"} 9 0 9
108 9 ▶ $retValue{'Service Type'} = $track->{service} if $track->{service};
Branch: 1 branch
BranchTrueFalseTotal
if $track->{"service"} 3 6 9
109  
110 9 ▶ if ( my $w = $track->{weight} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $track->{"weight"}) 4 5 9
111 4 ▶ if ( $w->{weight} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"weight"}) 3 1 4
112 3 ▶ my $unit = $w->{unitOfMeasurement} || '';
Condition100%2/2
$w->{'unitOfMeasurement'} || ''
l!l
21
MC/DC100%1/1
$w->{'unitOfMeasurement'} || ''
$w->{'unitOfMeasurement'}
Truth table100%2/2
A
A= $w->{'unitOfMeasurement'}
Aresult
11
00
113 3 ▶ $retValue{'Weight'} = length($unit) ? "$w->{weight} $unit" : $w->{weight};
Branch: 1 branch
BranchTrueFalseTotal
length $unit ? : 2 1 3
114 }
115 }
116  
117 9 ▶ if ( my $addr = $track->{shipToAddress} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr = $track->{"shipToAddress"}) 4 5 9
118 4 my @parts = grep { $_ } @{$addr}{qw(city state country)};
119 4 ▶ $retValue{'Shipped To'} = join( ', ', @parts ) if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 4 0 4
120 }
121  
122 9 ▶ my $delivery_date = $track->{scheduledDeliveryDate} || $track->{deliveredDate};
Condition100%3/3
$track->{'scheduledDeliveryDate'} || $track->{'deliveredDate'}
l!l&&r!l&&!r
225
MC/DC100%2/2
$track->{'scheduledDeliveryDate'} || $track->{'deliveredDate'}
$track->{'scheduledDeliveryDate'} $track->{'deliveredDate'}
Truth table100%3/3
A || B
A= $track->{'scheduledDeliveryDate'}B= $track->{'deliveredDate'}
ABresult
1X1
011
000
123 9 ▶ $retValue{'Delivery Date'} = $delivery_date if $delivery_date;
Branch: 1 branch
BranchTrueFalseTotal
if $delivery_date 4 5 9
124 9 ▶ $retValue{'Signed By'} = $track->{receivedBy} if $track->{receivedBy};
Branch: 1 branch
BranchTrueFalseTotal
if $track->{"receivedBy"} 1 8 9
125 9 ▶ $retValue{'Location'} = $track->{leftAt} if $track->{leftAt};
Branch: 1 branch
BranchTrueFalseTotal
if $track->{"leftAt"} 1 8 9
126  
127 9 my %scanning;
128 9 my $count = 0;
129  
130 9 ▶ if ( my $activities = $track->{shipmentProgressActivities} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $activities = $track->{"shipmentProgressActivities"}) 4 5 9
131 4 for my $act (@$activities) {
132 5 $count++;
133 5 ▶ $scanning{$count}{'date'} = $act->{date} if $act->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $act->{"date"} 5 0 5
134 5 ▶ $scanning{$count}{'time'} = $act->{time} if $act->{time};
Branch: 1 branch
BranchTrueFalseTotal
if $act->{"time"} 4 1 5
135 5 ▶ $scanning{$count}{'location'} = $act->{location} if $act->{location};
Branch: 1 branch
BranchTrueFalseTotal
if $act->{"location"} 4 1 5
136 5 ▶ $scanning{$count}{'activity'} = $act->{activityScan} if $act->{activityScan};
Branch: 1 branch
BranchTrueFalseTotal
if $act->{"activityScan"} 5 0 5
137 }
138 }
139  
140 9 $retValue{'Scanning'} = \%scanning;
141 9 $retValue{'Activity Count'} = $count;
142 9 $retValue{'Notice'} = "UPS authorizes you to use UPS tracking systems solely to track shipments tendered by or for you to UPS for delivery and for no other purpose. Any other use of UPS tracking systems and information is strictly prohibited.";
143  
144 9 return %retValue;
145 }
146  
147 3 1;
148  
149 __END__
↑ Summary