default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / Misc / Geo.pm
index 599b2a0..6f923f5 100644 (file)
@@ -134,8 +134,11 @@ sub get_censustract_uscensus {
 
   my $url = 'https://geocoding.geo.census.gov/geocoder/geographies/address?';
 
+  my $address1 = $location->{address1};
+  $address1 =~ s/(apt|ste|suite|unit)[\s\d]\w*\s*$//i;
+
   my $query_hash = {
-                     street     => $location->{address1},
+                     street     => $address1,
                      city       => $location->{city},
                      state      => $location->{state},
                      benchmark  => 'Public_AR_Current',
@@ -163,9 +166,11 @@ sub get_censustract_uscensus {
 
   warn Dumper($content) if $DEBUG;
 
-  if ( $content->{result}->{addressMatches} ) {
+  my $addressMatches_ref = $content->{result}->{addressMatches};
+
+  if ( $addressMatches_ref && scalar @{$addressMatches_ref} ) {
 
-    my $tract = $content->{result}->{addressMatches}[0]->{geographies}->{'Census Blocks'}[0]->{GEOID};
+    my $tract = $addressMatches_ref->[0]->{geographies}->{'Census Blocks'}[0]->{GEOID};
     return $tract;
 
   } else {