Skip to content

Commit

Permalink
Latest Utils.pm from VWF
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Dec 19, 2024
1 parent ed2f451 commit 7c1b519
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Revision history for Geo-Coder-Free

0.38
Latest as_string return code from VWF
Latest as_string return code and Utils.pm from VWF
Added t/config.t, t/utils.t and t/display.t

0.37 Wed Oct 23 10:09:54 EDT 2024
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ my $build_requires = {
'JSON::MaybeXS' => 0,
'LWP::UserAgent::Throttled' => 0,
'LWP::Protocol::https' => 0,
'Scalar::Util' => 0,
'Try::Tiny' => 0
};

Expand Down
11 changes: 11 additions & 0 deletions createdatabase.PL
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,17 @@ my %known_places = ( # Places I've checked with my GPS
'STATE' => 'VA',
'COUNTRY' => 'US',
'POSTCODE' => 22180
}, {
'LAT' => 39.099655,
'LON' => -77.836975,
'NAME' => 'DIRT FARM BREWERY',
'NUMBER' => 18701,
'STREET' => 'FOGGY BOTTOM RD',
'CITY' => 'BLUEMONT',
'COUNTY' => 'LOUDON',
'STATE' => 'VA',
'COUNTRY' => 'US',
'POSTCODE' => 20135
}, {
'LAT' => 39.124843,
'LON' => -77.535445,
Expand Down
23 changes: 18 additions & 5 deletions lib/Geo/Coder/Free.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use Geo::Coder::Free::OpenAddresses;
use List::MoreUtils;
use Locale::US;
use Carp;
use Scalar::Util;

=head1 NAME
Expand Down Expand Up @@ -88,16 +89,28 @@ but that can't be guaranteed to work.

sub new {
my $class = shift;
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

# Handle hash or hashref arguments
my %args;
if((@_ == 1) && (ref $_[0] eq 'HASH')) {
%args = %{$_[0]};
} elsif((@_ % 2) == 0) {
%args = @_;
} else {
carp(__PACKAGE__, ': Invalid arguments passed to new()');
return;
}

if(!defined($class)) {
# Using Geo::Coder::Free->new not Geo::Coder::Free::new
# carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
# return;
if((scalar keys %args) > 0) {
# Using Geo::Coder::Free->new not Geo::Coder::Free::new
carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
return;
}

# FIXME: this only works when no arguments are given
$class = __PACKAGE__;
} elsif(ref($class)) {
} elsif(Scalar::Util::blessed($class)) {
# clone the given object
return bless { %{$class}, %args }, ref($class);
}
Expand Down
1 change: 1 addition & 0 deletions lib/Geo/Coder/Free/Local.pm
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ __DATA__
"SHENANDOAH COOL SPRINGS BATTLEFIELD",,"","BLUEMONT","CLARKE","VA","US",39.142146,-77.866468
"",14900,"CONFERENCE CENTER DR","CHANTILLY","FAIRFAX","VA","US",38.873934,-77.461939
"THE PURE PASTY COMPANY",128C,"MAPLE AVE W","VIENNA","FAIRFAX","VA","US",44.40662476,-68.59610059
"DIRT FARM BREWERY",18701,"FOGGY BOTTOM RD","BLUEMONT","LOUDON","VA","US",39.099655,-77.836975
"",818,"FERNDALE TERRACE NE","LEESBURG","LOUDOUN","VA","US",39.124843,-77.535445
"",,"OATLANDS PLANTATION LN","OATLANDS","LOUDOUN","VA","US",39.04071,-77.61682
"",,"PURCELLVILLE GATEWAY DR","PURCELLVILLE","LOUDOUN","VA","US",39.136193,-77.693198
Expand Down
73 changes: 53 additions & 20 deletions lib/Geo/Coder/Free/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ Supports multiple cache drivers, including BerkeleyDB, DBI, and Redis.
=cut

sub create_disc_cache {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
my $args = get_params(undef, @_);

my $config = $args{'config'};
my $config = $args->{'config'};
throw Error::Simple('config is not optional') unless($config);

my $logger = $args{'logger'};
my $logger = $args->{'logger'};
my $driver = $config->{disc_cache}->{driver};
unless(defined($driver)) {
my $root_dir = $ENV{'root_dir'} || $args{'root_dir'} || $config->{disc_cache}->{root_dir} || $config->{'root_dir'};
my $root_dir = $ENV{'root_dir'} || $args->{'root_dir'} || $config->{disc_cache}->{root_dir} || $config->{'root_dir'};
throw Error::Simple('root_dir is not optional') unless($root_dir);

if($logger) {
$logger->warn(Data::Dumper->new([$config])->Dump());
$logger->warn('disc_cache not defined in ', $config->{'config_path'}, ' falling back to BerkeleyDB');
}
return CHI->new(driver => 'BerkeleyDB', root_dir => $root_dir, namespace => $args{'namespace'});
return CHI->new(driver => 'BerkeleyDB', root_dir => $root_dir, namespace => $args->{'namespace'});
}
if($logger) {
$logger->debug('disc cache via ', $config->{disc_cache}->{driver}, ', namespace: ', $args{'namespace'});
$logger->debug('disc cache via ', $config->{disc_cache}->{driver}, ', namespace: ', $args->{'namespace'});
}

my %chi_args = (
on_get_error => 'warn',
on_set_error => 'die',
driver => $driver,
namespace => $args{'namespace'}
namespace => $args->{'namespace'}
);

# Don't do this because it takes a lot of complex configuration
Expand Down Expand Up @@ -102,7 +102,7 @@ sub create_disc_cache {
);
$chi_args{'redis_options'} = \%redis_options;
} elsif($driver ne 'Null') {
$chi_args{'root_dir'} = $ENV{'root_dir'} || $args{'root_dir'} || $config->{disc_cache}->{root_dir};
$chi_args{'root_dir'} = $ENV{'root_dir'} || $args->{'root_dir'} || $config->{disc_cache}->{root_dir};
throw Error::Simple('root_dir is not optional') unless($chi_args{'root_dir'});
if($logger) {
$logger->debug("root_dir: $chi_args{root_dir}");
Expand All @@ -119,30 +119,30 @@ Supports multiple cache drivers, including SharedMem, Memory, and Redis.
=cut

sub create_memory_cache {
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
my $args = get_params(undef, @_);

my $config = $args{'config'};
my $config = $args->{'config'};
throw Error::Simple('config is not optional') unless($config);

my $logger = $args{'logger'};
my $logger = $args->{'logger'};
my $driver = $config->{memory_cache}->{driver};
unless(defined($driver)) {
if($logger) {
$logger->warn('memory_cache not defined in ', $config->{'config_path'}, ' falling back to sharedmem');
}
# return CHI->new(driver => 'Memcached', servers => [ '127.0.0.1:11211' ], namespace => $args{'namespace'});
# return CHI->new(driver => 'File', root_dir => '/tmp/cache', namespace => $args{'namespace'});
return CHI->new(driver => 'SharedMem', max_size => 1024, shm_size => 16 * 1024, shm_key => 98766789, namespace => $args{'namespace'});
# return CHI->new(driver => 'Memcached', servers => [ '127.0.0.1:11211' ], namespace => $args->{'namespace'});
# return CHI->new(driver => 'File', root_dir => '/tmp/cache', namespace => $args->{'namespace'});
return CHI->new(driver => 'SharedMem', max_size => 1024, shm_size => 16 * 1024, shm_key => 98766789, namespace => $args->{'namespace'});
}
if($logger) {
$logger->debug('memory cache via ', $config->{memory_cache}->{driver}, ', namespace: ', $args{'namespace'});
$logger->debug('memory cache via ', $config->{memory_cache}->{driver}, ', namespace: ', $args->{'namespace'});
}

my %chi_args = (
on_get_error => 'warn',
on_set_error => 'die',
driver => $driver,
namespace => $args{'namespace'}
namespace => $args->{'namespace'}
);

if($logger) {
Expand All @@ -168,15 +168,15 @@ sub create_memory_cache {
}
$chi_args{'servers'} = \@servers;
} elsif($driver eq 'SharedMem') {
$chi_args{'shm_key'} = $args{'shm_key'} || $config->{memory_cache}->{shm_key};
if(my $shm_size = ($args{'shm_size'} || $config->{'memory_cache'}->{'shm_size'})) {
$chi_args{'shm_key'} = $args->{'shm_key'} || $config->{memory_cache}->{shm_key};
if(my $shm_size = ($args->{'shm_size'} || $config->{'memory_cache'}->{'shm_size'})) {
$chi_args{'shm_size'} = $shm_size;
}
if(my $max_size = ($args{'max_size'} || $config->{'memory_cache'}->{'max_size'})) {
if(my $max_size = ($args->{'max_size'} || $config->{'memory_cache'}->{'max_size'})) {
$chi_args{'max_size'} = $max_size;
}
} elsif(($driver ne 'Null') && ($driver ne 'Memory')) {
$chi_args{'root_dir'} = $ENV{'root_dir'} || $args{'root_dir'} || $config->{memory_cache}->{root_dir} || $config->{'root_dir'};
$chi_args{'root_dir'} = $ENV{'root_dir'} || $args->{'root_dir'} || $config->{memory_cache}->{root_dir} || $config->{'root_dir'};
throw Error::Simple('root_dir is not optional') unless($chi_args{'root_dir'});
if($logger) {
$logger->debug("root_dir: $chi_args{root_dir}");
Expand All @@ -191,6 +191,39 @@ sub create_memory_cache {
return CHI->new(%chi_args);
}

=head2 get_params
Parse the arguments given to a function,
allowing the caller to call the function in anyway that they want e.g. foo('bar'), foo(arg => 'bar'), foo({ arg => 'bar' })
all mean the same when called _get_params('arg', @_);
=cut

sub get_params
{
my $default = shift;

# Directly return hash reference if the first parameter is a hash reference
return $_[0] if ref $_[0] eq 'HASH';

my %rc;
my $num_args = scalar @_;

# Populate %rc based on the number and type of arguments
if(($num_args == 1) && (defined $default)) {
# %rc = ($default => shift);
return { $default => shift };
} elsif($num_args == 1) {
throw Error::Simple('Usage: ' . __PACKAGE__ . '->' . (caller(1))[3] . '()');
} elsif($num_args == 0 && defined $default) {
throw Error::Simple('Usage: ' . __PACKAGE__ . '->' . (caller(1))[3] . '($default => \$val)');
} elsif(($num_args % 2) == 0) {
%rc = @_;
}

return \%rc;
}

=head2 distance
Calculate the distance between two geographical points using latitude and longitude.
Expand Down

0 comments on commit 7c1b519

Please sign in to comment.