My first plugin called Week.pm

master
gsquire 2013-08-10 13:11:30 -07:00
parent 21f4cbb94b
commit e72db7c2bc
2 changed files with 88 additions and 0 deletions

59
lib/DDG/Goodie/Week.pm Normal file
View File

@ -0,0 +1,59 @@
package DDG::Goodie::Week;
# ABSTRACT: Give information about the week query typed in
use DDG::Goodie;
# My imports
use strict;
use warnings;
use DateTime; # Already in dist.ini
use Date::Calc qw(:all); # Already in dist.ini
triggers start => "week";
handle remainder => sub {
my $input = $_; # Named variables are better
my $dt = DateTime->now(time_zone => "local"); # Local to sub
if ($input eq "current") {
return "We are in week number " . $dt->week_number;
}
elsif ($input =~ /^(\d{1,2})$/) {
my $year = $dt->year();
my $week_num = $1;
return if $week_num > 52;
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
return "Week $week_num started on $month-$day in $year";
}
elsif ($input =~ /^(\d{1,2})\s+(\d{4})$/) {
my $week_num = $1;
my $year = $2;
return if $week_num > 52;
my (undef, $month, $day) = Monday_of_Week($week_num, $year);
return "Week $week_num started on $month-$day of $year";
}
else {
return;
}
};
zci is_cached => 1;
1;

29
t/Week.t Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "week";
zci is_cached => 1;
# Output verified with UNIX cal program
ddg_goodie_test(
[qw(
DDG::Goodie::Week
)],
# "week current" => test_zci("We are in week number 32"),
# "week 6" => test_zci("Week 6 started on 2-4 in 2013"),
"week 43 1984" => test_zci("Week 43 started on 10-22 of 1984"),
"week 8 1956" => test_zci("Week 8 started on 2-20 of 1956"),
"week 21 1987" => test_zci("Week 21 started on 5-18 of 1987"),
);
done_testing;