Shipment/Carrier/UPS.pm

filter: Statementstmt 89.6% 78 / 87 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.7% 21 / 26 Podpod 100% 6 / 6 totaltotal 84.1% 117 / 139 CC 11 SCAR 6.1
CC
11
Cov
84%
extract_status
3.5
extract_destination
3.0
extract_service
3.0
CRAP 1.8
« Shipment/Carrier/FedEx.pm ↑ Summary Shipment/MockUserAgent.pm »
1 package Webservice::Shipment::Carrier::UPS;
2  
3 2 ▶use Mojo::Base 'Webservice::Shipment::Carrier';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::IOLoop::Delay;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use constant DEBUG => $ENV{MOJO_SHIPMENT_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶has api_key => sub { croak 'api_key is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 1 ▶has api_url => sub { Mojo::URL->new('https://wwwcie.ups.com/ups.app/xml/Track') };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 0 ▶has carrier_description => sub { 'UPS' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 2 has template => <<'TEMPLATE';
19 % my ($self, $id) = @_;
20 <?xml version="1.0" ?>
21 <AccessRequest xml:lang='en-US'>
22 <AccessLicenseNumber><%== $self->api_key %></AccessLicenseNumber>
23 <UserId><%== $self->username %></UserId>
24 <Password><%== $self->password %></Password>
25 </AccessRequest>
26 <?xml version="1.0" ?>
27 <TrackRequest>
28 <Request>
29 <TransactionReference>
30 <CustomerContext><%== $id %></CustomerContext>
31 </TransactionReference>
32 <RequestAction>Track</RequestAction>
33 </Request>
34 <TrackingNumber><%== $id %></TrackingNumber>
35 </TrackRequest>
36 TEMPLATE
37  
38 1 ▶has validation_regex => sub { qr/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\dT]\d\d\d ?\d\d\d\d ?\d\d\d)\b/i };
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 24 ▶sub extract_destination {
Subroutine extract_destination: called CC=3 SCAR=11.0
Pod documented
41 24 my ($self, $id, $dom, $target) = @_;
42  
43 24 my %targets = (
44 postal_code => 'PostalCode',
45 state => 'StateProvinceCode',
46 city => 'City',
47 country => 'CountryCode',
48 address1 => 'AddressLine1',
49 address2 => 'AddressLine2',
50 );
51  
52 24 ▶ my $t = $targets{$target} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $targets{$target} or return 24 0 24
53 24 ▶ my $addr = $dom->at("Shipment ShipTo Address $t") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $dom->at("Shipment ShipTo Address $t") or return 12 12 24
54 12 return $addr->text;
55 }
56  
57 4 ▶sub extract_service {
Subroutine extract_service: called CC=3 SCAR=11.0
Pod documented
58 4 my ($self, $id, $dom) = @_;
59 4 ▶ my $service = $dom->at('Shipment Service Description') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $service = $dom->at("Shipment Service Description") or return 4 0 4
60 4 my $text = $service->text;
61 4 ▶ $text = "UPS $text" unless $text =~ /UPS/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /UPS/u 3 1 4
62 4 return $text;
63 }
64  
65 4 ▶sub extract_status {
Subroutine extract_status: called CC=3 SCAR=12.6
Pod documented
66 4 my ($self, $id, $dom) = @_;
67  
68 4 my $activity = $dom->find('Shipment Package Activity');
69 4 my ($status, $date, $delivered);
70  
71 4 ▶ my $to_date = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 4 my $dom = shift;
73 4 return Time::Piece->strptime(
74 $dom->at('Date')->text . ' T ' . $dom->at('Time')->text,
75 '%Y%m%d T %H%M%S'
76 );
77 4 };
78  
79 4 ▶ my $d = $activity->first(sub{ $_->at('Status StatusType Code')->text eq 'D' });
Subroutine __ANON__: called CC=1 SCAR=0.0
80 4 ▶ if ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) { } 4 0 4
81 4 $status = $d->at('Status StatusType Description')->text;
82 4 $date = $d->$to_date();
83 4 $delivered = 1;
84 } else {
85 my $current = $activity
86 0 ▶ ->map(sub{ [$_, $_->$to_date()] })
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 ▶ ->sort(sub{ $b->[1] <=> $a->[1] })
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ->[0];
89  
90 0 $status = $current->[0]->at('Status StatusType Description');
91 0 ▶ $status = $status ? $status->text : '';
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
92 0 $date = $current->[1];
93 }
94  
95 4 return($status, $date, $delivered);
96 }
97  
98 4 ▶sub extract_weight {
Subroutine extract_weight: called CC=2 SCAR=7.1
Pod documented
99 4 my ($self, $id, $dom) = @_;
100  
101 4 ▶ my $weight = $dom->at('Shipment ShipmentWeight') or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $weight = $dom->at("Shipment ShipmentWeight") or return "" 4 0 4
102 4 return $weight->at('Weight')->text . ' ' . $weight->at('UnitOfMeasurement Code')->text;
103 }
104  
105 5 ▶sub human_url {
Subroutine human_url: called CC=1 SCAR=0.0
Pod documented
106 5 my ($self, $id, $dom) = @_;
107 5 return Mojo::URL->new('http://wwwapps.ups.com/WebTracking/track')->query(trackNums => $id, 'track.x' => 'Track');
108 }
109  
110 4 ▶sub request {
Subroutine request: called CC=2 SCAR=7.0
Pod documented
111 4 my ($self, $id, $cb) = @_;
112  
113 4 my $xml = Mojo::Template->new->render($self->template, $self, $id);
114 4 warn "Request:\n$xml" if DEBUG;
115  
116 4 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 3 1 4
117 3 my $tx = $self->ua->post($self->api_url, $xml);
118 3 return _handle_response($tx);
119 }
120  
121 Mojo::IOLoop::Delay->new->steps(
122 1 ▶ sub { $self->ua->post($self->api_url, $xml, shift->begin) },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
124 1 my ($ua, $tx) = @_;
125 1 ▶ die $tx->error->{message} if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 1 1
126 1 my $dom = _handle_response($tx);
127 1 $self->$cb(undef, $dom);
128 },
129 0 ▶ )->catch(sub{ $self->$cb(pop, undef) })->wait;
Subroutine __ANON__: not called CC=1 SCAR=1.2
130 }
131  
132 4 ▶sub _handle_response {
Subroutine _handle_response: called CC=2 SCAR=7.0
133 4 my $tx = shift;
134 4 my $dom = $tx->res->dom;
135 4 warn "Response:\n$dom\n" if DEBUG;
136 4 $dom = $dom->at('TrackResponse');
137  
138 4 ▶ return undef unless $dom->at('Response ResponseStatusCode')->text; # "1" on success
Branch: 1 branch
BranchTrueFalseTotal
unless $dom->at("Response ResponseStatusCode")->text 0 4 4
139 4 return $dom;
140 }
141  
142 2 1;
143  
144 =head1 NAME
145  
146 Webservice::Shipment::Carrier::UPS - UPS handling for Webservice::Shipment
147  
148 =head1 DESCRIPTION
149  
150 Implements UPS handling for L<Webservice::Shipment>.
151 It is a subclass of L<Webservice::Shipment::Carrier> which implements all the necessary methods.
152  
153 =head1 ATTRIBUTES
154  
155 L<Webservice::Shipment::Carrier::UPS> implements all of the attributes from L<Webservice::Shipment::Carrier> and implements the following new ones
156  
157 =head2 api_key
158  
159 Your api key for the UPS API.
160 The default implementation dies if used without being specified.
161  
162 =head2 template
163  
164 The string template used with L<Mojo::Template> to format the request.
165  
« Shipment/Carrier/FedEx.pm ↑ Summary Shipment/MockUserAgent.pm »