PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DDG/Goodie/CurrencyIn.pm

https://gitlab.com/0072016/zeroclickinfo-goodies
Perl | 90 lines | 50 code | 22 blank | 18 comment | 7 complexity | 5e4435e79d7c5abc5ccfb832641514a8 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 DDG::Goodie;
  17. use Locale::SubCountry;
  18. zci is_cached => 1;
  19. zci answer_type => "currency_in";
  20. primary_example_queries 'currency in australia';
  21. secondary_example_queries 'currency in AU';
  22. description 'find the official currency of a country';
  23. name 'CurrencyIn';
  24. code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/CurrencyIn.pm';
  25. category 'facts';
  26. topics 'travel';
  27. attribution github => ['http://github.com/Alchymista', 'Alchymista'];
  28. triggers any => 'currency', 'currencies'; # User typed currency...
  29. # Countries are lowercased but input from user, too ... so those always match...
  30. # ...country is capitalized on output...
  31. my %countries = share('currency.txt')->slurp;
  32. sub clear_country_name {
  33. my $txt = shift;
  34. $txt =~ s/^\?$|\?$//g; # Query may end with "?". If so take it away.
  35. $txt =~ s/^\s+|\s+$//g; # Trim spaces before and after the country name
  36. $txt;
  37. }
  38. handle remainder => sub {
  39. if (/^.*(?:in|of|for)(?:\sthe)?\s(.*?)$/i) {
  40. my $country = clear_country_name(lc($1)); # Clear country name - white spaces, question mark..
  41. # handle two-letter country codes
  42. if ( $country =~ /^[a-z]{2}$/i ) {
  43. my $loc;
  44. eval { $loc = Locale::SubCountry->new(uc($country)) };
  45. return if $@ || !$loc;
  46. $country = lc($loc->country);
  47. }
  48. if (exists $countries{$country."\n"}){
  49. my $string_currency = $countries{$country."\n"}; # Load currencies as string (one line from .txt)
  50. my @currencies = split(',', $string_currency); # Split currencies into array
  51. my $count = $#currencies + 1; # Get number of currencies
  52. my $output_country = $country; # Pass country name to the output_country
  53. $output_country =~ s/\b(\w)/\U$1/g; # so it can by capitalized
  54. my $result = $count == 1 ? "The currency in $output_country is the " : "Currencies in $output_country are: \n";
  55. # Append result with all currencies
  56. for (@currencies) {
  57. chomp;
  58. $result .= "$_\n";
  59. }
  60. chomp $result;
  61. my $html = $result;
  62. $html =~ s|\n|<br/>|g;
  63. return $result, html=>$html;
  64. }
  65. }
  66. return;
  67. };
  68. 1;