-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.uconn.edu/dmd-web-and-mobile…
…/foodbank into develop
- Loading branch information
Showing
3 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
<?php | ||
|
||
/** | ||
* POST Request for food decision | ||
* @param barcode Food Barcode | ||
* @param category Food Category | ||
*/ | ||
$app->post('/decision', function($request, $response) { | ||
$formDataArray = $request->getParsedBody(); | ||
$response_json = [ | ||
"status" => [ | ||
"code" => $response->getStatusCode(), | ||
"message" => "OK" | ||
], | ||
"data" => $formDataArray | ||
]; | ||
// Some Decision Logic | ||
$random = rand(0,2); | ||
$rank = ""; | ||
if ( $random === 0 ) { | ||
$rank = "Green"; | ||
} else if ( $random === 1 ) { | ||
$rank = "Yellow"; | ||
} else { | ||
$rank = "Red"; | ||
} | ||
$response_json["decision"] = $rank; | ||
$response = $response->withJSON($response_json); | ||
return $response; | ||
}); | ||
|
||
function getFdctLoginInfo(){ | ||
$endpoint = "http://api.fooducate.com/fdct/login/basic/?username=guest&password=&responseformat=json"; | ||
$ch = curl_init($endpoint); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | ||
"X-Fdct-Udid: RuddCenter_Test", | ||
"X-Fdct-User-Agent: RuddCenter/1.0 ( OSX; 10.10.4; MacPro/2009; 1.0 )", | ||
)); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($ch); | ||
curl_close($ch); | ||
$response = json_decode($response,true); | ||
$importantStuff ['fdctuserid'] = $response['response']['body']['user']['@attributes']['id']; | ||
$importantStuff ['fdctsessionid'] = $response['response']['head']['session']['@attributes']['id']; | ||
$importantStuff ['fdcttimestamp'] = $response['response']['head']['request']['@attributes']['timestamp']; | ||
return $importantStuff; | ||
} | ||
function getProductIngredients($fdct_id) { | ||
$endpoint ="http://www.fooducate.com/internal/chef_client_proxy/get_product_by_id/".$fdct_id."/null"; | ||
$ch = curl_init($endpoint); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($ch); | ||
$res = json_decode($response,true); | ||
$greds = $res['response']['body']['product_list']['products'][0]['product_ingredients']['original_ingredients']['@text']; | ||
return $greds; | ||
curl_close($ch); | ||
} | ||
|
||
|
||
/** Not a route. Should not be here. Don't care. Sue me. **/ | ||
|
||
function getProductInfo($upc) { | ||
|
||
$fdctconfig = array(); | ||
$fdctconfig['passphrase'] = "Partially Hydrogenated Vegetable"; | ||
//login and get the session id and userid from the login | ||
$fdctstuff = getFdctLoginInfo(); | ||
// set the default timezone to match fooducate server | ||
date_default_timezone_set("UTC"); | ||
$tstamp = date(DATE_ISO8601, time()); | ||
// build the token | ||
$token = $fdctstuff['fdctuserid']. ":" . $fdctstuff['fdctsessionid'] . ":" . $tstamp; | ||
// create the hash w the passphrase | ||
$signature = base64_encode(hash_hmac('sha256', $token, $fdctconfig['passphrase'],true)); | ||
// get ready and stuff | ||
$endpoint = "http://api.fooducate.com/fdct/product/view/product/?reason=scan&upc=".$upc."&responseformat=json"; | ||
$ch = curl_init($endpoint); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
//set the headers based on the api spec..right? WHY YOU NO WORK! | ||
$headers = array( | ||
"X-Fdct-Udid: RuddCenter_Test", | ||
"X-Fdct-User-Agent: RuddCenter/1.0 ( OSX; 10.10.4; MacPro/2009; 1.0 )", | ||
"X-Fdct-Signature-Method: SHA1-HMAC256", | ||
"X-Fdct-Timestamp: ".$tstamp, | ||
"X-Fdct-Session: " .$fdctstuff['fdctsessionid'], | ||
"X-Fdct-Signature: ". $signature); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | ||
// set our own response headers and output the result | ||
// $response->headers->set('Content-Type', 'application/json'); | ||
$fdctresponse = curl_exec($ch); | ||
$res = json_decode($fdctresponse,true); | ||
$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); | ||
|
||
|
||
|
||
$response_json = [ | ||
"status" => [ | ||
"code" => $response->getStatusCode(), | ||
"message" => "OK" | ||
], | ||
"data" => [$res] | ||
]; | ||
|
||
$response = $response->withJSON($response_json); | ||
return $response; | ||
|
||
|
||
|
||
}); | ||
|
||
|
||
|
||
$app->get('/getDecision/{upc}/{category}', function($request, $response) { | ||
|
||
// 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; | ||
$c = new CategoryQuery(); | ||
|
||
|
||
$allRules = $q->filterByCategoryId($request->getAttribute('category'))->find(); | ||
//$res["food"] = $prodInfo; | ||
$res["food"]['brand'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['brand']; | ||
$res["food"]['name'] = $prodInfo['response']['body']['product_list']['products'][0]["@attributes"]['name']; | ||
|
||
$res["request"]["categoryRuleRun"] = $c->findPk($request->getAttribute("category"))->getName(); | ||
|
||
|
||
//default | ||
$res["rankId"] = 0; | ||
$res["rankName"] = "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) { | ||
$res["rankId"] = $rule->getRank()->getId(); | ||
$res["rankName"] = $rule->getRank()->getName(); | ||
} | ||
|
||
} | ||
|
||
// echo "<h1>The rules expected...</h1>"; | ||
// echo "<pre>"; | ||
// print_r($rules); | ||
// echo "</pre>"; | ||
|
||
// echo "Ultimately this food is a ". $passedRank; | ||
|
||
$response_json = [ | ||
"status" => [ | ||
"code" => $response->getStatusCode(), | ||
"message" => "OK" | ||
], | ||
"data" => [$res] | ||
]; | ||
|
||
$response = $response->withJSON($response_json); | ||
return $response; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters