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

Commit

Permalink
Merge pull request #384 from duckduckgo/mintsoft/blood
Browse files Browse the repository at this point in the history
Blood Donor Goodie Continuation
  • Loading branch information
Jag Talon committed Jun 30, 2014
2 parents 82ec048 + 781c7bb commit 6f34db5
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/DDG/Goodie/BloodDonor.pm
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;
20 changes: 20 additions & 0 deletions share/goodie/blood_donor/style.css
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;
}
34 changes: 34 additions & 0 deletions t/BloodDonor.t
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;

0 comments on commit 6f34db5

Please sign in to comment.