This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from duckduckgo/mintsoft/blood
Blood Donor Goodie Continuation
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package DDG::Goodie::BloodDonor; | ||
# ABSTRACT: Returns available donors for a blood type | ||
|
||
use DDG::Goodie; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
triggers startend => 'donor compatibility', 'donor', 'donors for', | ||
'blood donor', 'blood donors for', 'blood donor for', | ||
'blood type', 'blood compatibility', 'compatibility', 'blood donor compatibility'; | ||
|
||
zci answer_type => "blood_donor"; | ||
|
||
primary_example_queries 'donor O+'; | ||
secondary_example_queries 'donor AB+'; | ||
description 'Donor types for a given blood type'; | ||
name 'BloodDonor'; | ||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/BloodDonor.pm'; | ||
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 apply_css($) | ||
{ | ||
my ($html) = @_; | ||
my $css = scalar share('style.css')->slurp; | ||
return "<style type='text/css'>$css</style>\n$html"; | ||
} | ||
|
||
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; | ||
|
||
my @idealResults = (); | ||
my @criticalResults = (); | ||
|
||
return unless defined $typeMap{$type}; | ||
|
||
# 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); | ||
|
||
if($rh eq '+') { | ||
$output .= "Only if no Rh(+) found: " . $criticalStr . "\n"; | ||
$html .= table_data("<i>Only if</i> no Rh(+) found:", $criticalStr); | ||
} | ||
|
||
$html .= '</table>'; | ||
return $output, html => apply_css($html), heading => "Donors for blood type ".uc($_); | ||
} | ||
return; | ||
}; | ||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use DDG::Test::Goodie; | ||
|
||
zci answer_type => "blood_donor"; | ||
|
||
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"<style.*<table class='blooddonor'>.*</table>"s, | ||
heading => "Donors for blood type A+" | ||
), | ||
'donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n", | ||
html => qr"<style.*<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"<style.*<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"<style.*<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"<style.*<table class='blooddonor'>.*</table>"s, | ||
heading => "Donors for blood type O+" | ||
), | ||
); | ||
|
||
done_testing; |