ZipLookup/Address.pm

filter: Statementstmt 20.1% 26 / 129 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 15.6% 5 / 32 Podpod 76.9% 20 / 26 totaltotal 21.0% 51 / 242 CC 28 SCAR 42.1
CC
28
Cov
15%
input_fields
240.0
new
20.0
dump
20.0
CRAP 67.1
↑ Summary ZipLookup.pm »
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use vars qw($VERBOSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $VERBOSE = 0;
26  
27 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 my @input_fields = (
32 'Firm',
33 'Urbanization',
34 'Delivery Address',
35 'City',
36 'State',
37 'Zip Code'
38 );
39 1 my %input_fields = map { ($_, 1) } @input_fields;
40  
41 1 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 1 my %output_fields = map { ($_, 1) } @output_fields;
58  
59  
60 1 my @all_fields = (
61 @input_fields,
62 @output_fields
63 );
64 1 my %all_fields = map { ($_, 1) } @all_fields;
65
66  
67 #
68 # new()
69 #
70  
71 sub new
72 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
73 0 my $class = shift;
74 0 my %fields = map { ($_, undef) } @all_fields;
75 0 my $self = bless { %fields }, $class;
76  
77 0 ▶ return $_[0] if @_ and ref($_[0]) eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref $_[0] eq $class 0 0 0
Condition0%0/3
@_ and ref $_[0] eq $class
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq $class
@_ ref $_[0] eq $class
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq $class
ABresult
0X0
100
111
78
79 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
80 0 $self->input_fields(@_);
81 }
82 else {
83 0 confess "Cannot create empty $class!";
84 }
85  
86 0 return $self;
87 }
88  
89  
90 #
91 # dump()
92 #
93  
94 sub dump
95 0 ▶{
Subroutine dump: not called CC=4 SCAR=30.0
Pod undocumented
96 0 my $self = shift;
97 0 my $message = shift;
98  
99 0 ▶ confess "Expected message" unless $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 0 0 0
100  
101 0 print "ADDRESS: $message\n";
102  
103 0 foreach my $key (sort @all_fields) {
104 0 ▶ next unless defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$key} 0 0 0
105 0 printf " %s => '%s'\n", $key, $self->{$key};
106 }
107  
108 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 0 ▶{
Subroutine _field: not called CC=4 SCAR=30.0
130 0 my $self = shift;
131 0 my $field = shift;
132  
133 0 ▶ die "Undefined field!" unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 0 0
134 0 ▶ die "Illegal field!" unless $all_fields{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $all_fields{$field} 0 0 0
135  
136 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
137 0 my $value = shift;
138 0 $self->{$field} = $value;
139 0 return $value;
140 } else {
141 0 return $self->{$field};
142 }
143 }
144  
145  
146 0 ▶sub firm { my $self = shift; $self->_field('Firm', @_); }
Subroutine firm: not called CC=1 SCAR=6.9
Pod documented
147 0 ▶sub urbanization { my $self = shift; $self->_field('Urbanization', @_); }
Subroutine urbanization: not called CC=1 SCAR=6.9
Pod documented
148 0 ▶sub delivery_address { my $self = shift; $self->_field('Delivery Address', @_); }
Subroutine delivery_address: not called CC=1 SCAR=6.9
Pod documented
149 0 ▶sub city { my $self = shift; $self->_field('City', @_); }
Subroutine city: not called CC=1 SCAR=6.9
Pod documented
150 0 ▶sub state { my $self = shift; $self->_field('State', @_); }
Subroutine state: not called CC=1 SCAR=6.9
Pod documented
151 0 ▶sub zip_code { my $self = shift; $self->_field('Zip Code', @_); }
Subroutine zip_code: not called CC=1 SCAR=6.9
Pod documented
152  
153 0 ▶sub carrier_route { my $self = shift; $self->_field('Carrier Route', @_); }
Subroutine carrier_route: not called CC=1 SCAR=6.9
Pod documented
154 0 ▶sub county { my $self = shift; $self->_field('County', @_); }
Subroutine county: not called CC=1 SCAR=6.9
Pod documented
155 0 ▶sub delivery_point { my $self = shift; $self->_field('Delivery Point', @_); }
Subroutine delivery_point: not called CC=1 SCAR=6.9
Pod documented
156 0 ▶sub check_digit { my $self = shift; $self->_field('Check Digit', @_); }
Subroutine check_digit: not called CC=1 SCAR=6.9
Pod documented
157  
158 0 ▶sub commercial_mail_receiving_agency { my $self = shift; $self->_field('Commercial Mail Receiving Agency', @_); }
Subroutine commercial_mail_receiving_agency: not called CC=1 SCAR=6.9
Pod documented
159  
160 0 ▶sub lac_indicator { my $self = shift; $self->_field('LAC Indicator', @_); }
Subroutine lac_indicator: not called CC=1 SCAR=6.9
Pod documented
161 0 ▶sub elot_sequence { my $self = shift; $self->_field('eLOT Sequence', @_); }
Subroutine elot_sequence: not called CC=1 SCAR=6.9
Pod documented
162 0 ▶sub elot_indicator { my $self = shift; $self->_field('eLOT Indicator', @_); }
Subroutine elot_indicator: not called CC=1 SCAR=6.9
Pod documented
163 0 ▶sub record_type { my $self = shift; $self->_field('Record Type', @_); }
Subroutine record_type: not called CC=1 SCAR=6.9
Pod documented
164 0 ▶sub pmb_designator { my $self = shift; $self->_field('PMB Designator', @_); }
Subroutine pmb_designator: not called CC=1 SCAR=6.9
Pod documented
165 0 ▶sub pmb_number { my $self = shift; $self->_field('PMB Number', @_); }
Subroutine pmb_number: not called CC=1 SCAR=6.9
Pod documented
166 0 ▶sub default_address { my $self = shift; $self->_field('Default Address', @_); }
Subroutine default_address: not called CC=1 SCAR=6.9
Pod documented
167 0 ▶sub early_warning { my $self = shift; $self->_field('Early Warning', @_); }
Subroutine early_warning: not called CC=1 SCAR=6.9
Pod documented
168 0 ▶sub valid { my $self = shift; $self->_field('Valid', @_); }
Subroutine valid: not called CC=1 SCAR=6.9
Pod documented
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 0 ▶{
Subroutine input_fields: not called CC=15 SCAR=54.8
Pod undocumented
180 0 my $self = shift;
181  
182 0 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
183 0 my %fields = map { ($_, $self->{$_}) } @input_fields;
184 0 print join("\n", %fields), "\n";
185 0 return %fields;
186 }
187  
188 0 my %fields = map { ($_, undef) } @input_fields;
189 0 my %input;
190  
191 0 ▶ if (@_ == 1 and ref $_[0] and UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")) {
Branch: 7 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and UNIVERSAL::isa($_[0], 'Scrape::USPS::ZipLookup::Address')) { } 0 0 0
elsif (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
elsif (@_ == 6) { } 0 0 0
elsif (@_ == 5) { } 0 0 0
elsif (@_ == 4) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] and UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")
!ll&&!rl&&r
000
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] and UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")
@_ == 1 ref $_[0] UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0]C= UNIVERSAL::isa($_[0], "Scrape::USPS::ZipLookup::Address")
ABCresult
0XX0
10X0
1100
1111
192 0 return $_[0];
193 ▶ } elsif (@_ == 1 and ref $_[0] eq 'HASH') {
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
194 0 %input = %{$_[0]};
195 } elsif (@_ == 6) {
196 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 %input = (
206 'Firm' => $_[0],
207 'Delivery Address' => $_[1],
208 'City' => $_[2],
209 'State' => $_[3],
210 'Zip Code' => $_[4],
211 );
212 } elsif (@_ == 4) {
213 0 %input = (
214 'Delivery Address' => $_[0],
215 'City' => $_[1],
216 'State' => $_[2],
217 'Zip Code' => $_[3],
218 );
219 } elsif (@_ == 3) {
220 0 %input = (
221 'Delivery Address' => $_[0],
222 'City' => $_[1],
223 'State' => $_[2],
224 );
225 } elsif (@_ == 2) {
226 0 %input = (
227 'Delivery Address' => $_[0],
228 'Zip Code' => $_[1],
229 );
230 } else {
231 0 confess "Unrecognized input (" . scalar(@_) . " args: " . join(', ', map { ref } @_) . ")!";
232 }
233  
234 0 foreach (@all_fields) { $self->{$_} = undef; }
235  
236 0 foreach my $key (keys %input) {
237 0 ▶ die "Illegal field '$key'!" unless $input_fields{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $input_fields{$key} 0 0 0
238 0 $self->{$key} = $input{$key};
239 }
240 }
241  
242  
243 #
244 # to_dump()
245 #
246  
247 sub to_dump
248 0 ▶{
Subroutine to_dump: not called CC=3 SCAR=24.8
Pod undocumented
249 0 my $self = shift;
250 0 my @lines;
251  
252 0 foreach (@all_fields) {
253 0 ▶ push @lines, sprintf("%-20s => %s", $_, $self->{$_}) unless $self->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} eq "" 0 0 0
254 }
255  
256 0 return join("\n", @lines);
257 }
258  
259  
260 #
261 # to_array()
262 #
263  
264 sub to_array
265 0 ▶{
Subroutine to_array: not called CC=3 SCAR=24.8
Pod undocumented
266 0 my $self = shift;
267 0 my @lines;
268  
269 0 foreach (@input_fields) {
270 0 ▶ push @lines, $self->{$_} unless $self->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} eq "" 0 0 0
271 }
272  
273 0 return @lines;
274 }
275  
276  
277 #
278 # to_string()
279 #
280  
281 sub to_string
282 0 ▶{
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
283 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 1;
294  
295 __END__
↑ Summary ZipLookup.pm »