/** * This will return tweets from DB cache if its valid and available otherwise it will get from twitter API call * and saves the user provided location name in DB * * @param $locationName * @return array */ public static function getTweets($locationName) { $locationName = urldecode($locationName); $locationName = $locationName = preg_replace('!\\s+!', ' ', $locationName); Capsule::connection()->beginTransaction(); $searchTweetForLocation = self::isLocationTweetValidInCache($locationName); $tweets = array(); if ($searchTweetForLocation['valid'] === false) { $tweets = self::getTweetsByLocation($locationName); if (count($tweets) != 0) { self::saveTweet($locationName, $tweets); } } else { $tweets = $searchTweetForLocation['tweets']; } SearchHistoryService::save($locationName); Capsule::connection()->commit(); return $tweets; }
})->via('GET', 'POST'); /** * Api Route Group */ $app->group('/api', function () use($app) { //Api Group tweet $app->group('/tweet', function () use($app) { //Get tweets by location name $app->get('/recent/:locationName/', function ($locationName) use($app) { $tweets = Service\TweetService::getTweets($locationName); $app->render(200, $tweets); }); //->conditions(array('locationName' => '[a-zA-Z\+]+')); }); /** * get latest entries */ $app->get('/search_history/', function () use($app) { $searchHistory = Service\SearchHistoryService::getSearchHistory(40); $app->render(200, $searchHistory); }); /** * get latest entries * @param timestamp if provided returned result will have records greater than this timestamp */ $app->get('/search_history/:timestamp/', function ($timestamp) use($app) { $searchHistory = Service\SearchHistoryService::getSearchHistory(40, $timestamp); $app->render(200, $searchHistory); })->conditions(array('timestamp' => '[0-9]+')); }); $app->run();