refactor & add test cases
parent
91c64d7b3d
commit
c3eae2a012
|
@ -1,5 +1,5 @@
|
|||
package DDG::Goodie::Week;
|
||||
# ABSTRACT: Give information about the week query typed in
|
||||
# ABSTRACT: Find the current week number or when a week began
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
|
@ -14,79 +14,67 @@ use Date::Calc qw(:all);
|
|||
primary_example_queries "what is the current week";
|
||||
secondary_example_queries "what was the 5th week of this year",
|
||||
"what was the 5th week of 1944";
|
||||
description "find the current week number or when a random week began";
|
||||
description "find the current week number or when a week began";
|
||||
name "Week";
|
||||
code_url "https://github.com/gsquire/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Week.pm";
|
||||
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Week.pm";
|
||||
category "dates";
|
||||
topics "everyday", "special_interest";
|
||||
attribution twitter => "garrettsquire",
|
||||
github => "gsquire";
|
||||
github => "gsquire";
|
||||
|
||||
triggers start => "what is the current", "what was the";
|
||||
|
||||
handle remainder => sub {
|
||||
|
||||
my %months = (
|
||||
1 => "January",
|
||||
2 => "February",
|
||||
3 => "March",
|
||||
4 => "April",
|
||||
5 => "May",
|
||||
6 => "June",
|
||||
7 => "July",
|
||||
8 => "August",
|
||||
9 => "September",
|
||||
10 => "October",
|
||||
11 => "November",
|
||||
12 => "December"
|
||||
);
|
||||
|
||||
my $cur_st = " week of this year"; # For regex options
|
||||
my $var_st = " week of ";
|
||||
|
||||
my $input = $_; # Named variables are better
|
||||
my $dt = DateTime->now(time_zone => "local");
|
||||
|
||||
if ($input eq "week") {
|
||||
return "We are in currently in the " . ordinate($dt->week_number) .
|
||||
" week of " . $dt->year();
|
||||
}
|
||||
|
||||
elsif ($input =~ /(\d{1,2})(?:rd|nd|st|th)$cur_st/) {
|
||||
|
||||
my $year = $dt->year();
|
||||
my $week_num = $1;
|
||||
|
||||
return if $week_num > 52;
|
||||
|
||||
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
|
||||
|
||||
return "The " . ordinate($week_num) . " week of $year began on " .
|
||||
$months{$month} . " " . ordinate($day);
|
||||
|
||||
}
|
||||
|
||||
elsif ($input =~ /(\d{1,2})(?:rd|nd|st|th)$var_st(\d{1,4})/) {
|
||||
|
||||
my $week_num = $1;
|
||||
my $year = $2;
|
||||
|
||||
return if $week_num > 52;
|
||||
|
||||
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
|
||||
|
||||
return "The " . ordinate($week_num) . " week of $year began on " .
|
||||
$months{$month} . " " . ordinate($day);
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
};
|
||||
triggers any => 'week';
|
||||
|
||||
zci is_cached => 1;
|
||||
zci answer_type => "week";
|
||||
|
||||
my @months = qw/
|
||||
January
|
||||
February
|
||||
March
|
||||
April
|
||||
May
|
||||
June
|
||||
July
|
||||
August
|
||||
September
|
||||
October
|
||||
November
|
||||
December
|
||||
/;
|
||||
|
||||
handle query_raw => sub {
|
||||
return unless /
|
||||
what(?:'?s|\sis|\swas)\s
|
||||
the\s
|
||||
(current|(\d{1,2})(?:nd|th|rd|st)?)\s
|
||||
week
|
||||
(
|
||||
\sof\s
|
||||
(?:(?:the|this)\s)?
|
||||
(year|\d{4})
|
||||
)?\??
|
||||
/x;
|
||||
|
||||
my $week = $1;
|
||||
my $year = defined $4 ? ($4 eq 'year' ? 'current' : $4) : 'current';
|
||||
|
||||
return if $week =~ s/(nd|th|rd|st)$// and $week > 52;
|
||||
|
||||
my $dt = DateTime->now(time_zone => $loc->time_zone);
|
||||
|
||||
if ($week eq 'current' and $year eq 'current') {
|
||||
return "We are in currently in the " . ordinate($dt->week_number) .
|
||||
" week of $dt->year";
|
||||
} elsif ($year eq 'current') {
|
||||
$year = $dt->year();
|
||||
my (undef, $month, $day) = Monday_of_Week($week, $year);
|
||||
return "The " . ordinate($week) . " week of $year began on " .
|
||||
"$months[--$month] " . ordinate($day);
|
||||
} else {
|
||||
my (undef, $month, $day) = Monday_of_Week($week, $year);
|
||||
return "The " . ordinate($week) . " week of $year began on " .
|
||||
"$months[--$month] " . ordinate($day);
|
||||
}
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
6
t/Week.t
6
t/Week.t
|
@ -18,6 +18,12 @@ ddg_goodie_test(
|
|||
"what is the current week" =>
|
||||
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
|
||||
|
||||
"what's the current week?" =>
|
||||
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
|
||||
|
||||
"whats the current week of the year" =>
|
||||
test_zci(qr/We are in currently in the \d+\w+ week of \d+/),
|
||||
|
||||
"what was the 5th week of this year" =>
|
||||
test_zci(qr/The \d+\w+ week of \d+ began on January \d+\w+/),
|
||||
|
||||
|
|
Loading…
Reference in New Issue