DirectConnect.pm

filter: Statementstmt 10.2% 20 / 195 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 22.7% 5 / 22 Podpod 92.8% 13 / 14 totaltotal 10.2% 38 / 372 CC 62 SCAR 43.1
CC
62
Cov
6%
_split_data
240.0
get_data
90.0
set_data
72.0
CRAP 74.6
↑ Summary Constants.pm »
1 # FedEx::DirectConnect
2 # $Id: DirectConnect.pm,v 1.27 2004/08/09 16:07:20 jay.powers Exp $
3 # Copyright (c) 2004 Jay Powers
4 # All rights reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself
8  
9 package Business::FedEx::DirectConnect; #must be in Business/FedEx
10  
11 1 require 5.006_000;
12  
13 1 ▶use Business::FedEx::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 %FE_RE %FE_SE %FE_TT %FE_RQ
15 field_name_to_tag
16 field_tag_to_name
17 1 );
18  
19 1 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = '1.01';
23  
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 #Please register with FedEx to receive the addresses to their DirectConnect API.
28 1 ▶use constant FEDEX_API_URI => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Use Tie::IxHash so that fields are output in the order they're set,
31 # which makes reading the list of fields easier if you set them in a
32 # useful order.
33 #
34 # XXX only use IxHash when a debug flag is set?
35  
36 1 { my $have_ix_hash;
37 0 ▶sub _init_ix_hash {
Subroutine _init_ix_hash: not called CC=3 SCAR=24.8
38 0 my ($rhash, $rdata) = @_;
39  
40 0 ▶ $have_ix_hash = !!eval { local $SIG{__DIE__}; require Tie::IxHash }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have_ix_hash 0 0 0
41 if !defined $have_ix_hash;
42  
43 0 ▶ if ($have_ix_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_ix_hash) { } 0 0 0
44 0 tie %$rhash, 'Tie::IxHash', @$rdata;
45 }
46 else {
47 0 %$rhash = @$rdata;
48 }
49 } }
50  
51 1 ▶sub new {
Subroutine new: not called CC=3 SCAR=23.0
Pod documented
52 0 my $name = shift;
53 0 ▶ my $class = ref($name) || $name;
Condition0%0/3
ref $name || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $name || $name
ref $name $name
Truth table0%0/3
A || B
A= ref $nameB= $name
ABresult
1X1
011
000
54 0 my $self = {
55 uri=>FEDEX_API_URI
56 ,acc => ''
57 ,meter => ''
58 ,referer => ''
59 ,Debug=>0
60 ,sdata => {}
61 ,@_ };
62 $self->{UA} = LWP::UserAgent->new(
63 exists $self->{timeout}
64 ? (timeout => $self->{timeout})
65 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'timeout'} ? : 0 0 0
66 );
67 0 $self->{REQ} = HTTP::Request->new(POST => $self->{uri}); # Create a request
68 0 bless ($self, $class);
69 }
70  
71 0 ▶sub get_data {
Subroutine get_data: not called CC=9 SCAR=45.0
Pod documented
72 0 my $self = shift;
73 0 my @key = @_;
74  
75 0 ▶ if (!@key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@key) 0 0 0
76 0 return $self->{sdata};
77 }
78  
79 0 ▶ if (@key > 1 && !wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key > 1 and not wantarray) 0 0 0
Condition0%0/3
@key > 1 and not wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
@key > 1 and not wantarray
@key > 1 not wantarray
Truth table0%0/3
A and not(B)
A= @key > 1B= not wantarray
ABresult
0X0
100
111
80 0 croak "scalar context but asked for ", 0+@key, " items";
81 }
82  
83 0 my $r = $self->{sdata};
84 0 my @ret;
85 0 for my $key (@key) {
86 # This sucks, but the key might be present as either a number
87 # or a name (or both, there's nothing which prevents that
88 # currently).
89 #
90 # Check first as they passed it (in case it's there both ways):
91  
92 #print "key=$key\n";
93 0 ▶ if (exists $r->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{$key}) 0 0 0
94 0 push @ret, $r->{$key};
95 0 next;
96 }
97  
98 # Then as a number:
99  
100 0 my $tag = field_name_to_tag $key;
101 #print "tag=$tag\n";
102 0 ▶ if (exists $r->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{$tag}) 0 0 0
103 0 push @ret, $r->{$tag};
104 0 next;
105 }
106  
107 # Then as a name:
108  
109 0 my $name = field_tag_to_name $key;
110 #print "name=$name\n";
111 0 ▶ if (exists $r->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $r->{$name}) 0 0 0
112 0 push @ret, $r->{$name};
113 0 next;
114 }
115  
116 # It isn't there.
117  
118 0 print "undef\n";
119 0 push @ret, undef;
120 }
121  
122 0 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
123 }
124  
125 0 ▶sub set_data {
Subroutine set_data: not called CC=8 SCAR=42.8
Pod documented
126 0 my $self = shift;
127 0 $self->{UTI} = shift;
128 0 $self->{sdata} = {};
129 0 _init_ix_hash $self->{sdata}, \@_;
130 0 ▶ if (!$self->{UTI}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"UTI"}) 0 0 0
131 0 $self->set_err("Error: You must provide a valid UTI.");
132 0 return 0;
133 }
134 0 $self->{sbuf} = '0,"' . $FE_TT{$self->{UTI}}[0] . '"';
135  
136 # Supply default values where appropriate.
137  
138 0 ▶ for ([3025 => $FE_TT{$self->{UTI}}[1]]
Branch: 1 branch
BranchTrueFalseTotal
$self->{'UTI'} == 3003 ? : 0 0 0
139 , [10 => $self->{acc}]
140 , $self->{UTI} == 3003 ? () : [498 => $self->{meter}]
141 ) {
142 0 my ($key, $val) = @$_;
143 $self->{sdata}->{$key} = $val
144 0 ▶ unless exists $self->{sdata}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"sdata"}{$key} 0 0 0
145 }
146
147 0 ▶ while (my ($key, $val) = each %{$self->{sdata}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"sdata"};}) 0 0 0
148 0 $key = field_name_to_tag $key;
149  
150 # Empty value should not be sent (except for 99).
151  
152 0 ▶ next if !defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
153 0 ▶ next if $val !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /\S/ 0 0 0
154  
155 # Alphanumeric fields should not contain leading or
156 # trailing blanks.
157  
158 0 $val =~ s/^\s+//;
159 0 $val =~ s/\s+\z//;
160  
161 # %-escape as necessary
162  
163 0 $val =~ s/([%"\x00])/sprintf '%%%02X', ord $1/eg;
164  
165 # XXX The TT guide says the data needs to be ASCII only (though
166 # it didn't choke on Latin 1 chars I tried sending it), but
167 # there's no way for me to do the translation here since I don't
168 # know what char set you're using. Croak if there are non-ASCII
169 # data?
170  
171 0 $self->{sbuf} .= qq($key,"$val");
172 }
173 0 $self->{sbuf} .= '99,""';
174 0 return $self->{sbuf};
175 }
176  
177 # Send a call to FedEx
178 0 ▶sub transaction {
Subroutine transaction: not called CC=8 SCAR=42.8
Pod documented
179 0 my $self = shift;
180 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
181 0 $self->{sbuf} = shift;
182 }
183 0 ▶ if (!$self->{sbuf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sbuf"}) 0 0 0
184 0 $self->set_err("Error: You must provide data to send to FedEx.");
185 0 return 0;
186 }
187  
188 0 ▶ if ($self->_send()) { # send POST to FedEx
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_send) { } 0 0 0
189 0 $self->{rbuf} =~ s/[\x00\s]+$//g; # get rid of the extra spaces/nulls
190 0 ▶ $self->{rstring} = "Total bytes returned ". length($self->{rbuf}) if ($self->{Debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
191 0 ▶ print $self->{rstring} . "\n" if ($self->{Debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
192 0 ▶ $self->{rHash} = $self->_split_data()
Branch: 1 branch
BranchTrueFalseTotal
$self->{"rHash"} = $self->_split_data or return 0 0 0 0
193 or return 0;
194 # Check for Errors from FedEx
195 0 ▶ if (exists $self->{rHash}->{2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"rHash"}{2}) 0 0 0
196 $self->set_err("FedEx Transaction Error: " . $self->{rHash}->{2} . " " . $self->{rHash}->{3},
197 0 $self->{rHash}->{2});
198 0 return 0;
199 }
200 0 return 1;
201 } else {
202 0 return 0;
203 }
204 }
205  
206  
207 # Send POST request to FedEx API
208 0 ▶sub _send {
Subroutine _send: not called CC=5 SCAR=34.0
209 0 my $self = shift;
210 0 ▶ print "Sending ". $self->{sbuf} . "\n" if ($self->{Debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
211 $self->{REQ}->header(()
212 ,'Referer' => $self->{referer}
213 ,'User-Agent' => 'Business-FedEx-DirectConnect-'.$VERSION
214 ,'Accept' => "image/gif,image/jpeg,image/pjpeg,text/plain,text/html,*/*"
215 ,'Content-Type' => "image/gif"
216 0 ,'Content-Length' => length($self->{sbuf}));
217 0 ▶ $self->{sbuf} .= '99,""' unless ($self->{sbuf} =~ /99,\"\"$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sbuf"} =~ /99,\"\"$/ 0 0 0
218 0 $self->{REQ}->content($self->{sbuf});
219 0 ▶ print $self->{REQ}->as_string() if ($self->{Debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
220 # Pass request to the user agent and get a response back
221 0 my $res = $self->{UA}->request($self->{REQ});
222 # Check the outcome of the response
223 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
224 0 $self->{rbuf} = $res->content;
225 0 return 1;
226 } else {
227 0 $self->set_err("Request Error: " . $res->status_line);
228 0 return 0;
229 }
230 }
231  
232 # here are some functions to deal with data from FedEx
233 0 ▶sub _split_data {
Subroutine _split_data: not called CC=15 SCAR=54.8
234 0 my $self = shift;
235 0 ▶ my $sdata = shift || $self->{rbuf};
Condition0%0/3
shift() || $self->{'rbuf'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'rbuf'}
shift() $self->{'rbuf'}
Truth table0%0/3
A || B
A= shift()B= $self->{'rbuf'}
ABresult
1X1
011
000
236 0 my $count = 0;
237 0 my (%hash, @key);
238  
239 0 ▶ if (!defined $sdata) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sdata) 0 0 0
240 0 $self->set_err("No data received from FedEx server");
241 0 return;
242 }
243  
244 # There ought to be a way to signal less serious errors without
245 # choking completely, or a way for the user to tell you to notice
246 # them.
247  
248 0 my $warn = 0;
249  
250 0 ▶ while ($sdata ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdata ne "") 0 0 0
251 0 ▶ if ($sdata !~ s/^(0|[1-9]\d*(?:-\d*)*),"([^"]*)"//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sdata =~ s/^(0|[1-9]\d*(?:-\d*)*),"([^"]*)"//) 0 0 0
252 0 $self->set_err("Invalid FedEx transaction data at `$sdata'");
253 0 return;
254 }
255 0 my ($key, $val) = ($1, $2);
256  
257 0 ▶ if (exists $hash{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash{$key}) 0 0 0
258 0 $self->set_err("Duplicate key $key in FedEx transaction");
259 0 return;
260 }
261  
262 0 ▶ if ($warn && $val eq '' && $key ne '99') {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn and $val eq "" and $key ne 99) 0 0 0
Condition0%0/6
$warn and $val eq "" and $key ne 99
!ll&&!rl&&r
000
$warn and $val eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
$warn and $val eq "" and $key ne 99
$warn $val eq "" $key ne 99
Truth table0%0/4
A and B and C
A= $warnB= $val eq ""C= $key ne 99
ABCresult
0XX0
10X0
1100
1111
263 0 $self->set_err("Empty value for key $key in FedEx transaction");
264 0 return;
265 }
266  
267 0 push @key, $key;
268 0 $hash{$key} = $val;
269 }
270  
271 0 ▶ if ($warn && $key[0] ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn and $key[0] ne 0) 0 0 0
Condition0%0/3
$warn and $key[0] ne 0
!ll&&!rl&&r
000
MC/DC0%0/2
$warn and $key[0] ne 0
$warn $key[0] ne 0
Truth table0%0/3
A and B
A= $warnB= $key[0] ne 0
ABresult
0X0
100
111
272 0 $self->set_err("first element of FedEx transaction data isn't 0");
273 0 return;
274 }
275  
276 0 ▶ if ($warn && $key[-1] ne '99') {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn and $key[-1] ne 99) 0 0 0
Condition0%0/3
$warn and $key[-1] ne 99
!ll&&!rl&&r
000
MC/DC0%0/2
$warn and $key[-1] ne 99
$warn $key[-1] ne 99
Truth table0%0/3
A and B
A= $warnB= $key[-1] ne 99
ABresult
0X0
100
111
277 0 $self->set_err("last element of FedEx transaction data isn't 99");
278 0 return;
279 }
280  
281 0 ▶ if ($warn && $hash{99} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn and $hash{99} ne "") 0 0 0
Condition0%0/3
$warn and $hash{99} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$warn and $hash{99} ne ""
$warn $hash{99} ne ""
Truth table0%0/3
A and B
A= $warnB= $hash{99} ne ""
ABresult
0X0
100
111
282 0 $self->set_err("non-empty value for last element of FedEx transaction data");
283 0 return;
284 }
285  
286 # XXX validate \d+-\d+ type fields
287  
288 0 return \%hash;
289 }
290  
291  
292 # array of all the required fields
293 0 ▶sub required {
Subroutine required: not called CC=2 SCAR=17.9
Pod documented
294 0 my $self = shift;
295 0 my $uti = shift;
296 0 my @req;
297 0 foreach (@{$FE_RQ{$uti}}) {
298 0 push (@req, $FE_RE{$_});
299 }
300 0 return @req;
301 }
302  
303 #check against required fields
304 # XXX Why are you passing in the UTI instead of using $self->{UTI}?
305 0 ▶sub has_required {
Subroutine has_required: not called CC=3 SCAR=24.8
Pod documented
306 0 my $self = shift;
307 0 my $uti = shift;
308 0 my @keys = keys %{$self->{sdata}};
309 0 my (%seen, @diff);
310 0 @seen{@keys} = ();
311 # XXX 0 is in %FE_RQ but not in sdata
312 0 $seen{0} = undef;
313 0 foreach (@{$FE_RQ{$uti}}) {
314 0 ▶ push(@diff, $_) unless exists $seen{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $seen{$_} 0 0 0
315 }
316 return @diff
317 0 }
318 # print or create a label
319 0 ▶sub label {
Subroutine label: not called CC=5 SCAR=34.0
Pod documented
320 0 my $self = shift;
321 0 my $file = shift;
322 0 ▶ my $image_key = shift || 188;
Condition0%0/2
shift() || 188
l!l
00
MC/DC0%0/1
shift() || 188
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
323 0 $self->{rbinary} = $self->{rHash}->{$image_key};
324 0 ▶ $self->{rbinary} =~ s/\%([0-9A-F][0-9A-F])/chr(hex("0x$1"))/eg if ($self->{rbinary});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rbinary"} 0 0 0
325 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
326 0 ▶ open(FILE, ">$file") or die "Could not open $file:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$file" or die "Could not open ${file}:\n$!" 0 0 0
327 0 binmode(FILE);
328 0 print FILE $self->{rbinary};
329 0 close(FILE);
330 0 return 1;
331 } else {
332 0 return $self->{rbinary};
333 }
334 }
335 #look up a value
336 0 ▶sub lookup {
Subroutine lookup: not called CC=2 SCAR=17.9
Pod documented
337 0 my $self = shift;
338 0 my $code = field_name_to_tag shift;
339 0 ▶ print "Looking for " . $code . "\n" if ($self->{Debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
340 0 return $self->{rHash}->{$code};
341 }
342 # All the data from FedEx
343 0 ▶sub rbuf {
Subroutine rbuf: not called CC=2 SCAR=17.9
Pod documented
344 0 my $self = shift;
345 0 ▶ $self->{rbuf} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
346 0 return $self->{rbuf};
347 }
348 # Build a hash from the return data from FedEx
349 0 ▶sub hash_ret {
Subroutine hash_ret: not called CC=1 SCAR=6.9
Pod documented
350 0 my $self = shift;
351 0 return $self->{rHash};
352 }
353  
354 0 ▶sub errstr {
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
355 0 my $self = shift;
356 0 ▶ $self->{errstr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
357 0 return $self->{errstr};
358 }
359  
360 0 ▶sub errcode {
Subroutine errcode: not called CC=2 SCAR=17.9
Pod documented
361 0 my $self = shift;
362 0 ▶ $self->{errcode} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
363 0 return $self->{errcode};
364 }
365  
366 0 ▶sub set_err {
Subroutine set_err: not called CC=4 SCAR=30.0
Pod undocumented
367 0 my $self = shift;
368 0 ▶ my $errstr = @_ ? shift : "Unknown error";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
369 0 ▶ my $errcode = @_ ? shift : -1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
370 0 ▶ @_ and confess;
Branch: 1 branch
BranchTrueFalseTotal
@_ and confess() 0 0 0
371  
372 0 $self->errstr($errstr);
373 0 $self->errcode($errcode);
374 }
375  
376 0 ▶sub user_data {
Subroutine user_data: not called CC=4 SCAR=30.0
Pod documented
377 0 ▶ @_ == 1 || @_ == 2
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or croak("->user_data expected 0 or 1 args, got ", 0 + @_) 0 0 0
Condition0%0/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
378 or croak "->user_data expected 0 or 1 args, got ", 0+@_;
379 0 my $self = shift;
380 0 my $old = $self->{user_data};
381 0 ▶ $self->{user_data} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
382 0 return $old;
383 }
384  
385 1 1;
386 __END__
↑ Summary Constants.pm »