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;
0 commit comments