SunInfo: Refactoring to use big regex on query

master
Rob Emery 2015-01-08 01:26:34 +00:00
parent b0551f9b74
commit 6b438acd47
2 changed files with 28 additions and 4 deletions

View File

@ -6,7 +6,7 @@ with 'DDG::GoodieRole::Dates';
with 'DDG::GoodieRole::ImageLoader';
use DateTime::Event::Sunrise;
use Data::Dump qw(dump);
zci answer_type => "sun_info";
zci is_cached => 0;
@ -34,19 +34,39 @@ my $sunset_svg = goodie_img_tag({
width => 48,
});
my $lat_lon_regex = qr/[\+\-]?[0-9]+(?:
(?:\.[0-9]+[°]?)
|(?:°?
(?:[0-9]{1,2}')?
(?:[0-9]{1,2}(?:''|"))?
)
)/x;
handle remainder => sub {
my $remainder = shift // '';
$remainder =~ s/\?//g; # Strip question marks.
return unless $remainder =~ qr/^
(?:at\s
(?<lat>$lat_lon_regex)[NS]\s
(?<lon>$lat_lon_regex)[EW]\s
)?
(?:on|for)?\s?
(?<when>$datestring_regex)?
$/xi;
# print STDERR $remainder.$/.dump($+).$/;
# die;
my ($lat, $lon, $tz) = ($loc->latitude, $loc->longitude, $loc->time_zone);
my $where = where_string();
return unless (($lat || $lon) && $tz && $where); # We'll need a real location and time zone.
my $dt;
if (!$remainder) {
$dt = DateTime->now;
} elsif ($remainder =~ /^(?:on|for)?\s*(?<when>$datestring_regex)$/) {
if($+{'when'}) {
$dt = parse_datestring_to_date($+{'when'});
}
else {
$dt = DateTime->now;
}
return unless $dt; # Also going to need to know which day.
$dt->set_time_zone($tz);

View File

@ -30,6 +30,10 @@ ddg_goodie_test(
'sunset on 2015-01-01' => test_zci(@exact),
'what time is sunrise on 2015-01-01?' => test_zci(@exact),
'January 1st, 2015 sunrise' => test_zci(@exact),
# q{sunrise at 39°57'N 75°10'W} => test_zci(@exact),
# 'sunset at 53N 10E' => test_zci(@exact),
# 'sunset at 53S 15W' => test_zci(@exact),
# 'sunset at 53N 10E on 2014-01-01' => test_zci(@exact),
'sunset for philly' => undef,
'sunrise on mars' => undef,
'sunset boulevard' => undef,