Problem of the Day
A new programming or logic puzzle every Mon-Fri

We Hungry

Along with music one of the things we all do on a daily basis is eat. Eatting the same ole thing day in and day out can get kind of boring. Let's see if we can prevent that from happening with a Problem of the Day suggestion courtesy of Reddit user w1ndwak3r.

Today's goal is given a recipe name, find a set of ingredients for that recipe. There are several great APIs out there to help you with this. One that I found helpful is Food2Fork's API. Feel free to use any means you'd like to obtain the data including writing a database of your own recipes! Here is an API key (limited to 500 requests/day) for Food2Fork and some sample queries to get you started.

Permalink: http://problemotd.com/problem/we-hungry/

Comments:

  • Anonymous - 10 years, 8 months ago

    Fun question! Here's a quick AngularJS & NodeJS solution:

    package.json

    {
        "name": "we-hungry",
        "version": "0.0.1", 
        "private": true, 
        "dependencies": {
            "express": "4.1.x", 
            "request": "2.33.x",
            "underscore": "1.6.x", 
            "winston": "0.7.x"
      }
    }
    

    app.js

    var _ = require('underscore');
    var express = require('express');
    var fs = require('fs');
    var request = require('request');
    var winston = require('winston');
    
    var app = express();
    
    app.get('/api/recipe-search', function(req, res) {
        var url = encodeURI('http://food2fork.com/api/search?key=3e9166ad629eca6587a5e501e4e30961&q=' + req.query.q);
        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                res.send(body);
            }
            else {
                res.status(404);
            }
        });
    });
    
    app.get('/api/ingredients', function(req, res) {
        var url = encodeURI('http://food2fork.com/api/get?key=3e9166ad629eca6587a5e501e4e30961&rId=' + req.query.recipe);
        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                res.send(body);
            }
            else {
                res.status(404);
            }
        });
    });
    
    app.get('/', function(req, res) {
        res.send(fs.readFileSync('index.html', "utf8"));
    });
    
    app.listen(3000, function() {
        winston.info("Server running on port 3000");
    });
    

    index.html

    <!DOCTYPE html>
    <html>
    <head>
      <title>We Hungry</title>
    
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.7/angular.min.js"></script>
      <script>
        var weHungryApp = angular.module('WeHungryApp', []);
    
        weHungryApp.controller('WeHungryController', function ($scope, $http) {
          $scope.searchForRecipes = function() {
            $scope.recipes = [];
            if ($scope.recipeSearch) {
              $http.get('/api/recipe-search?q=' + encodeURI($scope.recipeSearch)).success(function(data) {
                angular.forEach(data.recipes, function(recipe) {
                  $scope.recipes.push({ name: recipe.title, id: recipe.recipe_id, loaded: false });
                });
              });
            }
          };
    
          $scope.loadIngredients = function(recipe) {
            if (recipe) {
              $http.get('/api/ingredients?recipe=' + recipe.id).success(function(data) {
                recipe.ingredients = data.recipe.ingredients;
                recipe.loaded = true;
              });
            }
          };
    
        });
      </script>
    
    </head>
    <body ng-app="WeHungryApp" ng-controller="WeHungryController">
      <div>Search for recipes: <input ng-model="recipeSearch"><button ng-click="searchForRecipes()">Search</button></div>
      <div>
        <h3>Results:</h3>
        <ul>
          <li ng-repeat="recipe in recipes">
            {{ recipe.name }} <a href="" ng-if="!recipe.loaded" ng-click="loadIngredients(recipe)">load ingredients</a>
            <ul><li ng-repeat="ingredient in recipe.ingredients">{{ ingredient }}</li></ul>
          </li>
        </ul>
      </div>
    </body>
    </html>
    

    reply permalink

Content curated by @MaxBurstein