File Coverage

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