File Coverage

blib/lib/Business/UPS/Tracking/Role/Base.pm
Criterion Covered Total %
statement 20 52 38.4
branch 0 12 0.0
condition 0 8 0.0
subroutine 7 13 53.8
pod n/a
total 27 85 31.7


line stmt bran cond sub pod time code
1             # ============================================================================
2             package Business::UPS::Tracking::Role::Base;
3             # ============================================================================
4 1     1   9619 use utf8;
  1         3  
  1         6  
5 1     1   39 use 5.0100;
  1         4  
6              
7 1     1   5 use Moose::Role;
  1         3  
  1         10  
8              
9 1     1   5682 no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
  1         3  
  1         8  
10              
11 1     1   70 use Try::Tiny;
  1         3  
  1         61  
12 1     1   499 use Path::Class::File;
  1         27965  
  1         387  
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 0     0     my ($self) = @_;
94              
95 0           $self->_build_config();
96              
97 0 0         if ($self->_has_AccessLicenseNumber) {
98 0           return $self->AccessLicenseNumber;
99             }
100             }
101              
102             sub _build_UserId {
103 0     0     my ($self) = @_;
104              
105 0           $self->_build_config();
106              
107 0 0         if ($self->_has_UserId) {
108 0           return $self->UserId;
109             }
110             }
111              
112             sub _build_Password {
113 0     0     my ($self) = @_;
114              
115 0           $self->_build_config();
116              
117 0 0         if ($self->_has_Password) {
118 0           return $self->Password;
119             }
120             }
121              
122             sub _build_config {
123 0     0     my ($self) = @_;
124              
125 0 0         unless (-e $self->config) {
126 0           Business::UPS::Tracking::X->throw('Could not find UPS tracking webservice access config file at "'.$self->config.'"');
127             }
128              
129 0           my $parser = XML::LibXML->new();
130              
131             try {
132 0     0     my $document = $parser->parse_file( $self->config );
133 0           my $root = $document->documentElement();
134              
135 0           my $params = {};
136 0           foreach my $param ($root->childNodes) {
137 0           my $method = $param->nodeName;
138             next
139 0 0         unless grep { $_ eq $method } qw(AccessLicenseNumber UserId Password);
  0            
140 0           $params->{$method} = $param->textContent;
141 0           $self->$method($param->textContent);
142             }
143 0           return 1;
144             } catch {
145 0   0 0     my $e = $_ || 'Unknwon error';
146 0           Business::UPS::Tracking::X->throw('Could not open/parse UPS tracking webservice access config file at '.$self->config.' : '.$e);
147 0           };
148              
149 0 0 0       unless ($self->_has_AccessLicenseNumber
      0        
150             && $self->_has_UserId
151             && $self->_has_Password) {
152 0           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 0           return;
156             }
157              
158 1     1   9 no Moose::Role;
  1         4  
  1         10  
159             1;