Skip to content

Add UVI API #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ php:

matrix:
fast_finish: true
allow_failures:
- php: nightly

before_install:
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then phpenv config-rm xdebug.ini || echo "xdebug not available"; fi

install:
- if [[ ! $TRAVIS_PHP_VERSION = hhvm* ]]; then INI_FILE=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; else INI_FILE=/etc/hhvm/php.ini; fi
Expand Down
142 changes: 142 additions & 0 deletions Cmfcmf/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Cmfcmf\OpenWeatherMap\AbstractCache;
use Cmfcmf\OpenWeatherMap\CurrentWeather;
use Cmfcmf\OpenWeatherMap\CurrentUvi;
use Cmfcmf\OpenWeatherMap\CurrentWeatherGroup;
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
Expand Down Expand Up @@ -67,6 +68,16 @@ class OpenWeatherMap
*/
private $weatherHistoryUrl = 'http://history.openweathermap.org/data/2.5/history/city?';

/**
* @var string The basic api url to fetch current uv data from.
*/
private $uviUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/current.json?';

/**
* @var string The basic api url to fetch current uv data from.
*/
private $uviHistoryUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/%s.json?';

/**
* @var AbstractCache|bool $cache The cache to use.
*/
Expand Down Expand Up @@ -436,6 +447,109 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
return $this->cacheOrFetchResult($url);
}

/**
* Directly returns the json string returned by OpenWeatherMap for the UVI data.
*
* @param array $query The place to get information as follows: [latitude, longitude, date time]. For possible values see ::getWeather.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws \InvalidArgumentException
*
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.
*
* @api
*/
public function getRawUviData($query, $appid = '')
{
if (!is_array($query)) {
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]');
} elseif (count($query) != 2) {
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]');
} else {
$url = $this->buildUviUrl($query, $appid);
}

return $this->cacheOrFetchResult($url);
}

/**
* Directly returns the json string returned by OpenWeatherMap for the UVI history data.
*
* @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws \InvalidArgumentException
*
* @return string Returns false on failure and the fetched data in the format you specified on success.
*
* Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.
*
* @api
*/
public function getRawUviHistory($query, $appid = '')
{
if (!is_array($query)) {
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]');
} elseif (count($query) != 3) {
throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]');
} else {
$url = $this->buildUviUrl($query, $appid);
}

return $this->cacheOrFetchResult($url);
}

/**
* Returns the current uvi at the location you specified.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
*
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
* @throws \InvalidArgumentException If an argument error occurs.
*
* @return CurrentUvi The uvi object.
*
* There are three ways to specify the place to get weather information for:
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* @api
*/
public function getUvi($query, $appid = '')
{
$answer = $this->getRawUviData($query, $appid);
$json = $this->parseJson($answer);

return new CurrentUvi($json);
}

/**
* Returns the history uvi at the location you specified.
*
* @param array|int|string $query The place to get weather information for. For possible values see below.
* @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
* @param string $dateTime Your date time, default ''. See http://openweathermap.org/api/uvi for more details about date format.
*
* @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
* @throws \InvalidArgumentException If an argument error occurs.
*
* @return CurrentUvi The uvi object.
*
* There are three ways to specify the place to get weather information for:
* - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
*
* @api
*/
public function getUviHistory($query, $appid = '')
{
$answer = $this->getRawUviHistory($query, $appid);
$json = $this->parseJson($answer);

return new CurrentUvi($json);
}

/**
* Returns whether or not the last result was fetched from the cache.
*
Expand Down Expand Up @@ -503,6 +617,34 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
return $url;
}

/**
* Build the url to fetch UVI data from.
*
* @param $query
* @param $units
* @param $lang
* @param $appid
* @param $mode
* @param string $url The url to prepend.
*
* @return bool|string The fetched url, false on failure.
*/
private function buildUviUrl($query, $appid)
{
$queryLength = count($query);
switch ($queryLength) {
case 2:
$queryUrl = sprintf($this->uviUrl, $query[0], $query[1]);
break;
case 3:
$queryUrl = sprintf($this->uviHistoryUrl, $query[0], $query[1], $query[2]);
break;
}
$queryUrl .= 'APPID=';

return $queryUrl .= empty($appid) ? $this->apiKey : $appid;
}

/**
* Builds the query string for the url.
*
Expand Down
51 changes: 51 additions & 0 deletions Cmfcmf/OpenWeatherMap/CurrentUvi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/

namespace Cmfcmf\OpenWeatherMap;

use Cmfcmf\OpenWeatherMap\Util\Uvi;

/**
* Weather class used to hold the current weather data.
*/
class CurrentUvi
{
/**
* The city object.
*
* @var Util\Uvi
*/
public $uvi;

/**
* Create a new uvi object.
*
* @param mixed $data
*
* @internal
*/
public function __construct($data)
{
// generate the object from response JSON.
// ($time, $latitude, $longitude, $data)
if (empty($data->message) && empty($data->code)) {
$this->uvi = new Uvi($data->time, $data->location->latitude, $data->location->longitude, $data->data);
} else {
$this->uvi = null;
}
}
}
62 changes: 62 additions & 0 deletions Cmfcmf/OpenWeatherMap/Util/Uvi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap before using this class:
*
* @see http://www.OpenWeatherMap.org
* @see http://www.OpenWeatherMap.org/terms
* @see http://openweathermap.org/appid
*/

namespace Cmfcmf\OpenWeatherMap\Util;

/**
* The city class representing a city object.
*/
class Uvi
{
/**
* @var string The date time.
*/
public $time;

/**
* @var float The latitude.
*/
public $latitude;

/**
* @var float The longitude.
*/
public $longitude;

/**
* @var float The UVI data.
*/
public $data;

/**
* Create a new city object.
*
* @param string $time The current time or time slot.
* @param float $latitude The name of the city.
* @param float $longitude The longitude of the city.
* @param float $data The UVI value.
*
* @internal
*/
public function __construct($time, $latitude, $longitude, $data)
{
$this->time = $time;
$this->latitude = (float)$latitude;
$this->longitude = (float)$longitude;
$this->data = (float)$data;
}
}
26 changes: 26 additions & 0 deletions tests/Exceptions/OpenWeatherMapExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ public function testGetRawWeatherHistoryWithEndDateException()
{
$this->owm->getRawWeatherHistory('Berlin', new \DateTime('now'), 'wrong-endOrCount', 'hour', 'imperial', 'en', '');
}

/**
* @expectedException \InvalidArgumentException
* @dataProvider uviExceptionDataProvider
*/
public function testGetRawUviWithQueryErrorException($query)
{
$this->owm->getRawUviData($query);
}

/**
* @expectedException \InvalidArgumentException
* @dataProvider uviExceptionDataProvider
*/
public function testGetRawUviHistoryWithQueryErrorException($query)
{
$this->owm->getRawUviHistory($query);
}

/**
* @expectedException \InvalidArgumentException
Expand Down Expand Up @@ -161,4 +179,12 @@ public function testParseJsonException()

$method->invoke($this->owm, $answer);
}

public function uviExceptionDataProvider()
{
return array(
array('error-query-format'),
array(array())
);
}
}
2 changes: 1 addition & 1 deletion tests/OpenWeatherMap/WeatherHistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testJsonWithRainKey()
{
$fakeJson = json_decode(FakeData::WEATHER_HISTORY_WITH_RAIN_JSON, true);
$history = new WeatherHistory($fakeJson, 'Berlin');

$history->rewind();
$this->assertTrue($history->valid());
}
Expand Down
Loading