Merge pull request #1260 from duckduckgo/mintsoft/bloodydonors

BloodDonor: Switched to record template
master
Jag Talon 2015-07-17 09:48:13 -04:00
commit 48cb88dda2
3 changed files with 124 additions and 74 deletions

89
lib/DDG/Goodie/BloodDonor.pm Normal file → Executable file
View File

@ -30,49 +30,66 @@ my %typeMap = (
'B' => 'B,O',
);
sub table_data {
my ($label, $value) = @_;
return "<tr><td class='text--secondary'>$label</td><td class='text--primary'>$value</td></tr>";
}
handle remainder => sub {
if ($_ =~ /^(O|A|B|AB)(\-|\+)$/i) {
my $type = uc $1;
my $rh = $2;
return unless ($_ =~ /^(O|A|B|AB)(\-|\+)$/i);
my @idealResults = ();
my @criticalResults = ();
my $type = uc $1;
my $rh = $2;
return unless defined $typeMap{$type};
my @idealResults = ();
my @criticalResults = ();
# 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 . '-');
}
}
my $output = '';
my $html = "<table class='blooddonor'>";
my $idealStr = join(' or ', @idealResults);
my $criticalStr = join(' or ', @criticalResults);
$output .= "Ideal donor: " . uc($_) . "\n";
$output .= "Other donors: " . $idealStr . "\n";
$html .= table_data("Ideal donor:", uc($_));
$html .= table_data("Other donors:", $idealStr);
return unless defined $typeMap{$type};
# ideally same Rh
foreach our $donorType (split(",", $typeMap{$type})) {
push(@idealResults, $donorType . $rh);
if($rh eq '+') {
$output .= "Only if no Rh(+) found: " . $criticalStr . "\n";
$html .= table_data("<i>Only if</i> no Rh(+) found:", $criticalStr);
# only when access to same Rh is impossible
push(@criticalResults, $donorType . '-');
}
$html .= '</table>';
return $output, html => $html, heading => "Donors for blood type ".uc($_);
}
return;
my $idealStr = join(' or ', @idealResults);
my $criticalStr = join(' or ', @criticalResults);
my %record_data = (
"Ideal donor" => uc($_),
"Other donors" => $idealStr,
);
my @record_keys = ("Ideal donor", "Other donors");
if($rh eq '+') {
push @record_keys,"Only if no Rh(+) found";
$record_data{"Only if no Rh(+) found"} = $criticalStr;
}
sub to_text
{
my ($data, $keys) = @_;
return join "\n", map {"$_: $data->{$_}";} @{$keys};
}
return to_text(\%record_data, \@record_keys), structured_answer => {
id => 'blood_donor',
name => 'Blood Donors',
description => 'Returns available donors for a blood type',
meta => {
sourceName => 'Wikipedia',
sourceUrl => 'https://en.wikipedia.org/wiki/Blood_type',
},
templates => {
group => 'list',
options => {
content => 'record'
}
},
data => {
title => "Donors for blood type ".uc($_),
record_data => \%record_data,
record_keys => \@record_keys
}
};
};
1;

23
share/goodie/blood_donor/blood_donor.css Normal file → Executable file
View File

@ -1,20 +1,3 @@
.zci--answer .blooddonor
{
font-size: 1.1em;
line-height: 1.5em;
}
.zci--answer .blooddonor .text--secondary
{
padding-right: 1em;
}
.zci--answer .blooddonor tr
{
border-top: 1px solid rgba(155,155,155,.15);
}
.zci--answer .blooddonor tr:nth-of-type(1)
{
border-top: 0px;
}
.zci--blood_donor .record .record__cell--key {
width: 10.7em;
}

86
t/BloodDonor.t Normal file → Executable file
View File

@ -8,27 +8,77 @@ use DDG::Test::Goodie;
zci answer_type => "blood_donor";
zci is_cached => 1;
sub build_structure
{
my ($blood_type, $data, $keys) = @_;
return {
id => 'blood_donor',
name => 'Blood Donors',
description => 'Returns available donors for a blood type',
meta => {
sourceName => 'Wikipedia',
sourceUrl => 'https://en.wikipedia.org/wiki/Blood_type'
},
templates => {
group => 'list',
options => {
content => 'record'
}
},
data => {
title => "Donors for blood type $blood_type",
record_data => $data,
record_keys => $keys
}
};
}
ddg_goodie_test(
['DDG::Goodie::BloodDonor'],
'donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
html => qr"<table class='blooddonor'>.*</table>"s,
heading => "Donors for blood type A+"
'donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-",
structured_answer => build_structure("A+",{
"Ideal donor" => "A+",
"Other donors" => "A+ or O+",
"Only if no Rh(+) found" => "A- or O-"
},
["Ideal donor", "Other donors", "Only if no Rh(+) found"]
)
),
'donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
html => qr"<table class='blooddonor'>.*</table>"s,
heading => "Donors for blood type A+"
),
'blood donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
html => qr"<table class='blooddonor'>.*</table>"s,
heading => "Donors for blood type A+"
),
'blood donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
html => qr"<table class='blooddonor'>.*</table>"s,
heading => "Donors for blood type A+"
),
'donor o+' => test_zci("Ideal donor: O+\nOther donors: O+\nOnly if no Rh(+) found: O-\n",
html => qr"<table class='blooddonor'>.*</table>"s,
heading => "Donors for blood type O+"
'donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-",
structured_answer => build_structure("A+",{
"Ideal donor" => "A+",
"Other donors" => "A+ or O+",
"Only if no Rh(+) found" => "A- or O-"
},
["Ideal donor", "Other donors", "Only if no Rh(+) found"]
)
),
'blood donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-",
structured_answer => build_structure("A+",{
"Ideal donor" => "A+",
"Other donors" => "A+ or O+",
"Only if no Rh(+) found" => "A- or O-"
},
["Ideal donor", "Other donors", "Only if no Rh(+) found"]
)
),
'blood donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-",
structured_answer => build_structure("A+",{
"Ideal donor" => "A+",
"Other donors" => "A+ or O+",
"Only if no Rh(+) found" => "A- or O-"
},
["Ideal donor", "Other donors", "Only if no Rh(+) found"]
)
),
'donor o+' => test_zci("Ideal donor: O+\nOther donors: O+\nOnly if no Rh(+) found: O-",
structured_answer => build_structure("O+",{
"Ideal donor" => "O+",
"Other donors" => "O+",
"Only if no Rh(+) found" => "O-"
},
["Ideal donor", "Other donors", "Only if no Rh(+) found"]
)
),
);