/lib/DDG/Goodie/CurrencyIn.pm

https://github.com/zoracon/zeroclickinfo-goodies · Perl · 91 lines · 51 code · 22 blank · 18 comment · 7 complexity · cd7171102e088bc1a5c4d8ab45557b7a MD5 · raw file

  1. package DDG::Goodie::CurrencyIn;
  2. # ABSTRACT: Return currency type(s) in given country
  3. # TODO: At the moment it return value only if user inputs the whole country name...
  4. # ...if user types "Salvador" instead of "El Salvador" then no results...
  5. # TODO: think about how often currency in countries changes?
  6. # Parser (for Wikipedia) is included in share directory...
  7. # In some countries there are more than one currency.
  8. # For that reason values in hash are stored as arrays. (loaded from .txt as comma separated values )
  9. # Example: %countries( "Zimbabwe"=>["A","B"], "Slovakia"=>["A"], ... )"
  10. # Working examples for queries:
  11. # What currency do I need in Egypt ?
  12. # What currency will I need for Zimbabwe
  13. # What is the currency used in Slovakia
  14. # currency in Russia
  15. # What type of currency do I need for Russia?
  16. use strict;
  17. use DDG::Goodie;
  18. use Locale::SubCountry;
  19. use Text::Trim;
  20. zci is_cached => 1;
  21. zci answer_type => "currency_in";
  22. primary_example_queries 'currency in australia';
  23. secondary_example_queries 'currency in AU';
  24. description 'find the official currency of a country';
  25. name 'CurrencyIn';
  26. code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/CurrencyIn.pm';
  27. category 'facts';
  28. topics 'travel';
  29. attribution github => ['http://github.com/Alchymista', 'Alchymista'];
  30. triggers any => 'currency', 'currencies'; # User typed currency...
  31. # Countries are lowercased but input from user, too ... so those always match...
  32. # ...country is capitalized on output...
  33. my %countries = share('currency.txt')->slurp;
  34. sub clear_country_name {
  35. my $txt = shift;
  36. $txt =~ s/^\?$|\?$//g; # Query may end with "?". If so take it away.
  37. return trim $txt;
  38. }
  39. handle remainder => sub {
  40. if (/^.*(?:in|of|for)(?:\sthe)?\s(.*?)$/i) {
  41. my $country = clear_country_name(lc($1)); # Clear country name - white spaces, question mark..
  42. # handle two-letter country codes
  43. if ( $country =~ /^[a-z]{2}$/i ) {
  44. my $loc;
  45. eval { $loc = Locale::SubCountry->new(uc($country)) };
  46. return if $@ || !$loc;
  47. $country = lc($loc->country);
  48. }
  49. if (exists $countries{$country."\n"}){
  50. my $string_currency = $countries{$country."\n"}; # Load currencies as string (one line from .txt)
  51. my @currencies = split(',', $string_currency); # Split currencies into array
  52. my $count = $#currencies + 1; # Get number of currencies
  53. my $output_country = $country; # Pass country name to the output_country
  54. $output_country =~ s/\b(\w)/\U$1/g; # so it can by capitalized
  55. my $result = $count == 1 ? "The currency in $output_country is the " : "Currencies in $output_country are: \n";
  56. # Append result with all currencies
  57. for (@currencies) {
  58. chomp;
  59. $result .= "$_\n";
  60. }
  61. chomp $result;
  62. my $html = $result;
  63. $html =~ s|\n|<br/>|g;
  64. return $result, html=>$html;
  65. }
  66. }
  67. return;
  68. };
  69. 1;