diff --git a/api/index.php b/api/index.php index 857907c..2dc475c 100644 --- a/api/index.php +++ b/api/index.php @@ -14,6 +14,7 @@ require './routes/category.php'; require './routes/rules.php'; require './routes/food.php'; +require './routes/decision.php'; // Run App $app->run(); \ No newline at end of file diff --git a/api/routes/decision.php b/api/routes/decision.php new file mode 100644 index 0000000..90d0ca0 --- /dev/null +++ b/api/routes/decision.php @@ -0,0 +1,225 @@ +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 "
"; +// print_r($theNuts); +// echo ""; + +// 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 "
"; +// print_r($rules); +// echo ""; + +// echo "Ultimately this food is a ". $passedRank; + + $response_json = [ + "status" => [ + "code" => $response->getStatusCode(), + "message" => "OK" + ], + "data" => [$res] + ]; + + $response = $response->withJSON($response_json); + return $response; +}); \ No newline at end of file diff --git a/api/routes/rules.php b/api/routes/rules.php index 5a24562..f18979a 100644 --- a/api/routes/rules.php +++ b/api/routes/rules.php @@ -18,7 +18,8 @@ "nutrientId" => $rule->getNutrient()->getId(), "operator" => $rule->getOperator(), "threshold" => $rule->getThreshold(), - "units" => $rule->getUnits() + "units" => $rule->getUnits(), + "rank" => $rule->getRankId() ]; }