File Coverage

blib/lib/Business/US/USPS/WebTools/AddressStandardization.pm
Criterion Covered Total %
statement 15 36 41.6
branch n/a
condition 0 2 0.0
subroutine 5 11 45.4
pod 1 1 100.0
total 21 50 42.0


line stmt bran cond sub pod time code
1             # $Id: AddressStandardization.pm 2360 2007-11-03 04:48:52Z comdog $
2             package Business::US::USPS::WebTools::AddressStandardization;
3 1     1   1775 use strict;
  1         2  
  1         42  
4 1     1   6 no warnings 'uninitialized';
  1         2  
  1         37  
5              
6 1     1   6 use base qw(Business::US::USPS::WebTools);
  1         1  
  1         94  
7              
8 1     1   6 use subs qw();
  1         562  
  1         28  
9 1     1   6 use vars qw($VERSION);
  1         2  
  1         471  
10              
11             $VERSION = 1.11;
12              
13             =head1 NAME
14              
15             Business::US::USPS::WebTools::AddressStandardization - canonicalize a US address
16              
17             =head1 SYNOPSIS
18              
19             use Business::US::USPS::WebTools::AddressStandardization;
20              
21             my $verifier = Business::US::USPS::WebTools::AddressStandardization->new( {
22             UserID => $ENV{USPS_WEBTOOLS_USERID},
23             Password => $ENV{USPS_WEBTOOLS_PASSWORD},
24             Testing => 1,
25             } );
26            
27             my $hash = $verifier->verify_address(
28             FirmName => '',
29             Address1 => '',
30             Address2 => '6406 Ivy Lane',
31             City => 'Greenbelt',
32             State => 'MD',
33             Zip5 => '',
34             Zip4 => '',
35             );
36              
37             if( $verifier->is_error )
38             {
39             warn "Oh No! $verifier->{error}{description}\n";
40             }
41             else
42             {
43             print join "\n", map { "$_: $hash->{$_}" }
44             qw(FirmName Address1 Address2 City State Zip5 Zip4);
45             }
46            
47            
48             =head1 DESCRIPTION
49              
50             *** THIS IS ALPHA SOFTWARE ***
51              
52             This module implements the Address Standardization web service from the
53             US Postal Service. It is a subclass of Business::US::USPS::WebTools.
54              
55             =cut
56              
57             =over 4
58              
59             =cut
60              
61 0     0     sub _fields { qw( FirmName Address1 Address2 City State Zip5 Zip4 ) }
62 0     0     sub _required { qw( Address2 City State ) }
63              
64             =item verify_address( KEY, VALUE, ... )
65              
66             The C method takes the following keys, which come
67             directly from the USPS web service interface:
68              
69             FirmName The name of the company
70             Address1 The suite or apartment
71             Address2 The street address
72             City The name of the city
73             State The two letter state abbreviation
74             Zip5 The 5 digit zip code
75             Zip4 The 4 digit extension to the zip code
76            
77             It returns an anonymous hash with the same keys, but the values are
78             the USPS's canonicalized address. If there is an error, the hash values
79             will be the empty string, and the error flag is set. Check is with C:
80              
81             $verifier->is_error;
82            
83             See the C documentation in Business::US::USPS::WebTools for more
84             details on error information.
85            
86             =cut
87              
88             sub verify_address
89             {
90 0     0 1   my( $self, %hash ) = @_;
91            
92 0           $self->_make_url( \%hash );
93            
94 0           $self->_make_request;
95            
96 0           $self->_parse_response;
97             }
98              
99            
100 0     0     sub _api_name { "Verify" }
101              
102             sub _make_query_xml
103             {
104 0     0     my( $self, $hash ) = @_;
105            
106 0           my $user = $self->userid;
107 0           my $pass = $self->password;
108            
109 0           my $xml =
110             qq|| .
111             qq|
|;
112              
113 0           foreach my $field ( $self->_fields )
114             {
115 0           $xml .= "<$field>$$hash{$field}";
116             }
117              
118 0           $xml .= qq||;
119              
120 0           return $xml;
121             }
122              
123             sub _parse_response
124             {
125 0     0     my( $self ) = @_;
126             #require 'Hash::AsObject';
127            
128 0           my %hash = ();
129 0           foreach my $field ( $self->_fields )
130             {
131 0           my( $value ) = $self->response =~ m|<$field>(.*?)|g;
132            
133 0   0       $hash{$field} = $value || '';
134             }
135            
136 0           bless \%hash, ref $self; # 'Hash::AsObject';
137             }
138              
139             =back
140              
141             =head1 TO DO
142              
143             =head1 SEE ALSO
144              
145             L
146              
147             The WebTools API is documented on the US Postal Service's website:
148              
149             http://www.usps.com/webtools/htm/Address-Information.htm
150              
151             =head1 SOURCE AVAILABILITY
152              
153             This source is part of a SourceForge project which always has the
154             latest sources in CVS, as well as all of the previous releases.
155              
156             http://sourceforge.net/projects/brian-d-foy/
157              
158             If, for some reason, I disappear from the world, one of the other
159             members of the project can shepherd this module appropriately.
160              
161             =head1 AUTHOR
162              
163             brian d foy, C<< >>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             Copyright (c) 2006-2007, brian d foy, All Rights Reserved.
168              
169             You may redistribute this under the same terms as Perl itself.
170              
171             =cut
172              
173             1;