Exemple #1
0
 /**
  * Main point of entry for the Schools endpoint.
  *
  * @param Request     $req The request to be handled.
  * @param Application $app The Silex Application used to handle the request.
  *
  * @return array An array of schools in Hampton, VABeach, Norfolk, Portsmouth, Chesapeake, and Suffolk.
  */
 public function main(Request $req, Application $app)
 {
     $requestedCities = ['Hampton', 'Norfolk', 'Virginia Beach', 'Portsmouth', 'Chesapeake', 'Suffolk'];
     if ($req->query->has('cities')) {
         $requestedCities = explode(',', $req->query->get('cities'));
     }
     $groupByField = $req->query->get('groupBy');
     $averageField = $req->query->get('averageField');
     $resultSet = [];
     foreach ($requestedCities as $requestedCity) {
         $url = self::formatRequestUrl($requestedCity);
         $response = $app['guzzle']->get($url, []);
         $schools = self::convertToJson($response->getBody());
         $resultSet[] = self::filterResultsByCity($schools, $requestedCity);
     }
     if (!empty($averageField) && !empty($groupByField)) {
         $resultSet = self::calculateAverages($resultSet, $averageField, $groupByField);
     }
     $herculesResponse = new HerculesResponse('/schools', 200, $resultSet);
     // The frontend expects a JSONP format, to do this the response must be wrapped in a callback.
     return $_GET['callback'] . '(' . $herculesResponse->to_json() . ')';
 }
 /**
  * Verifies behaviour when validating a response fails while converting to json.
  *
  * @return void
  *
  * expectedException \InvalidResponseException
  */
 public function testToJson_missingData()
 {
     $actual = new HerculesResponse(null, null, null, null);
     $actual->to_json();
 }
Exemple #3
0
 /**
  * Parse the results from Elasticsearch for the Hampton Crime data set.
  *
  * @param array            $results  The json data from the request.
  * @param HerculesResponse $response The response object to append data to.
  *
  * @return HerculesReponse The response object all pretty.
  */
 private function parseResults(array $results, HerculesResponse $response)
 {
     // Parse the results.
     $resultArray = $results['hits'];
     foreach ($resultArray as $key => $value) {
         $id = $value['_id'];
         $offense = $value['_source']['offense'];
         $category = $value['_source']['category'];
         $class = $value['_source']['class'];
         $occured = new \DateTime($value['_source']['occurred']);
         $city = $value['_source']['city'];
         $location = $value['_source']['location'];
         if (isset($occured) && gettype($location) === 'array') {
             $datapoint = new DataPoint($id, $offense, $occured, $city, $location, $category, $class);
             $response->addDataEntry($datapoint->toArray());
         }
     }
     return $response;
 }