File Coverage

blib/lib/Amazon/MWS/XML/Address.pm
Criterion Covered Total %
statement 30 45 66.6
branch 12 18 66.6
condition 4 6 66.6
subroutine 9 14 64.2
pod 10 10 100.0
total 65 93 69.8


line stmt bran cond sub pod time code
1             package Amazon::MWS::XML::Address;
2              
3 7     7   63 use strict;
  7         19  
  7         233  
4 7     7   42 use warnings;
  7         21  
  7         346  
5              
6 7     7   439 use Moo;
  7         10060  
  7         46  
7              
8             =head1 NAME
9              
10             Amazon::MWS::XML::Address
11              
12             =head1 DESCRIPTION
13              
14             Class to handle the addresses in the Amazon MWS's XML responses. It
15             provides some aliases to get a consistent interface.
16              
17             =head1 ACCESSORS
18              
19             =over 4
20              
21             =item Name
22              
23             Name of customer for this address.
24              
25             =item AddressLine1
26              
27             =item AddressFieldOne
28              
29             =item address1
30              
31             This is a field where Amazon stores the company name, or the c/o, or
32             postal boxes, etc. It appears as the first line of the address, but
33             you can't be sure what exactly it is (save you don't want to lose it).
34              
35             Sometimes the street address is here, sometimes is empty.
36              
37             =item AddressLine2
38              
39             =item AddressFieldTwo
40              
41             =item address2
42              
43             This appears to be the regular street/number address line, sometimes.
44             Sometimes is in the address1. You just can't know, so you have to use
45             some euristics, like checking if they are both set, otherwise choosing
46             the first available to use as street address.
47              
48             =item PostalCode
49              
50             Postal code for this address.
51              
52             =item City
53              
54             City for this address.
55              
56             =item Phone
57              
58             =item PhoneNumber
59              
60             Phone number for this address.
61              
62             =item CountryCode
63              
64             =item StateOrRegion
65              
66             =back
67              
68             =cut
69              
70              
71             has Phone => (is => 'ro');
72             has PhoneNumber => (is => 'ro');
73             has PostalCode => (is => 'ro');
74             has AddressLine1 => (is => 'ro');
75             has AddressFieldOne => (is => 'ro');
76             has AddressLine2 => (is => 'ro');
77             has AddressFieldTwo => (is => 'ro');
78             has StateOrRegion => (is => 'ro');
79             has City => (is => 'ro');
80             has Name => (is => 'ro');
81             has CountryCode => (is => 'ro');
82              
83             sub name {
84 8     8 1 27 return shift->Name;
85             }
86              
87             sub address1 {
88 10     10 1 5036 my $self = shift;
89 10   100     28 return $self->_unroll_ref($self->AddressLine1) || $self->_unroll_ref($self->AddressFieldOne) || '';
90             }
91              
92             sub address2 {
93 10     10 1 28 my $self = shift;
94 10   100     27 return $self->_unroll_ref($self->AddressLine2) || $self->_unroll_ref($self->AddressFieldTwo) || '';
95             }
96              
97             sub _unroll_ref {
98 37     37   58 my ($self, $value) = @_;
99 37 100       162 return '' unless defined $value;
100 11 100       28 if (my $reftype = ref($value)) {
101 10 100       25 if ($reftype eq 'HASH') {
    100          
102             # this is probably incorrect, but given that we don't
103             # expect this, it's better than nothing.
104 5 100       11 if (%$value) {
105             # here we don't know exactly what to do.
106 2         7 my @list = %$value;
107 2         15 return join(' ', @list);
108             }
109             else {
110 3         13 return '';
111             }
112             }
113             elsif ($reftype eq 'ARRAY') {
114 4 100       11 if (@$value) {
115 2         16 return join(' ', @$value);
116             }
117             else {
118 2         13 return '';
119             }
120             }
121             else {
122 1         78 warn "Unknown ref passed $reftype";
123 1         14 return '';
124             }
125             }
126             else {
127 1         7 return $value;
128             }
129             }
130              
131             =head2 ALIASES
132              
133             =over 4
134              
135             =item name (Name)
136              
137             =item address_line
138              
139             Concatenation of address1 and address2.
140              
141             =item city (City)
142              
143             =item zip (PostalCode)
144              
145             =item country (CountryCode)
146              
147             =item phone (Phone or PhoneNumber)
148              
149             =item region (StateOrRegion)
150              
151             =item state (StateOrRegion)
152              
153             =back
154              
155             =cut
156              
157             sub address_line {
158 0     0 1 0 my $self = shift;
159 0         0 my $line = $self->address1;
160 0 0       0 if (my $second = $self->address2) {
161 0 0       0 if ($second ne $line) {
162 0         0 $line .= "\n" . $second;
163             }
164             }
165 0         0 return $line;
166             }
167              
168             sub city {
169 0     0 1 0 return shift->City;
170             }
171              
172             sub zip {
173 0     0 1 0 return shift->PostalCode;
174             }
175              
176             sub country {
177 0     0 1 0 return shift->CountryCode;
178             }
179              
180             sub phone {
181 0     0 1 0 my $self = shift;
182 0   0     0 my $phone = $self->Phone || $self->PhoneNumber || '';
183 0 0       0 if (ref($phone) eq 'ARRAY') {
184 0         0 return join(' ', map { $_->{_} } @$phone);
  0         0  
185             }
186             else {
187 0         0 return $phone;
188             }
189             }
190              
191             sub region {
192 1     1 1 11 return shift->StateOrRegion;
193             }
194              
195             sub state {
196 1     1 1 1717 return shift->StateOrRegion;
197             }
198              
199             1;