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

79 lines
2.3 KiB
Perl
Raw Normal View History

2013-11-14 07:00:38 -08:00
package DDG::Goodie::BloodDonor;
# ABSTRACT: Returns available donors for a blood type
use strict;
2013-11-14 07:00:38 -08:00
use DDG::Goodie;
use strict;
use warnings;
2014-10-28 11:36:15 -07:00
triggers startend => 'donor compatibility', 'donor', 'donors for',
'blood donor', 'blood donors for', 'blood donor for',
'blood type', 'blood compatibility', 'compatibility', 'blood donor compatibility';
2013-11-14 07:00:38 -08:00
zci answer_type => "blood_donor";
zci is_cached => 1;
2013-11-14 07:00:38 -08:00
primary_example_queries 'donor O+';
2013-11-14 07:00:38 -08:00
secondary_example_queries 'donor AB+';
description 'Donor types for a given blood type';
2013-11-14 07:00:38 -08:00
name 'BloodDonor';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/BloodDonor.pm';
2013-11-14 07:00:38 -08:00
category 'special';
topics 'everyday';
attribution github => ['https://github.com/faraday', 'faraday'];
my %typeMap = (
'A' => 'A,O',
'O' => 'O',
'AB' => 'AB,A,B,O',
'B' => 'B,O',
);
sub table_data {
my ($label, $value) = @_;
return "<tr><td class='text--secondary'>$label</td><td class='text--primary'>$value</td></tr>";
}
2013-11-14 07:00:38 -08:00
handle remainder => sub {
if ($_ =~ /^(O|A|B|AB)(\-|\+)$/i) {
my $type = uc $1;
my $rh = $2;
2013-11-14 07:00:38 -08:00
my @idealResults = ();
my @criticalResults = ();
2013-11-14 07:00:38 -08:00
return unless defined $typeMap{$type};
2014-10-28 11:36:15 -07:00
# ideally same Rh
foreach our $donorType (split(",", $typeMap{$type})) {
push(@idealResults, $donorType . $rh);
if($rh eq '+') {
# only when access to same Rh is impossible
push(@criticalResults, $donorType . '-');
}
}
2013-11-14 07:00:38 -08:00
my $output = '';
my $html = "<table class='blooddonor'>";
2014-10-28 11:36:15 -07:00
my $idealStr = join(' or ', @idealResults);
my $criticalStr = join(' or ', @criticalResults);
2014-10-28 11:36:15 -07:00
$output .= "Ideal donor: " . uc($_) . "\n";
$output .= "Other donors: " . $idealStr . "\n";
$html .= table_data("Ideal donor:", uc($_));
$html .= table_data("Other donors:", $idealStr);
2014-10-28 11:36:15 -07:00
if($rh eq '+') {
$output .= "Only if no Rh(+) found: " . $criticalStr . "\n";
$html .= table_data("<i>Only if</i> no Rh(+) found:", $criticalStr);
}
2014-10-28 11:36:15 -07:00
$html .= '</table>';
return $output, html => $html, heading => "Donors for blood type ".uc($_);
2013-11-14 07:00:38 -08:00
}
return;
};
1;