diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-02-13 12:24:34 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-02-13 12:24:34 +0000 |
commit | 0db002a977d00d0a6e11333e190de4746e009bfb (patch) | |
tree | e754e071d99d3eed7618fe32c004196a8a8f3bd3 /perllib/FixMyStreet/Geocode/Bing.pm | |
parent | 3571f96251df08dd8a9dda3ec4e1fadf30d6c63d (diff) | |
parent | 0dc3e4f2c0b6e90c2b67fab5aaf7c9c3d2b1f971 (diff) |
Merge branch 'issues/684-expire-geocache'
Diffstat (limited to 'perllib/FixMyStreet/Geocode/Bing.pm')
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bing.pm | 45 |
1 files changed, 4 insertions, 41 deletions
diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index 702e19814..c62a87767 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -7,11 +7,6 @@ package FixMyStreet::Geocode::Bing; use strict; -use Encode; -use File::Slurp; -use File::Path (); -use LWP::Simple; -use Digest::MD5 qw(md5_hex); use mySociety::Locale; @@ -34,24 +29,10 @@ sub string { $url .= '&userLocation=' . $params->{centre} if $params->{centre}; $url .= '&c=' . $params->{bing_culture} if $params->{bing_culture}; - my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/'; - my $cache_file = $cache_dir . md5_hex($url); - my $js; - if (-s $cache_file) { - $js = File::Slurp::read_file($cache_file); - } else { - $url .= '&key=' . FixMyStreet->config('BING_MAPS_API_KEY'); - $js = LWP::Simple::get($url); - $js = encode_utf8($js) if utf8::is_utf8($js); - File::Path::mkpath($cache_dir); - File::Slurp::write_file($cache_file, $js) if $js; - } - + my $js = FixMyStreet::Geocode::cache('bing', $url, 'key=' . FixMyStreet->config('BING_MAPS_API_KEY')); if (!$js) { return { error => _('Sorry, we could not parse that location. Please try again.') }; } - - $js = JSON->new->utf8->allow_nonref->decode($js); if ($js->{statusCode} ne '200') { return { error => _('Sorry, we could not find that location.') }; } @@ -88,33 +69,15 @@ sub string { } sub reverse { - my ( $latitude, $longitude, $bing_culture, $cache ) = @_; + my ( $latitude, $longitude, $bing_culture ) = @_; # Get nearest road-type thing from Bing my $key = mySociety::Config::get('BING_MAPS_API_KEY', ''); if ($key) { my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?key=$key"; $url .= '&c=' . $bing_culture if $bing_culture; - my $j; - if ( $cache ) { - my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/'; - my $cache_file = $cache_dir . md5_hex($url); - - if (-s $cache_file) { - $j = File::Slurp::read_file($cache_file); - } else { - $j = LWP::Simple::get($url); - File::Path::mkpath($cache_dir); - File::Slurp::write_file($cache_file, $j) if $j; - } - } else { - $j = LWP::Simple::get($url); - } - - if ($j) { - $j = JSON->new->utf8->allow_nonref->decode($j); - return $j; - } + my $j = FixMyStreet::Geocode::cache('bing', $url); + return $j if $j; } return undef; |