File Coverage

blib/lib/Business/UPS/Tracking/Response.pm
Criterion Covered Total %
statement 21 23 91.3
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 29 31 93.5


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Response;
3             # ============================================================================
4 1     1   1367 use utf8;
  1         2  
  1         6  
5 1     1   47 use 5.0100;
  1         3  
6              
7 1     1   4 use Moose;
  1         2  
  1         9  
8              
9 1     1   5192 no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
  1         2  
  1         9  
10              
11 1     1   65 use Business::UPS::Tracking::Utils;
  1         2  
  1         58  
12 1     1   396 use Business::UPS::Tracking::Shipment::Freight;
  1         3  
  1         41  
13 1     1   568 use Business::UPS::Tracking::Shipment::SmallPackage;
  1         2  
  1         43  
14              
15 1     1   259 use XML::LibXML;
  0            
  0            
16             use DateTime;
17              
18             =encoding utf8
19              
20             =head1 NAME
21              
22             Business::UPS::Tracking::Response - A response from the UPS webservice
23              
24             =head1 SYNOPSIS
25              
26             my $response = $request->run();
27             my $shipment = $response->shipment->[0];
28             say $shipment->ScheduledDelivery;
29            
30             =head1 DESCRIPTION
31              
32             This class represents a UPS tracking response. This class glues a
33             L<Business::UPS::Tracking::Request> object and a
34             L<Business::UPS::Tracking::Shipment> object togheter. All methods and
35             accessors available in L<Business::UPS::Tracking::Shipment> can also be
36             accessed via this class.
37              
38             =head1 ACCESSORS
39              
40             =head2 request
41              
42             The request that lead to this response.
43             L<Business::UPS::Tracking::Request> object.
44              
45             =head2 xml
46              
47             Parsed xml document. L<XML::LibXML::Document> object
48              
49             =head2 shipment
50              
51             Array reference of shipments in the response (
52             L<Business::UPS::Tracking::Shipment::SmallPackage> or
53             L<Business::UPS::Tracking::Shipment::Freight> objects)
54              
55             =head2 CustomerContext
56              
57             Customer context as supplied in the request
58              
59             =cut
60              
61             has 'request' => (
62             is => 'ro',
63             required => 1,
64             isa => 'Business::UPS::Tracking::Request',
65             );
66             has 'xml' => (
67             is => 'ro',
68             required => 1,
69             coerce => 1,
70             isa => 'Business::UPS::Tracking::Type::XMLDocument',
71             );
72             has 'shipment' => (
73             is => 'rw',
74             isa => 'ArrayRef[Business::UPS::Tracking::Shipment]',
75             #lazy => 1,
76             #builder => '_build_shipment',
77             #handles => \&_handle_shipment,
78             );
79             has 'CustomerContext' => (
80             is => 'ro',
81             isa => 'Str',
82             lazy_build => 1,
83             );
84              
85             sub BUILD {
86             my ($self) = @_;
87              
88             my $xml = $self->xml;
89             my $response_status
90             = $xml->findvalue('/TrackResponse/Response/ResponseStatusCode');
91              
92             # LOGGER
93             # use Path::Class;
94             # my $filename = $self->request->TrackingNumber || $self->request->ReferenceNumber;
95             # my $file = Path::Class::File->new('t','xmlresponse',$filename); # Same thing
96             # unless (-e $file->stringify) {
97             # $xml->toFile($file->stringify,1);
98             # }
99             # LOGGER
100              
101             Business::UPS::Tracking::X::XML->throw(
102             error => '/TrackResponse/ResponseStatusCode missing',
103             xml => $xml->find('/TrackResponse/Response')->get_node(1)->toString,
104             ) unless defined $response_status;
105              
106             # Check for error
107             if ($response_status == 0) {
108             Business::UPS::Tracking::X::UPS->throw(
109             severity => $xml->findvalue('/TrackResponse/Response/Error/ErrorSeverity'),
110             code => $xml->findvalue('/TrackResponse/Response/Error/ErrorCode'),
111             message => $xml->findvalue('/TrackResponse/Response/Error/ErrorDescription'),
112             request => $self->request,
113             context => $xml->findnodes('/TrackResponse/Response/Error')->get_node(1),
114             );
115             }
116            
117             my $shipment_return = [];
118             my @shipments = $xml->findnodes('/TrackResponse/Shipment');
119            
120             foreach my $shipment_xml (@shipments) {
121             my $shipment_type = $xml->findvalue('ShipmentType/Code');
122             my $shipment_class;
123            
124             $shipment_type ||= '01';
125            
126             given ($shipment_type) {
127             when ('01') {
128             $shipment_class = 'Business::UPS::Tracking::Shipment::SmallPackage';
129             }
130             when ('02') {
131             $shipment_class = 'Business::UPS::Tracking::Shipment::Freight';
132             }
133             default {
134             Business::UPS::Tracking::X::XML->throw(
135             error => "Unknown shipment type: $shipment_type",
136             xml => $shipment_type,
137             );
138             }
139             }
140            
141             push @$shipment_return, $shipment_class->new(
142             xml => $shipment_xml,
143             );
144             }
145              
146             $self->shipment($shipment_return);
147            
148             return;
149             }
150              
151             sub _build_CustomerContext {
152             my ($self) = @_;
153            
154             return $self->xml->findvalue('/TrackResponse/Response/TransactionReference/CustomerContext')
155             }
156              
157             #sub _handle_shipment {
158             # my ($meta,$metaclass) = @_;
159             #
160             # my @classes = ($metaclass->subclasses,$metaclass);
161             #
162             # my @name;
163             # foreach my $class (@classes) {
164             # push @name, map { $_ } $class->meta->get_method_list;
165             # push @name, map { $_ } $class->meta->get_attribute_list;
166             # }
167             #
168             # my %return = map { $_ => $_ } grep { $_ !~ m/_.+/ && m/[A-Z]/ } @name;
169             # delete $return{DESTROY};
170             # delete $return{BUILD};
171             # delete $return{xml};
172             #
173             # return %return;
174             #}
175              
176             __PACKAGE__->meta->make_immutable;
177             no Moose;
178             1;