File Coverage

blib/lib/Business/UPS/Tracking/Role/Base.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Role::Base;
3             # ============================================================================
4 1     1   5902 use utf8;
  1         3  
  1         9  
5 1     1   57 use 5.0100;
  1         4  
  1         37  
6              
7 1     1   935 use Moose::Role;
  0            
  0            
8              
9             no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
10              
11             use Try::Tiny;
12             use Path::Class::File;
13              
14             =encoding utf8
15              
16             =head1 NAME
17              
18             Business::UPS::Tracking::Role::Base - Helper role
19            
20             =head1 DESCRIPTION
21              
22             This role provides accessors for the UPS webservice credentials.
23             The credentials can be provided when constructing a new object, or optionally
24             stored in a configuration file.
25              
26             =head1 ACCESSORS
27              
28             =head2 AccessLicenseNumber
29              
30             UPS tracking service access license number
31              
32             =head2 UserId
33              
34             UPS account username
35              
36             =head2 Password
37              
38             UPS account password
39              
40             =head2 config
41              
42             Optionally you can retrieve all or some UPS webservice credentials from a
43             configuration file. This accessor holds the path to this file.
44             Defaults to C<~/.ups_tracking>
45              
46             Example configuration file:
47              
48             <?xml version="1.0"?>
49             <UPS_tracing_webservice_config>
50             <AccessLicenseNumber>1CFFED5A5E91B17</AccessLicenseNumber>
51             <UserId>myupsuser</UserId>
52             <Password>secret</Password>
53             </UPS_tracing_webservice_config>
54            
55             =cut
56              
57             has 'config' => (
58             is => 'rw',
59             isa => 'Str',
60             default => sub {
61             Path::Class::File->new( $ENV{HOME}, '.ups_tracking' )->stringify;
62             },
63             documentation => 'UPS tracking webservice access config file'
64             );
65              
66             has 'AccessLicenseNumber' => (
67             is => 'rw',
68             required => 1,
69             isa => 'Str',
70             lazy_build => 1,
71             predicate => '_has_AccessLicenseNumber',
72             documentation => 'UPS webservice license number (Can be set via the ups_tracking config file)',
73             );
74             has 'UserId' => (
75             is => 'rw',
76             isa => 'Str',
77             required => 1,
78             lazy_build => 1,
79             predicate => '_has_UserId',
80             documentation => 'UPS webservice user id (Can be set via the ups_tracking config file)',
81             );
82             has 'Password' => (
83             is => 'rw',
84             isa => 'Str',
85             required => 1,
86             lazy_build => 1,
87             predicate => '_has_Password',
88             documentation => 'UPS webservice password (Can be set via the ups_tracking config file)',
89             );
90              
91              
92             sub _build_AccessLicenseNumber {
93             my ($self) = @_;
94            
95             $self->_build_config();
96            
97             if ($self->_has_AccessLicenseNumber) {
98             return $self->AccessLicenseNumber;
99             }
100             }
101              
102             sub _build_UserId {
103             my ($self) = @_;
104            
105             $self->_build_config();
106            
107             if ($self->_has_UserId) {
108             return $self->UserId;
109             }
110             }
111              
112             sub _build_Password {
113             my ($self) = @_;
114            
115             $self->_build_config();
116            
117             if ($self->_has_Password) {
118             return $self->Password;
119             }
120             }
121              
122             sub _build_config {
123             my ($self) = @_;
124            
125             unless (-e $self->config) {
126             Business::UPS::Tracking::X->throw('Could not find UPS tracking webservice access config file at "'.$self->config.'"');
127             }
128            
129             my $parser = XML::LibXML->new();
130            
131             try {
132             my $document = $parser->parse_file( $self->config );
133             my $root = $document->documentElement();
134            
135             my $params = {};
136             foreach my $param ($root->childNodes) {
137             my $method = $param->nodeName;
138             next
139             unless grep { $_ eq $method } qw(AccessLicenseNumber UserId Password);
140             $params->{$method} = $param->textContent;
141             $self->$method($param->textContent);
142             }
143             return 1;
144             } catch {
145             my $e = $_ || 'Unknwon error';
146             Business::UPS::Tracking::X->throw('Could not open/parse UPS tracking webservice access config file at '.$self->config.' : '.$e);
147             };
148            
149             unless ($self->_has_AccessLicenseNumber
150             && $self->_has_UserId
151             && $self->_has_Password) {
152             Business::UPS::Tracking::X->throw('AccessLicenseNumber,UserId and Passwortd must be provided or set via a config file located at '.$self->config);
153             }
154            
155             return;
156             }
157              
158             no Moose::Role;
159             1;