Skip to content
This repository was archived by the owner on Oct 15, 2022. It is now read-only.

Commit 6f34db5

Browse files
author
Jag Talon
committed
Merge pull request #384 from duckduckgo/mintsoft/blood
Blood Donor Goodie Continuation
2 parents 82ec048 + 781c7bb commit 6f34db5

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

lib/DDG/Goodie/BloodDonor.pm

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package DDG::Goodie::BloodDonor;
2+
# ABSTRACT: Returns available donors for a blood type
3+
4+
use DDG::Goodie;
5+
6+
use strict;
7+
use warnings;
8+
9+
triggers startend => 'donor compatibility', 'donor', 'donors for',
10+
'blood donor', 'blood donors for', 'blood donor for',
11+
'blood type', 'blood compatibility', 'compatibility', 'blood donor compatibility';
12+
13+
zci answer_type => "blood_donor";
14+
15+
primary_example_queries 'donor O+';
16+
secondary_example_queries 'donor AB+';
17+
description 'Donor types for a given blood type';
18+
name 'BloodDonor';
19+
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/BloodDonor.pm';
20+
category 'special';
21+
topics 'everyday';
22+
attribution github => ['https://github.com/faraday', 'faraday'];
23+
24+
my %typeMap = (
25+
'A' => 'A,O',
26+
'O' => 'O',
27+
'AB' => 'AB,A,B,O',
28+
'B' => 'B,O',
29+
);
30+
31+
sub apply_css($)
32+
{
33+
my ($html) = @_;
34+
my $css = scalar share('style.css')->slurp;
35+
return "<style type='text/css'>$css</style>\n$html";
36+
}
37+
38+
sub table_data {
39+
my ($label, $value) = @_;
40+
return "<tr><td class='text--secondary'>$label</td><td class='text--primary'>$value</td></tr>";
41+
}
42+
43+
handle remainder => sub {
44+
if ($_ =~ /^(O|A|B|AB)(\-|\+)$/i) {
45+
my $type = uc $1;
46+
my $rh = $2;
47+
48+
my @idealResults = ();
49+
my @criticalResults = ();
50+
51+
return unless defined $typeMap{$type};
52+
53+
# ideally same Rh
54+
foreach our $donorType (split(",", $typeMap{$type})) {
55+
push(@idealResults, $donorType . $rh);
56+
if($rh eq '+') {
57+
# only when access to same Rh is impossible
58+
push(@criticalResults, $donorType . '-');
59+
}
60+
}
61+
62+
my $output = '';
63+
my $html = "<table class='blooddonor'>";
64+
65+
my $idealStr = join(' or ', @idealResults);
66+
my $criticalStr = join(' or ', @criticalResults);
67+
68+
$output .= "Ideal donor: " . uc($_) . "\n";
69+
$output .= "Other donors: " . $idealStr . "\n";
70+
$html .= table_data("Ideal donor:", uc($_));
71+
$html .= table_data("Other donors:", $idealStr);
72+
73+
if($rh eq '+') {
74+
$output .= "Only if no Rh(+) found: " . $criticalStr . "\n";
75+
$html .= table_data("<i>Only if</i> no Rh(+) found:", $criticalStr);
76+
}
77+
78+
$html .= '</table>';
79+
return $output, html => apply_css($html), heading => "Donors for blood type ".uc($_);
80+
}
81+
return;
82+
};
83+
1;

share/goodie/blood_donor/style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.zci--answer .blooddonor
2+
{
3+
font-size: 1.1em;
4+
line-height: 1.5em;
5+
}
6+
7+
.zci--answer .blooddonor .text--secondary
8+
{
9+
padding-right: 1em;
10+
}
11+
12+
.zci--answer .blooddonor tr
13+
{
14+
border-top: 1px solid rgba(155,155,155,.15);
15+
}
16+
17+
.zci--answer .blooddonor tr:nth-of-type(1)
18+
{
19+
border-top: 0px;
20+
}

t/BloodDonor.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use Test::More;
6+
use DDG::Test::Goodie;
7+
8+
zci answer_type => "blood_donor";
9+
10+
ddg_goodie_test(
11+
['DDG::Goodie::BloodDonor'],
12+
'donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
13+
html => qr"<style.*<table class='blooddonor'>.*</table>"s,
14+
heading => "Donors for blood type A+"
15+
),
16+
'donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
17+
html => qr"<style.*<table class='blooddonor'>.*</table>"s,
18+
heading => "Donors for blood type A+"
19+
),
20+
'blood donor A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
21+
html => qr"<style.*<table class='blooddonor'>.*</table>"s,
22+
heading => "Donors for blood type A+"
23+
),
24+
'blood donors for A+' => test_zci("Ideal donor: A+\nOther donors: A+ or O+\nOnly if no Rh(+) found: A- or O-\n",
25+
html => qr"<style.*<table class='blooddonor'>.*</table>"s,
26+
heading => "Donors for blood type A+"
27+
),
28+
'donor o+' => test_zci("Ideal donor: O+\nOther donors: O+\nOnly if no Rh(+) found: O-\n",
29+
html => qr"<style.*<table class='blooddonor'>.*</table>"s,
30+
heading => "Donors for blood type O+"
31+
),
32+
);
33+
34+
done_testing;

0 commit comments

Comments
 (0)