File Coverage

blib/lib/Business/UPS/Tracking/Commandline.pm
Criterion Covered Total %
statement 11 31 35.4
branch 0 4 0.0
condition n/a
subroutine 4 6 66.6
pod n/a
total 15 41 36.5


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Commandline;
3             # ============================================================================
4 1     1   2075 use utf8;
  1         2  
  1         7  
5 1     1   44 use 5.0100;
  1         4  
6              
7 1     1   5 use Moose;
  1         2  
  1         7  
8             extends qw(Business::UPS::Tracking::Request);
9             with qw(MooseX::Getopt Business::UPS::Tracking::Role::Base);
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             Business::UPS::Tracking::Commandline - Commandline interface to UPS tracking
16              
17             =head1 SYNOPSIS
18              
19             my $commandline = Business::UPS::Tracking::Commandline->new_with_options;
20             # Params are taken from @ARGV
21             $commandline->execute;
22              
23             =head1 DESCRIPTION
24              
25             This class allows Business::UPS::Tracking being called from a commandline
26             script using L<MooseX::Getopt>. (See L<ups_tracking>)
27              
28             =head1 ACCESSORS
29              
30             =head2 Inherited
31              
32             All accessors from L<Business::UPS::Tracking::Request>
33              
34             =head2 verbose
35              
36             Be verbose
37              
38             =head2 AccessLicenseNumber
39              
40             UPS tracking service access license number
41              
42             =head2 UserId
43              
44             UPS account username
45              
46             =head2 Password
47              
48             UPS account password
49              
50             =head2 config
51              
52             Optionally you can retrieve all or some UPS webservice credentials from a
53             configuration file. This accessor holds the path to this file.
54             Defaults to C<~/.ups_tracking>
55              
56             Example configuration file:
57              
58             <?xml version="1.0"?>
59             <UPS_tracking_webservice_config>
60             <AccessLicenseNumber>1CFFED5A5E91B17</AccessLicenseNumber>
61             <UserId>myupsuser</UserId>
62             <Password>secret</Password>
63             </UPS_tracking_webservice_config>
64              
65             =head1 METHODS
66              
67             =head3 execute
68              
69             $commandline->execute;
70              
71             Performs a UPS webservice query/request.
72              
73             =cut
74              
75             has 'tracking' => (
76             is => 'rw',
77             required => 0,
78             isa => 'Business::UPS::Tracking',
79             traits => [ 'NoGetopt' ],
80             lazy_build => 1,
81             );
82              
83             has 'verbose' => (
84             is => 'rw',
85             isa => 'Bool',
86             documentation => 'Be verbose',
87             );
88              
89             MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
90             'Business::UPS::Tracking::Type::TrackingNumber' => '=s',
91             'Business::UPS::Tracking::Type::CountryCode' => '=s',
92             );
93              
94             __PACKAGE__->meta->make_immutable;
95              
96             sub execute {
97 0     0     my $self = shift;
98              
99 0           my $response = $self->run();
100              
101 0           my $count = 1;
102              
103 0           foreach my $shipment (@{$response->shipment}) {
  0            
104 0           say ".============================================================================.";
105 0           say "| Shipment $count |";
106 0           say $shipment->printall->draw;
107 0           say "";
108 0 0         if ($self->verbose) {
109 0           say $shipment->xml->toString(1);
110             }
111 0           $count ++;
112             }
113              
114 0           return;
115             }
116              
117             sub _build_tracking {
118 0     0     my ($self) = @_;
119              
120 0           my %params = ();
121 0           foreach my $field (qw(AccessLicenseNumber UserId Password)) {
122 0           my $predicate = '_has_'.$field;
123 0 0         if ($self->$predicate) {
124 0           $params{$field} = $self->$field;
125             }
126             }
127              
128 0           return Business::UPS::Tracking->new(\%params);
129             }
130              
131             __PACKAGE__->meta->make_immutable;
132 1     1   8101 no Moose;
  1         3  
  1         5  
133             1;