aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHakim Cassimally <hakim@mysociety.org>2015-04-24 14:43:25 +0000
committerDave Arter <davea@mysociety.org>2015-10-06 09:09:23 +0100
commitef096dbe0fb71a00aaa23b69f7077ea999ae7116 (patch)
tree6138eb39e557970accc6e0d3f2a0cd92cba93df2
parentb00679b290fe7e326a8f9960e33db43b78490bde (diff)
Improvements to bin/geocode
- Fix behaviour of ALLOWED_COBRANDS checking - ... inferring cobrand if only one is allowed - new --use-cache option (defaulting to false) - better diagnostics - Can be manually uncommented and tested against bin/geocode script. See mysociety/FixMyStreet-Commercial#710
-rwxr-xr-xbin/geocode50
-rw-r--r--perllib/FixMyStreet/Geocode/Zurich.pm11
2 files changed, 52 insertions, 9 deletions
diff --git a/bin/geocode b/bin/geocode
index 2559f7a3c..f6bf79149 100755
--- a/bin/geocode
+++ b/bin/geocode
@@ -12,10 +12,23 @@ geocode - commandline tool to test geocoders
$ bin/geocode --cobrand=bromley "Glebe Rd"
# ... if you want to use config that you have in conf/general.bing.yml
- $ bin/geocode --override-config=general.bing --cobrand=bromley "Glebe Rd"
+ $ bin/geocode --override-config=general.yml.bing --cobrand=bromley "Glebe Rd"
+
+ # ... if your config only has a single entry in ALLOWED_COBRANDS
+ $ bin/geocode --override-config=general.yml.zurich "Im eisernen Zeit"
## ... output from geocoder
+=head1 OPTIONS
+
+ --help|h show this help
+ --use-cache use the configured GEO_CACHE (default OFF)
+
+ # the following options take a string argument:
+ --geocoder e.g. OSM/Bing/Google/Zurich
+ --cobrand (default, bromley, zurich etc.)
+ --override-config e.g. conf/general.yml
+
=cut
use strict;
@@ -32,6 +45,7 @@ BEGIN {
use Data::Dumper;
use Pod::Usage;
use feature 'say';
+use File::Temp 'tempdir';
use Getopt::Long;
@@ -41,13 +55,12 @@ GetOptions \%options,
'geocoder=s',
'help|h',
'cobrand=s',
- 'override-config=s';
+ 'override-config=s',
+ 'use-cache';
my $s = join ' ', @ARGV
or pod2usage(0);
-pod2usage(0) unless $options{cobrand};
-
local $ENV{FMS_OVERRIDE_CONFIG} = $options{'override-config'} if $options{'override-config'};
eval 'use FixMyStreet';
@@ -63,22 +76,41 @@ my $geocoder_type = $options{geocoder} || do {
} or pod2usage(0);
my $geocoder_name = "FixMyStreet::Geocode::${geocoder_type}";
+chomp $geocoder_name;
my $code_ref = $geocoder_name->can('string')
or die "$geocoder_name is not a valid geocoder?";
-my @allowed_cobrands = FixMyStreet::Cobrand->get_allowed_cobrands();
+my @allowed_cobrands = @{ FixMyStreet::Cobrand->get_allowed_cobrands() };
+
+my $cobrand_option = $options{cobrand}
+ || do {
+ $allowed_cobrands[0]->{moniker} if scalar @allowed_cobrands == 1;
+ } or pod2usage(0);
-my $cobrand_name = FixMyStreet::Cobrand->get_class_for_moniker($options{cobrand});
+my $cobrand_name = FixMyStreet::Cobrand->get_class_for_moniker($cobrand_option);
my $cobrand = $cobrand_name->new();
say "USING COBRAND $cobrand_name";
-if ($cobrand->moniker ne lc($options{cobrand})) {
- say "!!! asked for $options{cobrand}";
+if ($cobrand->moniker ne lc($cobrand_option)) {
+ say "!!! asked for $cobrand_option";
say "!!! Check ALLOWED_COBRANDS setting in conf/general.yml (or supplied --override-config file)";
say Dumper(\@allowed_cobrands);
}
+say "USING GEOCODER $geocoder_name "
+ . ( $options{'use-cache'} ?
+ 'WITH ' . FixMyStreet->config('GEO_CACHE') :
+ 'WITHOUT cache');
+
my $c = FixMyStreet::App->new();
$c->stash->{cobrand} = $cobrand;
-say Dumper( $code_ref->( $s, $c ) );
+FixMyStreet::override_config({
+ # if we're not using cache, then set GEO_CACHE to a temporary directory
+ $options{'use-cache'} ?
+ () :
+ ( GEO_CACHE => (tempdir( CLEANUP => 1 ) . '/') ),
+}, sub {
+ my $result = $code_ref->( $s, $c );
+ say Dumper $result;
+});
diff --git a/perllib/FixMyStreet/Geocode/Zurich.pm b/perllib/FixMyStreet/Geocode/Zurich.pm
index aad918b0e..50a7c355e 100644
--- a/perllib/FixMyStreet/Geocode/Zurich.pm
+++ b/perllib/FixMyStreet/Geocode/Zurich.pm
@@ -31,6 +31,7 @@ sub setup_soap {
my $action = "$attr/IFixMyZuerich/";
require SOAP::Lite;
+ # SOAP::Lite->import( +trace => [transport => \&log_message ] );
# Set up the SOAP handler
$security = SOAP::Header->name("Security")->attr({
@@ -109,5 +110,15 @@ sub string {
return { error => $error };
}
+sub log_message {
+ my ($in) = @_;
+ eval {
+ printf "log_message [$in]: %s\n\n", $in->content; # ...for example
+ };
+ if ($@) {
+ print "log_message [$in]: ???? \n\n";
+ }
+}
+
1;