File Coverage

blib/lib/Net/TrackIT.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # $Id: TrackIT.pm,v 1.1.1.1 2006/11/28 22:09:18 dtikhonov Exp $
2             #
3             # This module is an interface to DHL's TrackIT interface.
4             #
5             # Author: Dmitri Tikhonov
6             # Date: November 28, 2006
7              
8             package Net::TrackIT;
9              
10 1     1   26608 use strict;
  1         2  
  1         42  
11 1     1   5 use warnings;
  1         2  
  1         50  
12              
13             our $VERSION = '0.01';
14              
15 1     1   1061 use HTTP::Request;
  1         67769  
  1         37  
16 1     1   1044 use LWP::UserAgent;
  1         66738  
  1         118  
17 1     1   634 use XML::Simple qw(XMLin);
  0            
  0            
18              
19             sub new {
20             my $class = shift;
21              
22             bless {
23             # This can be overridden, obviously.
24             URI => 'https://ecommerce.airborne.com/APILanding.asp',
25              
26             # You can use this to test before getting access to the
27             # production tracking database.
28             # URI => 'https://ecommerce.airborne.com/APILandingTest.asp',
29              
30             @_,
31             }, ref($class) || $class;
32             }
33              
34             sub track {
35             my ($self, $number) = @_;
36              
37             my $req = HTTP::Request->new(
38             'POST', $self->URI, undef, $self->_req_xml($number),
39             );
40             my $resp = $self->_ua->request($req);
41              
42             unless ($resp->is_success) {
43             die "Connection error: " . $resp->status_line;
44             }
45              
46             XMLin($resp->content);
47             }
48              
49             sub _ua {
50             my $self = shift;
51             my %opts;
52            
53             if ('HASH' eq ref($self->{lwp_options})) {
54             %opts = %{$self->{lwp_options}};
55             }
56              
57             LWP::UserAgent->new(
58             agent => ref($self) . '/' . $self->VERSION,
59             %opts,
60             );
61             }
62              
63             sub _req_xml {
64             my ($self, $tracking_number) = @_;
65              
66             # Simple but effective way of constructing XML requests.
67             my $xml=<<'XML';
68            
69            
70            
71             %s
72             %s
73            
74            
75            
76             %s
77            
78            
79            
80             XML
81              
82             sprintf($xml, $self->ID, $self->password, $tracking_number);
83             }
84              
85             # Auto-generate accessors/mutators:
86             for my $method (qw(URI ID password)) {
87             no strict 'refs';
88              
89             *{$method} = sub {
90             my $self = shift;
91             if (@_) {
92             $self->{$method} = shift;
93             }
94             return $self->{$method};
95             };
96             }
97              
98             1;
99              
100             __END__