Skip to content

Commit fbddf8e

Browse files
committed
first commit
0 parents  commit fbddf8e

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 iqbalbary
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Laravel Open Weather API
2+
3+
4+
5+
6+
## License
7+
8+
Laravel Open Weather API is licensed under [The MIT License (MIT)](LICENSE).
9+

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "rakibdevs/openweather-laravel-api",
3+
"description": "Laravel package to connect https://openweathermap.org/ to get weather data for any location on the globe immediately",
4+
"keywords": ["laravel","weather","open-weather","api"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Md. Rakibul Islam",
9+
"email": "[email protected]",
10+
"website": "https:://rakibul.dev"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.1.3",
15+
"guzzlehttp/guzzle": "^6.3"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"RakibDevs\\Weather\\": "src/"
20+
}
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"RakibDevs\\Weather\\WeatherServiceProvider"
26+
]
27+
}
28+
},
29+
"minimum-stability": "stable"
30+
}

src/Exceptions/WeatherException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace RakibDevs\Weather\Src\Exceptions;
3+
4+
use Exception;
5+
6+
class WeatherException extends \Exception
7+
{
8+
9+
}

src/Weather.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace RakibDevs\Weather;
4+
5+
use RakibDevs\Weather\Src\Exceptions\WeatherException;
6+
use Illuminate\Support\Facades\Config;
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\ConnectException;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Exception\ServerException;
12+
use GuzzleHttp\Exception\TooManyRedirectsException;
13+
14+
class Weather
15+
{
16+
17+
protected $url = 'https://api.openweathermap.org/data/2.5/';
18+
19+
protected $client;
20+
21+
protected $api_key;
22+
23+
protected $temp_format;
24+
25+
protected $date_time_format;
26+
27+
protected $date_format;
28+
29+
protected $time_format;
30+
31+
protected $day_format;
32+
33+
34+
public function __construct()
35+
{
36+
$this->client = new Client([
37+
'base_uri' => $this->url,
38+
'timeout' => 10.0,
39+
]);
40+
$this->api_key = Config::get('openweather.api_key');
41+
$this->temp_format = Config::get('openweather.temp_format','k');
42+
$this->date_format = Config::get('openweather.date_format','m/d/Y');
43+
$this->time_format = Config::get('openweather.time_format','h:i A');
44+
$this->day_format = Config::get('openweather.day_format','l');
45+
$this->date_time_format = $this->date_format.' '.$this->time_format;
46+
}
47+
48+
private function tempConvert($num, $type = null)
49+
{
50+
$type = $type??$this->temp_format;
51+
if($type == 'c')
52+
return round($num - 273.15, 2);
53+
else if($type == 'f')
54+
return round((($num - 273.15) * 9/5 + 32),2);
55+
else
56+
return $num;
57+
}
58+
59+
private function getCurrent($query)
60+
{
61+
try{
62+
$response = $this->client->request('GET', 'weather?'.$query.'&appid='.$this->api_key);
63+
if($response->getStatusCode() == 200){
64+
$result = json_decode($response->getBody()->getContents());
65+
// modify data based on user requirement
66+
67+
// modify temparature in [C,F,K]
68+
$result->main->temp = $this->tempConvert($result->main->temp);
69+
$result->main->feels_like = $this->tempConvert($result->main->feels_like);
70+
$result->main->temp_min = $this->tempConvert($result->main->temp_min);
71+
$result->main->temp_max = $this->tempConvert($result->main->temp_max);
72+
$result->main->temp_max = $this->tempConvert($result->main->temp_max);
73+
74+
// modify date in given format
75+
$result->sys->sunrise = date($this->date_time_format, $result->sys->sunrise+$result->timezone);
76+
$result->sys->sunset = date($this->date_time_format, $result->sys->sunset+$result->timezone);
77+
78+
return $result;
79+
}
80+
}
81+
catch (ClientException | RequestException | ConnectException | ServerException | TooManyRedirectsException $e) {
82+
throw new WeatherException($e->getMessage());
83+
}
84+
}
85+
86+
public function getCurrentByCity($city)
87+
{
88+
return $this->getCurrent('q='.$city);
89+
}
90+
91+
public function getTemperature($city)
92+
{
93+
94+
}
95+
96+
public function getCurrentByCoordinates($lat, $lon)
97+
{
98+
return $this->getCurrent($lat.','.$lon, $date);
99+
}
100+
101+
}

src/WeatherServiceProvider.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RakibDevs\Weather;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class WeatherServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
$this->app->make('RakibDevs\Weather\Weather');
17+
$this->publishes([
18+
__DIR__ . '/config/openweather.php' => config_path('openweather.php'),
19+
]);
20+
}
21+
22+
/**
23+
* Bootstrap services.
24+
*
25+
* @return void
26+
*/
27+
public function boot()
28+
{
29+
//
30+
}
31+
}

src/config/openweather.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'api_key' => 'ae9f7b6a0cfc2563ec1d24f3c267ad42',
5+
'lang' => 'en',
6+
'date_format' => 'm/d/Y',
7+
'time_format' => 'h:i A',
8+
'day_format' => 'l',
9+
'temp_format' => 'c'
10+
];
11+
12+
?>

0 commit comments

Comments
 (0)