<commit_before><?php
/**
 * @author: <NAME> <<EMAIL>>
 * @copyright:
 * @date: 3/31/2015
 * @time: 1:17 PM
 */

namespace VertigoLabs\Overcast;


class Overcast
{
	const API_ENDPOINT = 'https://api.forecast.io/forecast/';

	private $apiKey = null;
	private $apiCalls = 0;

	public function __construct($apiKey)
	{
		$this->apiKey = $apiKey;
	}

	/**
	 * @param $latitude
	 * @param $longitude
	 * @param \DateTime $time
	 *
	 * @return Forecast
	 */
	public function getForecast($latitude, $longitude, \DateTime $time = null)
	{
		$requestUrl = self::API_ENDPOINT.$this->apiKey.'/'.$latitude.','.$longitude;

		if (!is_null($time)) {
			$requestUrl .= ','.$time->getTimestamp();
		}

		$response = json_decode(file_get_contents($requestUrl),true);
		$responseHeaders = $this->parseForecastResponseHeaders($http_response_header);

		if (!is_null($responseHeaders['apiCalls'])) {
			$this->apiCalls = $responseHeaders['apiCalls'];
		}

		$cacheAge = 0;
		if (!is_null($responseHeaders['cache']['maxAge'])) {
			$cacheAge = $responseHeaders['cache']['maxAge'];
		}elseif(!is_null($responseHeaders['cache']['expires'])) {
			$cacheAge = (new \DateTime())->getTimestamp() - (new \DateTime($responseHeaders['cache']['expires']))->getTimestamp();
		}

		return new Forecast($response,$cacheAge, $responseTime);
	}

	/**
	 * @return int
	 */
	public function getApiCalls()
	{
		return $this->apiCalls;
	}

	private function parseForecastResponseHeaders($headers)
	{
		$responseHeaders = [
			'cache' => [
				'maxAge'=>null,
				'expires'=>null
			],
			'responseTime'=>null,
			'apiCalls'=>null
		];
		foreach ($headers as $header) {
			switch (true) {
				case (substr($header,0,14) === 'Cache-Control:'):
					$responseHeaders['cache']['maxAge'] = trim(substr($header,strrpos($header,'=')+1));
					break;
				case (substr($header,0,8) === 'Expires:'):
					$responseHeaders['cache']['expires'] = trim(substr($header,8));
					break;
				case (substr($header,0,21) === 'X-Forecast-API-Calls:'):
					$responseHeaders['apiCalls'] = trim(substr($header,21));
					break;
				case (substr($header,0,16) === 'X-Response-Time:'):
					$responseHeaders['responseTime'] = trim(substr($header,16));
					break;
				default:
					break;
			}
		}
		return $responseHeaders;
	}
}<commit_msg>fixed a bug where $responseTime variable was undefined<commit_after><?php
/**
 * @author: <NAME> <<EMAIL>>
 * @copyright:
 * @date: 3/31/2015
 * @time: 1:17 PM
 */

namespace VertigoLabs\Overcast;


class Overcast
{
	const API_ENDPOINT = 'https://api.forecast.io/forecast/';

	private $apiKey = null;
	private $apiCalls = 0;

	public function __construct($apiKey)
	{
		$this->apiKey = $apiKey;
	}

	/**
	 * @param $latitude
	 * @param $longitude
	 * @param \DateTime $time
	 *
	 * @return Forecast
	 */
	public function getForecast($latitude, $longitude, \DateTime $time = null)
	{
		$requestUrl = self::API_ENDPOINT.$this->apiKey.'/'.$latitude.','.$longitude;

		if (!is_null($time)) {
			$requestUrl .= ','.$time->getTimestamp();
		}

		$response = json_decode(file_get_contents($requestUrl),true);
		$responseHeaders = $this->parseForecastResponseHeaders($http_response_header);

		if (!is_null($responseHeaders['apiCalls'])) {
			$this->apiCalls = $responseHeaders['apiCalls'];
		}

		$cacheAge = 0;
		if (!is_null($responseHeaders['cache']['maxAge'])) {
			$cacheAge = $responseHeaders['cache']['maxAge'];
		}elseif(!is_null($responseHeaders['cache']['expires'])) {
			$cacheAge = (new \DateTime())->getTimestamp() - (new \DateTime($responseHeaders['cache']['expires']))->getTimestamp();
		}

		return new Forecast($response,$cacheAge, $responseHeaders['responseTime']);
	}

	/**
	 * @return int
	 */
	public function getApiCalls()
	{
		return $this->apiCalls;
	}

	private function parseForecastResponseHeaders($headers)
	{
		$responseHeaders = [
			'cache' => [
				'maxAge'=>null,
				'expires'=>null
			],
			'responseTime'=>null,
			'apiCalls'=>null
		];
		foreach ($headers as $header) {
			switch (true) {
				case (substr($header,0,14) === 'Cache-Control:'):
					$responseHeaders['cache']['maxAge'] = trim(substr($header,strrpos($header,'=')+1));
					break;
				case (substr($header,0,8) === 'Expires:'):
					$responseHeaders['cache']['expires'] = trim(substr($header,8));
					break;
				case (substr($header,0,21) === 'X-Forecast-API-Calls:'):
					$responseHeaders['apiCalls'] = trim(substr($header,21));
					break;
				case (substr($header,0,16) === 'X-Response-Time:'):
					$responseHeaders['responseTime'] = (int)trim(substr($header,16));
					break;
				default:
					break;
			}
		}
		return $responseHeaders;
	}
}
{'fraction_non_alphanumeric': 0.2, 'fraction_numerical': 0.01256771397616468, 'mean_word_length': 4.417840375586854, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7010798', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1564, 'n_words': 255}