Skip to content

Commit

Permalink
getDecision now working, just need to finish the response
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Salisbury committed Feb 10, 2017
1 parent 1c4be69 commit 3c3ce56
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
96 changes: 87 additions & 9 deletions api/routes/decision.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ function getProductIngredients($fdct_id) {
}


/** Not a route. Should not be here. Don't care. Sue me. **/


$app->get('/getProductInfo/{upc}', function($request, $response) {

$upc = $request->getAttribute('upc');
function getProductInfo($upc) {

$fdctconfig = array();
$fdctconfig['passphrase'] = "Partially Hydrogenated Vegetable";
Expand Down Expand Up @@ -94,6 +92,17 @@ function getProductIngredients($fdct_id) {
$fdct_id = $res['response']['body']['product_list']['products'][0]['@attributes']['id'];
$ingreds = getProductIngredients($fdct_id);
$res['response']['body']['product_list']['products'][0]['@attributes']['ingreds'] = $ingreds;
curl_close($ch);

return $res;
}


$app->get('/getProductInfo/{upc}', function($request, $response) {

$upc = $request->getAttribute('upc');

$res = getProductInfo($upc);



Expand All @@ -109,15 +118,84 @@ function getProductIngredients($fdct_id) {
return $response;


curl_close($ch);

});



$app->get('/getDecision/{upc}/{category}', function($request, $response) {

// get the ingredients by UPC
// get the rules by category
// get the nutrients by UPC
$prodInfo = getProductInfo($request->getAttribute('upc'));
$nutrients = $prodInfo['response']['body']['product_list']['products'][0]['product_nutrient_list']['product_nutrients'];

// get the whittled down nutrtient info shittily
$theNuts = array();

foreach ($nutrients as $nutrient) {
$theNuts[$nutrient['nutrient']['@attributes']['name']] = $nutrient['@attributes']['value'];
}

echo "<h2>The Product has...</h2>";
echo "<pre>";
print_r($theNuts);
echo "</pre>";

// get the rules by category
$q = new CategoryRankNutrientQuery;
$allRules = $q->filterByCategoryId($request->getAttribute('category'))->find();
$passedRank = "Red";

// for each rule, evaluate ingredients
foreach($allRules as $rule) {
$nut = $rule->getNutrient()->getName();
$operator = $rule->getOperator();
$threshold = $rule->getThreshold();
$presentVal = $theNuts[$nut];
$pass = 0;
switch($operator) {
case "lte":
$pass = ($presentVal <= $threshold);
break;

case "lt":
$pass = ($presentVal < $threshold);
break;

case "gt":
$pass = ($presentVal > $threshold);
break;

case "gte":
$pass = ($presentVal >= $threshold);
break;

case "is":
$pass = ($presentVal == $threshold);
break;
}


$rules[$rule->getRank()->getName()][$rule->getNutrient()->getName()] = [
"operator" => $operator,
"threshold" => $threshold,
"passed" => $pass?"true" : "false"
];

$rules[$rule->getRank()->getName()]['passedRank'] = $pass?"true" : "false";

if($pass) {
$passedRank = $rule->getRank()->getName();
}

}

echo "<h1>The rules expected...</h1>";
echo "<pre>";
print_r($rules);
echo "</pre>";

echo "Ultimately this food is a ". $passedRank;


// for each rules, evaluate ingredients
// profit!
});
3 changes: 2 additions & 1 deletion api/routes/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"nutrientId" => $rule->getNutrient()->getId(),
"operator" => $rule->getOperator(),
"threshold" => $rule->getThreshold(),
"units" => $rule->getUnits()
"units" => $rule->getUnits(),
"rank" => $rule->getRankId()
];
}

Expand Down

0 comments on commit 3c3ce56

Please sign in to comment.