| 1 | # ============================================================================ | ||||||||||||
| 2 | package Business::UPS::Tracking::Response; | ||||||||||||
| 3 | # ============================================================================ | ||||||||||||
| 4 | 5 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use 5.0100; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 5 | ▶ | no if $] >= 5.017004, warnings => qw(experimental::smartmatch); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 5 | ▶ | use Business::UPS::Tracking::Utils; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 5 | ▶ | use Business::UPS::Tracking::Shipment::Freight; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 5 | ▶ | use Business::UPS::Tracking::Shipment::SmallPackage; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 5 | ▶ | use XML::LibXML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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 | 5 | has 'request' => ( | |||||||||||
| 62 | is => 'ro', | ||||||||||||
| 63 | required => 1, | ||||||||||||
| 64 | isa => 'Business::UPS::Tracking::Request', | ||||||||||||
| 65 | ); | ||||||||||||
| 66 | 5 | has 'xml' => ( | |||||||||||
| 67 | is => 'ro', | ||||||||||||
| 68 | required => 1, | ||||||||||||
| 69 | coerce => 1, | ||||||||||||
| 70 | isa => 'Business::UPS::Tracking::Type::XMLDocument', | ||||||||||||
| 71 | ); | ||||||||||||
| 72 | 5 | 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 | 5 | has 'CustomerContext' => ( | |||||||||||
| 80 | is => 'ro', | ||||||||||||
| 81 | isa => 'Str', | ||||||||||||
| 82 | lazy_build => 1, | ||||||||||||
| 83 | ); | ||||||||||||
| 84 | |||||||||||||
| 85 | 3 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=26.5
Pod
undocumented
| |||||||||||||
| 86 | 3 | my ($self) = @_; | |||||||||||
| 87 | |||||||||||||
| 88 | 3 | my $xml = $self->xml; | |||||||||||
| 89 | 3 | 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 | 3 | ▶ | Business::UPS::Tracking::X::XML->throw( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 3 | ▶ | if ($response_status == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 3 | 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 | 0 | my $shipment_return = []; | |||||||||||
| 118 | 0 | my @shipments = $xml->findnodes('/TrackResponse/Shipment'); | |||||||||||
| 119 | |||||||||||||
| 120 | 0 | foreach my $shipment_xml (@shipments) { | |||||||||||
| 121 | 0 | my $shipment_type = $xml->findvalue('ShipmentType/Code'); | |||||||||||
| 122 | 0 | my $shipment_class; | |||||||||||
| 123 | |||||||||||||
| 124 | 0 | ▶ | $shipment_type ||= '01'; | ||||||||||
|
Condition0%0/2
$shipment_type ||= "01"
MC/DC0%0/1
$shipment_type ||= "01"
$shipment_type
Truth table0%0/2
A
A= $shipment_type
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | given ($shipment_type) { | |||||||||||
| 127 | 0 | when ('01') { | |||||||||||
| 128 | 0 | $shipment_class = 'Business::UPS::Tracking::Shipment::SmallPackage'; | |||||||||||
| 129 | } | ||||||||||||
| 130 | 0 | when ('02') { | |||||||||||
| 131 | 0 | $shipment_class = 'Business::UPS::Tracking::Shipment::Freight'; | |||||||||||
| 132 | } | ||||||||||||
| 133 | 0 | default { | |||||||||||
| 134 | 0 | Business::UPS::Tracking::X::XML->throw( | |||||||||||
| 135 | error => "Unknown shipment type: $shipment_type", | ||||||||||||
| 136 | xml => $shipment_type, | ||||||||||||
| 137 | ); | ||||||||||||
| 138 | } | ||||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 0 | push @$shipment_return, $shipment_class->new( | |||||||||||
| 142 | xml => $shipment_xml, | ||||||||||||
| 143 | ); | ||||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | 0 | $self->shipment($shipment_return); | |||||||||||
| 147 | |||||||||||||
| 148 | 0 | return; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | ▶ | sub _build_CustomerContext { | ||||||||||
|
Subroutine
_build_CustomerContext: not called CC=1 SCAR=6.9
| |||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||
| 153 | |||||||||||||
| 154 | 0 | 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 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 177 | 5 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 178 | 5 | 1; | |||||||||||