Shipment/Carrier/FedEx.pm

filter: Statementstmt 84.0% 58 / 69 Branchbran 55.5% 10 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 15 / 20 Podpod 100% 6 / 6 totaltotal 78.7% 89 / 113 CC 10 SCAR 9.7
CC
10
Cov
78%
__ANON__
6.0
extract_status
5.6
extract_destination
3.0
CRAP 2.6
« Shipment/Carrier.pm ↑ Summary Shipment/Carrier/UPS.pm »
1 package Webservice::Shipment::Carrier::FedEx;
2  
3 1 ▶use Mojo::Base 'Webservice::Shipment::Carrier';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use constant DEBUG => $ENV{MOJO_SHIPMENT_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::IOLoop::Delay;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶has api_url => sub { Mojo::URL->new('https://www.fedex.com/trackingCal/track') };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 0 ▶has carrier_description => sub { 'FedEx' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 0 ▶has validation_regex => sub { qr/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 1 ▶sub human_url {
Subroutine human_url: called CC=1 SCAR=0.0
Pod documented
18 1 my ($self, $id, $doc) = @_;
19 1 return Mojo::URL->new('https://www.fedex.com/apps/fedextrack/')->query(
20 action => 'track',
21 locale => 'en_US',
22 cntry_code => 'us',
23 language => 'english',
24 tracknumbers => $id,
25 );
26 }
27  
28 6 ▶sub extract_destination {
Subroutine extract_destination: called CC=3 SCAR=11.0
Pod documented
29 6 my ($self, $id, $doc, $target) = @_;
30  
31 6 my %targets = (
32 postal_code => 'destZip',
33 state => 'destStateCD',
34 city => 'destCity',
35 country => 'destCntryCD',
36 );
37  
38 6 ▶ my $t = $targets{$target} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $targets{$target} or return 4 2 6
39 4 ▶ my $addr = $doc->{$t} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $doc->{$t} or return 3 1 4
40 3 return $addr;
41 }
42  
43 1 ▶sub extract_service {
Subroutine extract_service: called CC=2 SCAR=7.0
Pod documented
44 1 my ($self, $id, $doc) = @_;
45 1 my $class = $doc->{serviceDesc};
46 1 ▶ my $service = $class =~ /fedex/i ? $class : 'FedEx ' . $class;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /fedex/iu ? : 1 0 1
47 1 return $service;
48 }
49  
50 1 ▶sub extract_status {
Subroutine extract_status: called CC=5 SCAR=17.2
Pod documented
51 1 my ($self, $id, $doc) = @_;
52  
53 1 my $summary = $doc->{scanEventList}[0];
54 1 ▶ return unless $summary;
Branch: 1 branch
BranchTrueFalseTotal
unless $summary 0 1 1
55  
56 1 ▶ my $delivered = $doc->{isDelivered} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$doc->{'isDelivered'} ? : 1 0 1
57  
58 1 my $desc = $doc->{statusWithDetails};
59 1 ▶ unless ($summary->{date}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($summary->{"date"}) 0 1 1
60 0 $desc = 'No information found for <a href="' . $self->human_url($id) . '">' . $id . '</a>';
61 0 return ($desc, undef, $delivered);
62 }
63  
64 1 my $timestamp = join(' ', $summary->{date}, $summary->{time});
65 1 eval{
66 1 $timestamp = Time::Piece->strptime($summary->{date} . ' T ' . $summary->{time}, '%Y-%m-%d T %H:%M:%S');
67 };
68  
69 1 ▶ $desc = $summary->{date} ? join(' ', $desc , $summary->{date}, $summary->{time}) : $desc;
Branch: 1 branch
BranchTrueFalseTotal
$summary->{'date'} ? : 1 0 1
70 1 return ($desc, $timestamp, $delivered);
71 }
72  
73 1 ▶sub extract_weight { '' }
Subroutine extract_weight: called CC=1 SCAR=0.0
Pod documented
74  
75 1 ▶sub request {
Subroutine request: called CC=2 SCAR=10.9
Pod documented
76 1 my ($self, $id, $cb) = @_;
77  
78 1 my $tx = $self->ua->build_tx(
79 POST => $self->api_url.
80 {Accept => 'application/json'},
81 form => {
82 action => 'trackpackages',
83 locale => 'en_US',
84 version => '1',
85 format => 'json',
86 data => Mojo::JSON::encode_json({
87 TrackPackagesRequest => {
88 appType => 'WTRK',
89 uniqueKey => '',
90 processingParameters => {},
91 trackingInfoList => [
92 {
93 trackNumberInfo => {
94 trackingNumber => $id,
95 trackingQualifier => '',
96 trackingCarrier => '',
97 }
98 }
99 ]
100 }
101 })
102 }
103 );
104  
105 1 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 1 0 1
106 1 $self->ua->start($tx);
107 1 return _handle_response($tx);
108 }
109  
110 Mojo::IOLoop::Delay->new->steps(
111 0 ▶ sub { $self->ua->start($tx, shift->begin) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
113 0 my ($ua, $tx) = @_;
114 0 ▶ die $tx->error->{message} if $tx->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->error 0 0 0
115 0 my $json = _handle_response($tx);
116 0 $self->$cb(undef, $json);
117 },
118 0 ▶ )->catch(sub { $self->$cb(pop, undef) })->wait;
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 }
120  
121 1 ▶sub _handle_response {
Subroutine _handle_response: called CC=1 SCAR=0.0
122 1 my $tx = shift;
123 1 my $json = $tx->res->json;
124 1 warn "Response:\n" . $tx->res->body . "\n" if DEBUG;
125 1 return $json->{TrackPackagesResponse}{packageList}[0];
126 }
127  
128 1 1;
129  
130 =head1 NAME
131  
132 Webservice::Shipment::Carrier::FedEx - FedEx handling for Webservice::Shipment
133  
134 =head1 DESCRIPTION
135  
136 Implements FedEx handling for L<Webservice::Shipment>.
137 It is a subclass of L<Webservice::Shipment::Carrier> which implements all the necessary methods.
138  
139 =head1 NOTES
140  
141 The service does not provide weight information, so C<extract_weight> will always return an empty string.
« Shipment/Carrier.pm ↑ Summary Shipment/Carrier/UPS.pm »