X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FMisc%2FGeo.pm;h=92490bb3b86a4cfc83923d16722c02bcc78b8833;hp=1aa593974e3cde3c21436614052ebadcb8677fc6;hb=820906c790f19828477eabd37218c71c2a724d86;hpb=fe529fcf74225297231dc3678594166720721205 diff --git a/FS/FS/Misc/Geo.pm b/FS/FS/Misc/Geo.pm index 1aa593974..92490bb3b 100644 --- a/FS/FS/Misc/Geo.pm +++ b/FS/FS/Misc/Geo.pm @@ -6,11 +6,13 @@ use vars qw( $DEBUG @EXPORT_OK $conf ); use LWP::UserAgent; use HTTP::Request; use HTTP::Request::Common qw( GET POST ); +use IO::Socket::SSL; use HTML::TokeParser; -use JSON; +use Cpanel::JSON::XS; use URI::Escape 3.31; use Data::Dumper; use FS::Conf; +use FS::Log; use Locale::Country; FS::UID->install_callback( sub { @@ -300,6 +302,8 @@ sub standardize_usps { sub standardize_uscensus { my $self = shift; my $location = shift; + my $log = FS::Log->new('FS::Misc::Geo::standardize_uscensus'); + $log->debug(join("\n", @{$location}{'address1', 'city', 'state', 'zip'})); eval "use Geo::USCensus::Geocoding"; die $@ if $@; @@ -322,6 +326,7 @@ sub standardize_uscensus { my $result = Geo::USCensus::Geocoding->query($request); if ( $result->is_match ) { # unfortunately we get the address back as a single line + $log->debug($result->address); if ($result->address =~ /^(.*), (.*), ([A-Z]{2}), (\d{5}.*)$/) { return +{ address1 => $1, @@ -341,8 +346,8 @@ sub standardize_uscensus { } elsif ( $result->match_level ) { die "Geocoding did not find a matching address.\n"; } else { - warn Dumper($result) if $DEBUG; - die $result->error_message; + $log->error($result->error_message); + return; # for internal errors, don't return anything } } @@ -638,6 +643,50 @@ sub standardize_melissa { } } +sub standardize_freeside { + my $class = shift; + my $location = shift; + + my $url = 'https://ws.freeside.biz/normalize'; + + #free freeside.biz normalization only for US + if ( $location->{country} ne 'US' ) { + # soft failure + #why? something else could have cleaned it $location->{addr_clean} = ''; + return $location; + } + + my $ua = LWP::UserAgent->new( + 'ssl_opts' => { + verify_hostname => 0, + SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, + }, + ); + my $response = $ua->request( POST $url, [ + 'support-key' => scalar($conf->config('support-key')), + %$location, + ]); + + die "Address normalization error: ". $response->message + unless $response->is_success; + + local $@; + my $content = eval { decode_json($response->content) }; + if ( $@ ) { + warn $response->content; + die "Address normalization JSON error : $@\n"; + } + + die $content->{error}."\n" + if $content->{error}; + + { 'addr_clean' => 'Y', + map { $_ => $content->{$_} } + qw( address1 address2 city state zip country ) + }; + +} + =back =cut