PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Recipes.pm

https://github.com/robertbrook/zeroclickinfo-spice
Perl | 35 lines | 24 code | 10 blank | 1 comment | 3 complexity | 7cce61573c5d5f943a5672fbb6b18227 MD5 | raw file
  1. package DDG::Spice::Recipes;
  2. use DDG::Spice;
  3. use Text::Trim;
  4. primary_example_queries 'tofu ginger recipe';
  5. description 'Search for Recipes';
  6. name 'Recipes';
  7. code_url 'https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Recipes.pm';
  8. # removing line breaks from ingredients.txt file:
  9. my %ingredients = map { trim($_) => 0 } share('ingredients.txt')->slurp;
  10. triggers any => ('recipe', 'recipes', keys(%ingredients));
  11. spice to => 'http://api.yummly.com/v1/api/recipes?q=$1&requirePictures=true&maxResult=35&_app_id={{ENV{DDG_SPICE_YUMMLY_APPID}}}&_app_key={{ENV{DDG_SPICE_YUMMLY_APIKEY}}}&callback={{callback}}';
  12. handle query_lc => sub {
  13. if ($_ =~ s/recipes?//g) {
  14. return trim($_);
  15. }
  16. my $ingredient_count = 0;
  17. my $non_ingredient_count = 0;
  18. my @words = split(/\s/, $_);
  19. foreach my $word (@words) {
  20. exists $ingredients{$word} ? $ingredient_count++ : $non_ingredient_count++;
  21. };
  22. return $_ if $ingredient_count > 0 && $non_ingredient_count < $ingredient_count;
  23. return;
  24. };
  25. 1;