zeroclickinfo-goodies/lib/DDG/Goodie/UN.pm

33 lines
831 B
Perl
Raw Normal View History

2012-05-05 14:14:07 -07:00
package DDG::Goodie::UN;
# ABSTRACT: Gives a description for a given UN number
use DDG::Goodie;
use Number::UN 'get_un';
use constant WPHREF => "http://en.wikipedia.org/wiki/List_of_UN_numbers_%04d_to_%04d";
triggers start => 'un';
zci is_cached => 1;
2012-05-05 20:20:45 -07:00
zci answer_type => 'united_nations';
2012-05-05 14:14:07 -07:00
handle remainder => sub {
my $num = shift or return;
2012-05-09 07:37:05 -07:00
$num =~ s/^number\s+//gi;
return unless $num =~ /^\d+$/;
2012-05-09 07:37:05 -07:00
2012-05-05 14:14:07 -07:00
my %un = get_un($num) or return;
2012-05-09 07:32:46 -07:00
$un{description} =~ s/\.$//;
return (sprintf qq(UN Number %04d - %s.), $num, $un{description}), html => sprintf qq(<a href="%s">UN Number %04d</a> - %s.), wphref($num), $num, $un{description};
2012-05-05 14:14:07 -07:00
};
# Wikipedia attribution per CC-BY-SA
sub wphref {
my $num = shift;
my $lower = int($num / 100) * 100 + 1;
my $upper = $lower + 99;
return sprintf WPHREF, $lower, $upper;
}
1;