/lib/DDG/Spice/Recipes.pm
Perl | 35 lines | 24 code | 10 blank | 1 comment | 3 complexity | 7cce61573c5d5f943a5672fbb6b18227 MD5 | raw file
- package DDG::Spice::Recipes;
- use DDG::Spice;
- use Text::Trim;
- primary_example_queries 'tofu ginger recipe';
- description 'Search for Recipes';
- name 'Recipes';
- code_url 'https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Recipes.pm';
- # removing line breaks from ingredients.txt file:
- my %ingredients = map { trim($_) => 0 } share('ingredients.txt')->slurp;
- triggers any => ('recipe', 'recipes', keys(%ingredients));
- 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}}';
- handle query_lc => sub {
- if ($_ =~ s/recipes?//g) {
- return trim($_);
- }
- my $ingredient_count = 0;
- my $non_ingredient_count = 0;
- my @words = split(/\s/, $_);
- foreach my $word (@words) {
- exists $ingredients{$word} ? $ingredient_count++ : $non_ingredient_count++;
- };
- return $_ if $ingredient_count > 0 && $non_ingredient_count < $ingredient_count;
- return;
- };
- 1;