File Coverage

blib/lib/Scrape/USPS/ZipLookup/Address.pm
Criterion Covered Total %
statement 50 118 42.3
branch 14 36 38.8
condition 4 12 33.3
subroutine 12 32 37.5
pod 20 26 76.9
total 100 224 44.6


line stmt bran cond sub pod time code
1             #
2             # Address.pm
3             #
4             # Perl 5 module to represent U.S. postal addresses for the purpose of
5             # standardizing via the U.S. Postal Service's web site:
6             #
7             # http://www.usps.com/zip4/
8             #
9             # BE SURE TO READ AND UNDERSTAND THE TERMS OF USE SECTION IN THE
10             # DOCUMENTATION, WHICH MAY BE FOUND AT THE END OF THIS SOURCE CODE.
11             #
12             # Copyright (C) 1999-2012 Gregor N. Purdy, Sr. All rights reserved.
13             #
14             # This program is free software. It is subject to the same license as Perl.
15             #
16             # [ $Id$ ]
17             #
18              
19             package Scrape::USPS::ZipLookup::Address;
20 2     2   855 use strict;
  2         3  
  2         96  
21              
22 2     2   13 use Carp;
  2         5  
  2         1232  
23              
24 2     2   26 use vars qw($VERBOSE);
  2         5  
  2         162  
25             $VERBOSE = 0;
26              
27 2     2   10 use Carp;
  2         4  
  2         113  
28              
29 2     2   985 use URI;
  2         5295  
  2         6064  
30              
31             my @input_fields = (
32             'Firm',
33             'Urbanization',
34             'Delivery Address',
35             'City',
36             'State',
37             'Zip Code'
38             );
39             my %input_fields = map { ($_, 1) } @input_fields;
40              
41             my @output_fields = (
42             'Carrier Route',
43             'County',
44             'Delivery Point',
45             'Check Digit',
46             'Commercial Mail Receiving Agency',
47             'LAC Indicator',
48             'eLOT Sequence',
49             'eLOT Indicator',
50             'Record Type',
51             'PMB Designator',
52             'PMB Number',
53             'Default Address',
54             'Early Warning',
55             'Valid',
56             );
57             my %output_fields = map { ($_, 1) } @output_fields;
58              
59              
60             my @all_fields = (
61             @input_fields,
62             @output_fields
63             );
64             my %all_fields = map { ($_, 1) } @all_fields;
65            
66              
67             #
68             # new()
69             #
70              
71             sub new
72             {
73 5     5 0 13 my $class = shift;
74 5         14 my %fields = map { ($_, undef) } @all_fields;
  100         218  
75 5         71 my $self = bless { %fields }, $class;
76              
77 5 50 33     54 return $_[0] if @_ and ref($_[0]) eq $class;
78            
79 5 50       16 if (@_) {
80 5         20 $self->input_fields(@_);
81             }
82             else {
83 0         0 confess "Cannot create empty $class!";
84             }
85              
86 5         27 return $self;
87             }
88              
89              
90             #
91             # dump()
92             #
93              
94             sub dump
95             {
96 0     0 0 0 my $self = shift;
97 0         0 my $message = shift;
98              
99 0 0       0 confess "Expected message" unless $message;
100              
101 0         0 print "ADDRESS: $message\n";
102              
103 0         0 foreach my $key (sort @all_fields) {
104 0 0       0 next unless defined $self->{$key};
105 0         0 printf " %s => '%s'\n", $key, $self->{$key};
106             }
107              
108 0         0 print "\n";
109             }
110              
111              
112             #
113             # _field()
114             #
115             # * firm()
116             # * urbanization()
117             # * delivery_address()
118             # * city()
119             # * state()
120             # * zip_code()
121             #
122             # * carrier_route()
123             # * county()
124             # * delivery_point()
125             # * check_digit()
126             #
127              
128             sub _field
129             {
130 20     20   27 my $self = shift;
131 20         26 my $field = shift;
132              
133 20 50       48 die "Undefined field!" unless defined $field;
134 20 50       62 die "Illegal field!" unless $all_fields{$field};
135              
136 20 50       39 if (@_) {
137 0         0 my $value = shift;
138 0         0 $self->{$field} = $value;
139 0         0 return $value;
140             } else {
141 20         278 return $self->{$field};
142             }
143             }
144              
145              
146 0     0 1 0 sub firm { my $self = shift; $self->_field('Firm', @_); }
  0         0  
147 0     0 1 0 sub urbanization { my $self = shift; $self->_field('Urbanization', @_); }
  0         0  
148 5     5 1 9 sub delivery_address { my $self = shift; $self->_field('Delivery Address', @_); }
  5         28  
149 5     5 1 10 sub city { my $self = shift; $self->_field('City', @_); }
  5         13  
150 5     5 1 12 sub state { my $self = shift; $self->_field('State', @_); }
  5         16  
151 5     5 1 8 sub zip_code { my $self = shift; $self->_field('Zip Code', @_); }
  5         14  
152              
153 0     0 1 0 sub carrier_route { my $self = shift; $self->_field('Carrier Route', @_); }
  0         0  
154 0     0 1 0 sub county { my $self = shift; $self->_field('County', @_); }
  0         0  
155 0     0 1 0 sub delivery_point { my $self = shift; $self->_field('Delivery Point', @_); }
  0         0  
156 0     0 1 0 sub check_digit { my $self = shift; $self->_field('Check Digit', @_); }
  0         0  
157              
158 0     0 1 0 sub commercial_mail_receiving_agency { my $self = shift; $self->_field('Commercial Mail Receiving Agency', @_); }
  0         0  
159              
160 0     0 1 0 sub lac_indicator { my $self = shift; $self->_field('LAC Indicator', @_); }
  0         0  
161 0     0 1 0 sub elot_sequence { my $self = shift; $self->_field('eLOT Sequence', @_); }
  0         0  
162 0     0 1 0 sub elot_indicator { my $self = shift; $self->_field('eLOT Indicator', @_); }
  0         0  
163 0     0 1 0 sub record_type { my $self = shift; $self->_field('Record Type', @_); }
  0         0  
164 0     0 1 0 sub pmb_designator { my $self = shift; $self->_field('PMB Designator', @_); }
  0         0  
165 0     0 1 0 sub pmb_number { my $self = shift; $self->_field('PMB Number', @_); }
  0         0  
166 0     0 1 0 sub default_address { my $self = shift; $self->_field('Default Address', @_); }
  0         0  
167 0     0 1 0 sub early_warning { my $self = shift; $self->_field('Early Warning', @_); }
  0         0  
168 0     0 1 0 sub valid { my $self = shift; $self->_field('Valid', @_); }
  0         0  
169              
170              
171             #
172             # input_fields()
173             #
174             # Set or get all input fields simultaneously. When setting, any unspecified
175             # fields are set to undef and all output fields are set to the undef as well.
176             #
177              
178             sub input_fields
179             {
180 5     5 0 9 my $self = shift;
181              
182 5 50       16 unless (@_) {
183 0         0 my %fields = map { ($_, $self->{$_}) } @input_fields;
  0         0  
184 0         0 print join("\n", %fields), "\n";
185 0         0 return %fields;
186             }
187              
188 5         12 my %fields = map { ($_, undef) } @input_fields;
  30         61  
189 5         11 my %input;
190              
191 5 50 33     67 if (@_ == 1 and ref $_[0] and UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")) {
    50 33        
    50 33        
    50          
    100          
    50          
    0          
192 0         0 return $_[0];
193             } elsif (@_ == 1 and ref $_[0] eq 'HASH') {
194 0         0 %input = %{$_[0]};
  0         0  
195             } elsif (@_ == 6) {
196 0         0 %input = (
197             'Firm' => $_[0],
198             'Urbanization' => $_[1],
199             'Delivery Address' => $_[2],
200             'City' => $_[3],
201             'State' => $_[4],
202             'Zip Code' => $_[5],
203             );
204             } elsif (@_ == 5) {
205 0         0 %input = (
206             'Firm' => $_[0],
207             'Delivery Address' => $_[1],
208             'City' => $_[2],
209             'State' => $_[3],
210             'Zip Code' => $_[4],
211             );
212             } elsif (@_ == 4) {
213 3         19 %input = (
214             'Delivery Address' => $_[0],
215             'City' => $_[1],
216             'State' => $_[2],
217             'Zip Code' => $_[3],
218             );
219             } elsif (@_ == 3) {
220 2         14 %input = (
221             'Delivery Address' => $_[0],
222             'City' => $_[1],
223             'State' => $_[2],
224             );
225             } elsif (@_ == 2) {
226 0         0 %input = (
227             'Delivery Address' => $_[0],
228             'Zip Code' => $_[1],
229             );
230             } else {
231 0         0 confess "Unrecognized input (" . scalar(@_) . " args: " . join(', ', map { ref } @_) . ")!";
  0         0  
232             }
233              
234 5         16 foreach (@all_fields) { $self->{$_} = undef; }
  100         146  
235              
236 5         19 foreach my $key (keys %input) {
237 18 50       43 die "Illegal field '$key'!" unless $input_fields{$key};
238 18         54 $self->{$key} = $input{$key};
239             }
240             }
241              
242              
243             #
244             # to_dump()
245             #
246              
247             sub to_dump
248             {
249 0     0 0   my $self = shift;
250 0           my @lines;
251              
252 0           foreach (@all_fields) {
253 0 0         push @lines, sprintf("%-20s => %s", $_, $self->{$_}) unless $self->{$_} eq '';
254             }
255              
256 0           return join("\n", @lines);
257             }
258              
259              
260             #
261             # to_array()
262             #
263              
264             sub to_array
265             {
266 0     0 0   my $self = shift;
267 0           my @lines;
268              
269 0           foreach (@input_fields) {
270 0 0         push @lines, $self->{$_} unless $self->{$_} eq '';
271             }
272              
273 0           return @lines;
274             }
275              
276              
277             #
278             # to_string()
279             #
280              
281             sub to_string
282             {
283 0     0 0   my $self = shift;
284              
285 0           return join("\n", $self->delivery_address, $self->city, $self->state, $self->zip_code);
286             }
287              
288              
289             #
290             # Proper module termination:
291             #
292              
293             1;
294              
295             __END__