Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Reborn IA: Drinks #2191

Merged
merged 9 commits into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/DDG/Spice/Drinks.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package DDG::Spice::Drinks;
# ABSTRACT: Drink mixing instructions

use strict;
use DDG::Spice;
use Text::Trim;

spice is_cached => 1;

name "Drinks";
description "Bartending info";
primary_example_queries "how to make a mojito";
secondary_example_queries "what ingredients are being used within gin fizz", "long island cocktail";

code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Drinks.pm";
attribution github => ["https://github.com/mutilator", "mutilator"],
github => ["https://github.com/ozdemirburak", "Burak Özdemir"],
twitter => ["https://twitter.com/ozdemirbur", "Burak Özdemir"],
web => ["http://burakozdemir.co.uk", "Burak Özdemir"];

spice to => 'http://www.thecocktaildb.com/api/json/v1/1/search.php?s=$1';
spice wrap_jsonp_callback => 1;

triggers any => ('cocktail', 'drink', 'ingredient', 'ingredients', 'make', 'making', 'mix', 'mixing', 'recipe');

my %drinks = map { trim($_) => 0 } share('drinks.txt')->slurp;
my @stop_words = ("a", "an", "are", "being", "for", "how", "in", "is", "needed", "of", "that", "to", "used", "what", "within");
my ($rx) = map qr/(?:$_)/, join "|", map qr/\b\Q$_\E\b/, @stop_words;

# Handle statement
handle remainder_lc => sub {
$_ =~ s/$rx//g;
my $drink = trim($_);
if (exists($drinks{$drink})) {
return $drink if $drink ne "";
}
return;
};

1;
46 changes: 46 additions & 0 deletions share/spice/drinks/drinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function (env) {
"use strict";

function getInfoBoxData(item) {
var infoboxData = [{
heading: 'Ingredients:'
}];
for (var i = 1; i <= 15; i++) {
if(item["strIngredient" + i] !== "") {
infoboxData.push({
label: item["strMeasure" + i] + "" + item["strIngredient" + i]
});
}
}
return infoboxData;
}

env.ddg_spice_drinks = function(api_result){

if (!api_result || api_result.error || !api_result.drinks || api_result.drinks.length === 0) {
return Spice.failed('drinks');
}

var drink = api_result.drinks[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if drinks has any elements:

screen shot 2015-09-28 at 4 58 32 pm


Spice.add({
id: 'drinks',
data: drink,
name: "Drinks",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change this to Recipes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this 👍

meta: {
sourceUrl: "http://www.thecocktaildb.com/drink.php?c=" + drink.idDrink,
sourceName: 'TheCocktailDB'
},
normalize: function(item) {
return {
description: item.strInstructions,
title: item.strDrink,
infoboxData: getInfoBoxData(item)
};
},
templates: {
group: 'info'
}
});
};
}(this));