assorted updates to POTUS

moved the array outside of the handler
moved the wikipedia links to `https://`
urlencoded the link hrefs
added the trigger "president of the us"

reformatting for clarity. tests updated to match changes.
master
Dylan Lloyd 2013-07-23 13:35:54 -04:00
parent 2d2ad68a96
commit 44e4c7b157
3 changed files with 85 additions and 78 deletions

View File

@ -46,6 +46,7 @@ Digest::SHA = 5.82
Math::Prime::Util = 0.26
Games::Sudoku::Component = 0.02
Data::RandomPerson = 0.4
URI::Escape = 3.31
[Prereqs / TestRequires]
Test::More = 0.98

View File

@ -2,11 +2,13 @@ package DDG::Goodie::POTUS;
# ABSTRACT: Returns requested President of the United States
use DDG::Goodie;
use Scalar::Util qw(looks_like_number);
use Lingua::EN::Numbers::Ordinate;
use URI::Escape;
triggers start => 'POTUS', 'potus';
triggers any => 'president of the united states';
triggers start => 'potus';
triggers any => 'president of the united states', 'president of the us';
zci is_cached => 1;
name 'POTUS';
description 'returns the President of the United States';
@ -17,76 +19,68 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD
attribution github => ['https://github.com/numbertheory', 'John-Peter Etcheber'],
twitter => ['http://twitter.com/jpscribbles', 'John-Peter Etcheber'];
#For maintenance, just add the president to the end of this array
my @presidents = (
"George Washington",
"John Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
"John Quincy Adams",
"Andrew Jackson",
"Martin Van Buren",
"William Henry Harrison",
"John Tyler",
"James K. Polk",
"Zachary Taylor",
"Millard Fillmore",
"Franklin Pierce",
"James Buchanan",
"Abraham Lincoln",
"Andrew Johnson",
"Ulysses S. Grant",
"Rutherfod B. Hayes",
"James A. Garfield",
"Chester A. Arthur",
"Grover Cleveland",
"Benjamin Harrison",
"Grover Cleveland",
"William McKinley",
"Theodore Roosevelt",
"William Howard Taft",
"Woodrow Wilson",
"Warren G. Harding",
"Calvin Coolidge",
"Herbert Hoover",
"Franklin D. Roosevelt",
"Harry S. Truman",
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H.W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama",
);
handle remainder => sub {
#For maintenance, just add the president to the end of this array
my @prez = (
"George Washington",
"John Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
"John Quincy Adams",
"Andrew Jackson",
"Martin Van Buren",
"William Henry Harrison",
"John Tyler",
"James K. Polk",
"Zachary Taylor",
"Millard Fillmore",
"Franklin Pierce",
"James Buchanan",
"Abraham Lincoln",
"Andrew Johnson",
"Ulysses S. Grant",
"Rutherfod B. Hayes",
"James A. Garfield",
"Chester A. Arthur",
"Grover Cleveland",
"Benjamin Harrison",
"Grover Cleveland",
"William McKinley",
"Theodore Roosevelt",
"William Howard Taft",
"Woodrow Wilson",
"Warren G. Harding",
"Calvin Coolidge",
"Herbert Hoover",
"Franklin D. Roosevelt",
"Harry S. Truman",
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H.W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama",
);
my $num = $_;
$num =~ s/\D+//g;
if (looks_like_number($num) == 0) {
$num = scalar @prez;
}
#Don't use negative numbers
if ($num < 1) {
return;
}
#Don't answer who is the 300th president of the United States
if ($num > scalar @prez) {
return;
}
$num = $num - 1;
my $verb = " was";
if (($num+1) == scalar @prez){$verb = " is";}
return $prez[$num], html=> "<a href=\"http://en.wikipedia.org/wiki/". $prez[$num] ."\">" . $prez[$num] . "</a>" . $verb . " the " . ordinate($num+1) . " President of the United States.";
return;
s/\D+//g;
my $num = $_;
$num = scalar @presidents if not $num;
return if --$num < 0 or $num > scalar @presidents;
my $fact = ($num + 1 == scalar @presidents ? 'is' : 'was')
. " the " . ordinate($num + 1) . " President of the United States.";
my $link = '<a href="https://en.wikipedia.org/wiki/'
. uri_escape($presidents[$num]) .'">'
. $presidents[$num] . '</a>';
return "$presidents[$num] $fact", html => "$link $fact";
};
zci is_cached => 1;
1;

View File

@ -12,12 +12,24 @@ ddg_goodie_test(
[qw(
DDG::Goodie::POTUS
)],
'who is president of the united states' => test_zci('Barack Obama', html => '<a href="http://en.wikipedia.org/wiki/Barack Obama">Barack Obama</a> is the 44th President of the United States.'),
'who was the 1st president of the united states' => test_zci('George Washington', html => '<a href="http://en.wikipedia.org/wiki/George Washington">George Washington</a> was the 1st President of the United States.'),
'who was the 31 president of the united states' => test_zci('Herbert Hoover', html => '<a href="http://en.wikipedia.org/wiki/Herbert Hoover">Herbert Hoover</a> was the 31st President of the United States.'),
'who was the 22 president of the united states' => test_zci('Grover Cleveland', html => '<a href="http://en.wikipedia.org/wiki/Grover Cleveland">Grover Cleveland</a> was the 22nd President of the United States.'),
'potus 11' => test_zci('James K. Polk', html => '<a href="http://en.wikipedia.org/wiki/James K. Polk">James K. Polk</a> was the 11th President of the United States.'),
'potus 24', => test_zci('Grover Cleveland', html => '<a href="http://en.wikipedia.org/wiki/Grover Cleveland">Grover Cleveland</a> was the 24th President of the United States.'),
'who is president of the united states' => test_zci(
'Barack Obama is the 44th President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/Barack%20Obama">Barack Obama</a> is the 44th President of the United States.'),
'who was the 1st president of the united states' => test_zci(
'George Washington was the 1st President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/George%20Washington">George Washington</a> was the 1st President of the United States.'),
'who was the 31 president of the united states' => test_zci(
'Herbert Hoover was the 31st President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/Herbert%20Hoover">Herbert Hoover</a> was the 31st President of the United States.'),
'who was the 22 president of the united states' => test_zci(
'Grover Cleveland was the 22nd President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/Grover%20Cleveland">Grover Cleveland</a> was the 22nd President of the United States.'),
'potus 11' => test_zci(
'James K. Polk was the 11th President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/James%20K.%20Polk">James K. Polk</a> was the 11th President of the United States.'),
'POTUS 24', => test_zci(
'Grover Cleveland was the 24th President of the United States.',
html => '<a href="https://en.wikipedia.org/wiki/Grover%20Cleveland">Grover Cleveland</a> was the 24th President of the United States.'),
);