| 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 <dtikhonov@vonage.com> | ||||||||||||||||||||
| 6 | # Date: November 28, 2006 | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package Net::TrackIT; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use XML::Simple qw(XMLin); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 20 | 0 | my $class = shift; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | ▶ | bless { | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub track { | ||||||||||||||||||
|
Subroutine
track: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | my ($self, $number) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||
| 38 | 'POST', $self->URI, undef, $self->_req_xml($number), | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | 0 | my $resp = $self->_ua->request($req); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | unless ($resp->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | die "Connection error: " . $resp->status_line; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | XMLin($resp->content); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | sub _ua { | ||||||||||||||||||
|
Subroutine
_ua: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||
| 51 | 0 | my %opts; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | if ('HASH' eq ref($self->{lwp_options})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | %opts = %{$self->{lwp_options}}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | LWP::UserAgent->new( | ||||||||||||||||||||
| 58 | 0 | agent => ref($self) . '/' . $self->VERSION, | |||||||||||||||||||
| 59 | %opts, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub _req_xml { | ||||||||||||||||||
|
Subroutine
_req_xml: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 64 | 0 | my ($self, $tracking_number) = @_; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # Simple but effective way of constructing XML requests. | ||||||||||||||||||||
| 67 | 0 | my $xml=<<'XML'; | |||||||||||||||||||
| 68 | <?xml version='1.0'?> | ||||||||||||||||||||
| 69 | <ECommerce action='Request' version='1.1'> | ||||||||||||||||||||
| 70 | <Requestor> | ||||||||||||||||||||
| 71 | <ID>%s</ID> | ||||||||||||||||||||
| 72 | <Password>%s</Password> | ||||||||||||||||||||
| 73 | </Requestor> | ||||||||||||||||||||
| 74 | <Track action='Get' version='1.0'> | ||||||||||||||||||||
| 75 | <Shipment> | ||||||||||||||||||||
| 76 | <TrackingNbr>%s</TrackingNbr> | ||||||||||||||||||||
| 77 | </Shipment> | ||||||||||||||||||||
| 78 | </Track> | ||||||||||||||||||||
| 79 | </ECommerce> | ||||||||||||||||||||
| 80 | XML | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | sprintf($xml, $self->ID, $self->password, $tracking_number); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # Auto-generate accessors/mutators: | ||||||||||||||||||||
| 86 | 1 | for my $method (qw(URI ID password)) { | |||||||||||||||||||
| 87 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | ▶ | *{$method} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=15.1
| |||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||
| 91 | 0 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | $self->{$method} = shift; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | 0 | return $self->{$method}; | |||||||||||||||||||
| 95 | 3 | }; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||