Skip to content

Commit 8e353a7

Browse files
committed
Reorganized some namespaces
Added HTTP Client Adapters Added some documentation
1 parent 4cce033 commit 8e353a7

File tree

13 files changed

+324
-52
lines changed

13 files changed

+324
-52
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/vendor/
2+
example.php
3+
composer.lock

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# Overcast
2+
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4d076dd2-314d-4090-a025-25989a765f25/big.png)](https://insight.sensiolabs.com/projects/4d076dd2-314d-4090-a025-25989a765f25)
3+
4+
[![Latest Unstable Version](https://poser.pugx.org/vertigolabs/overcast/v/unstable.svg)](https://packagist.org/packages/vertigolabs/overcast)
5+
[![Latest Stable Version](https://poser.pugx.org/vertigolabs/overcast/v/stable.svg)](https://packagist.org/packages/vertigolabs/overcast)
6+
7+
[![License](https://poser.pugx.org/vertigolabs/overcast/license.svg)](https://packagist.org/packages/vertigolabs/overcast)
8+
[![Total Downloads](https://poser.pugx.org/vertigolabs/overcast/downloads.svg)](https://packagist.org/packages/vertigolabs/overcast)
9+
210
An easy to use wrapper for the [Forecast.io](https://forecast.io) API v2.
311

412
Overcast will query the Forecast.io API for weather information for the longitude and latitude you specify. Additionally
@@ -17,6 +25,21 @@ Installation is as simple as using [Composer](http://getcomposer.org/):
1725
}
1826
```
1927

28+
## Client Adapters
29+
Overcast uses client adapters to connect to the Forecast.io API. This gives you the ability to create your own adapter for whatever HTTP client you'd like to use. This is especially useful for people who have special needs when dealing with retrieving data from third parties (firewalls, proxies, etc)
30+
31+
Overcast comes with two client adapters ready for use, FileGetContentsClientAdapter and GuzzleClientAdapter. You can also create your own by simply implementing the ClientAdapterInterface
32+
33+
By default, you do not have to specify which adapter to use. Overcast will automatically use the best client adapter available. If Guzzle is installed, it will use the GuzzleClientAdaptor, otherwise it will fallback to the FileGetContentsClientAdapter.
34+
35+
If you do wish to specify the client adapter, you'd do this with the second parameter when instancing the Overcast class:
36+
37+
```php
38+
$overcast = new \VertigoLabs\Overcast\Overcast('YOUR API KEY', new \VertigoLabs\Overcast\ClientAdapters\FileGetContentsClientAdapter());
39+
// or
40+
$overcast = new \VertigoLabs\Overcast\Overcast('YOUR API KEY', new MyAwesomeClientAdapter());
41+
```
42+
2043
## Example
2144
Since the Forecast.io API is simple, Overcast is equally easy to use.
2245
Simply create an instance of the Overcast class, then call the getForecast() method.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"email": "jaimz@vertigolabs.org"
99
}
1010
],
11+
"suggest": {
12+
"guzzlehttp/guzzle": "Allows for better handling of http connections to Forecast.io webservice"
13+
},
1114
"autoload": {
1215
"psr-4": {
1316
"VertigoLabs\\Overcast\\": "src/"

src/ClientAdapterInterface.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @author: James Murray <jaimz@vertigolabs.org>
4+
* @copyright:
5+
* @date: 4/14/2015
6+
* @time: 12:48 PM
7+
*/
8+
namespace VertigoLabs\Overcast;
9+
10+
/**
11+
* Interface ClientAdapterInterface
12+
*
13+
* The Client Adapter interface is used to create HTTP clients for
14+
* connecting the the Forecast.io API
15+
*
16+
* @package VertigoLabs\Overcast
17+
*/
18+
interface ClientAdapterInterface
19+
{
20+
/**
21+
* Returns the response data from the Forecast.io in the
22+
* form of an array
23+
*
24+
* @param float $latitude
25+
* @param float $longitude
26+
* @param \DateTime $time
27+
*
28+
* @return array
29+
*/
30+
public function getForecast($latitude, $longitude, \DateTime $time = null);
31+
32+
/**
33+
* Returns the relevant response headers from the Forecast.io API
34+
*
35+
* @return array
36+
*/
37+
public function getHeaders();
38+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* @author: James Murray <jaimz@vertigolabs.org>
4+
* @copyright:
5+
* @date: 4/14/2015
6+
* @time: 12:50 PM
7+
*/
8+
namespace VertigoLabs\Overcast\ClientAdapters;
9+
10+
use VertigoLabs\Overcast\ClientAdapterInterface;
11+
use VertigoLabs\Overcast\Overcast;
12+
13+
/**
14+
* Class FileGetContentsClientAdapter
15+
*
16+
* The FileGetContents client adapter uses PHP's built in
17+
* file_get_contents function to retrieve data from the
18+
* Forecast.io API
19+
*
20+
* @package VertigoLabs\Overcast\ClientAdapters
21+
*/
22+
class FileGetContentsClientAdapter implements ClientAdapterInterface
23+
{
24+
private $requestedUrl = null;
25+
private $response = null;
26+
private $responseHeaders = [];
27+
28+
/**
29+
* Returns the response data from the Forecast.io in the
30+
* form of an array
31+
*
32+
* @param float $latitude
33+
* @param float $longitude
34+
* @param \DateTime $time
35+
*
36+
* @return array
37+
*/
38+
public function getForecast($latitude, $longitude, \DateTime $time = null)
39+
{
40+
$this->requestedUrl = Overcast::API_ENDPOINT.Overcast::getApiKey().'/'.$latitude.','.$longitude;
41+
42+
if (!is_null($time)) {
43+
$this->requestedUrl .= ','.$time->getTimestamp();
44+
}
45+
46+
$this->response = json_decode(file_get_contents($this->requestedUrl),true);
47+
$this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header);
48+
49+
return $this->response;
50+
}
51+
52+
/**
53+
* Returns the relevant response headers from the Forecast.io API
54+
*
55+
* @return array
56+
*/
57+
public function getHeaders()
58+
{
59+
return $this->responseHeaders;
60+
}
61+
62+
/**
63+
* Parses the response headers
64+
*
65+
* @param array $headers
66+
*
67+
* @return array
68+
*/
69+
private function parseForecastResponseHeaders($headers)
70+
{
71+
$responseHeaders = [
72+
'cache' => [
73+
'maxAge'=>null,
74+
'expires'=>null
75+
],
76+
'responseTime'=>null,
77+
'apiCalls'=>null
78+
];
79+
foreach ($headers as $header) {
80+
switch (true) {
81+
case (substr($header,0,14) === 'Cache-Control:'):
82+
$responseHeaders['cache']['maxAge'] = trim(substr($header,strrpos($header,'=')+1));
83+
break;
84+
case (substr($header,0,8) === 'Expires:'):
85+
$responseHeaders['cache']['expires'] = trim(substr($header,8));
86+
break;
87+
case (substr($header,0,21) === 'X-Forecast-API-Calls:'):
88+
$responseHeaders['apiCalls'] = trim(substr($header,21));
89+
break;
90+
case (substr($header,0,16) === 'X-Response-Time:'):
91+
$responseHeaders['responseTime'] = (int)trim(substr($header,16));
92+
break;
93+
default:
94+
break;
95+
}
96+
}
97+
return $responseHeaders;
98+
}
99+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* @author: James Murray <jaimz@vertigolabs.org>
4+
* @copyright:
5+
* @date: 4/14/2015
6+
* @time: 2:10 PM
7+
*/
8+
namespace VertigoLabs\Overcast\ClientAdapters;
9+
10+
use GuzzleHttp\Client;
11+
use VertigoLabs\Overcast\ClientAdapterInterface;
12+
use VertigoLabs\Overcast\Overcast;
13+
14+
/**
15+
* Class GuzzleClientAdapter
16+
*
17+
* The Guzzle client adapter uses Guzzle to connect to
18+
* the Forecast.io api
19+
*
20+
* @package VertigoLabs\Overcast\ClientAdapters
21+
*/
22+
class GuzzleClientAdapter implements ClientAdapterInterface
23+
{
24+
/**
25+
* @var Client
26+
*/
27+
private $guzzleClient;
28+
private $requestedUrl = null;
29+
private $responseHeaders = [];
30+
31+
/**
32+
* @param Client $guzzleClient
33+
*/
34+
public function __construct(Client $guzzleClient = null)
35+
{
36+
if (is_null($guzzleClient)){
37+
$guzzleClient = new Client();
38+
}
39+
$this->guzzleClient = $guzzleClient;
40+
}
41+
42+
/**
43+
* Returns the response data from the Forecast.io in the
44+
* form of an array
45+
*
46+
* @param float $latitude
47+
* @param float $longitude
48+
* @param \DateTime $time
49+
*
50+
* @return array
51+
*/
52+
public function getForecast($latitude, $longitude, \DateTime $time = null)
53+
{
54+
$this->requestedUrl = Overcast::API_ENDPOINT.Overcast::getApiKey().'/'.$latitude.','.$longitude;
55+
$response = $this->guzzleClient->get($this->requestedUrl);
56+
$this->responseHeaders = [
57+
'cache' => [
58+
'maxAge'=>(int)trim(substr($response->getHeader('cache-control'),strrpos($response->getHeader('cache-control'),'=')+1)),
59+
'expires'=>$response->getHeader('expires')
60+
],
61+
'responseTime'=>(int)$response->getHeader('x-response-time'),
62+
'apiCalls'=>(int)$response->getHeader('x-forecast-api-calls')
63+
];
64+
return $response->json();
65+
}
66+
67+
/**
68+
* Returns the relevant response headers from the Forecast.io API
69+
*
70+
* @return array
71+
*/
72+
public function getHeaders()
73+
{
74+
return $this->responseHeaders;
75+
}
76+
}

src/Forecast.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
* @date: 3/31/2015
66
* @time: 1:20 PM
77
*/
8-
98
namespace VertigoLabs\Overcast;
109

10+
use VertigoLabs\Overcast\ValueObjects\Alert;
11+
use VertigoLabs\Overcast\ValueObjects\DataBlock;
12+
use VertigoLabs\Overcast\ValueObjects\DataPoint;
1113

12-
use VertigoLabs\Overcast\Entities\Alert;
13-
use VertigoLabs\Overcast\Entities\DataBlock;
14-
use VertigoLabs\Overcast\Entities\DataPoint;
15-
14+
/**
15+
* Class Forecast
16+
*
17+
* The Forecast class represents the data returned
18+
* from the Forecast.io API
19+
*
20+
* @package VertigoLabs\Overcast
21+
*/
1622
class Forecast
1723
{
1824
/**
@@ -62,7 +68,7 @@ class Forecast
6268
private $responseTime;
6369

6470
/**
65-
* @param $forecastData
71+
* @param array $forecastData
6672
* @param int|null $cacheTTL
6773
* @param int|null $responseTime
6874
*/
@@ -166,7 +172,7 @@ public function getDaily()
166172
}
167173

168174
/**
169-
* @return Entities\Alert[]
175+
* @return ValueObjects\Alert[]
170176
*/
171177
public function getAlerts()
172178
{

0 commit comments

Comments
 (0)