Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rakibdevs committed Jan 8, 2021
0 parents commit fbddf8e
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 iqbalbary

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Laravel Open Weather API




## License

Laravel Open Weather API is licensed under [The MIT License (MIT)](LICENSE).

30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "rakibdevs/openweather-laravel-api",
"description": "Laravel package to connect https://openweathermap.org/ to get weather data for any location on the globe immediately",
"keywords": ["laravel","weather","open-weather","api"],
"license": "MIT",
"authors": [
{
"name": "Md. Rakibul Islam",
"email": "[email protected]",
"website": "https:://rakibul.dev"
}
],
"require": {
"php": "^7.1.3",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"RakibDevs\\Weather\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"RakibDevs\\Weather\\WeatherServiceProvider"
]
}
},
"minimum-stability": "stable"
}
9 changes: 9 additions & 0 deletions src/Exceptions/WeatherException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace RakibDevs\Weather\Src\Exceptions;

use Exception;

class WeatherException extends \Exception
{

}
101 changes: 101 additions & 0 deletions src/Weather.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace RakibDevs\Weather;

use RakibDevs\Weather\Src\Exceptions\WeatherException;
use Illuminate\Support\Facades\Config;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\TooManyRedirectsException;

class Weather
{

protected $url = 'https://api.openweathermap.org/data/2.5/';

protected $client;

protected $api_key;

protected $temp_format;

protected $date_time_format;

protected $date_format;

protected $time_format;

protected $day_format;


public function __construct()
{
$this->client = new Client([
'base_uri' => $this->url,
'timeout' => 10.0,
]);
$this->api_key = Config::get('openweather.api_key');
$this->temp_format = Config::get('openweather.temp_format','k');
$this->date_format = Config::get('openweather.date_format','m/d/Y');
$this->time_format = Config::get('openweather.time_format','h:i A');
$this->day_format = Config::get('openweather.day_format','l');
$this->date_time_format = $this->date_format.' '.$this->time_format;
}

private function tempConvert($num, $type = null)
{
$type = $type??$this->temp_format;
if($type == 'c')
return round($num - 273.15, 2);
else if($type == 'f')
return round((($num - 273.15) * 9/5 + 32),2);
else
return $num;
}

private function getCurrent($query)
{
try{
$response = $this->client->request('GET', 'weather?'.$query.'&appid='.$this->api_key);
if($response->getStatusCode() == 200){
$result = json_decode($response->getBody()->getContents());
// modify data based on user requirement

// modify temparature in [C,F,K]
$result->main->temp = $this->tempConvert($result->main->temp);
$result->main->feels_like = $this->tempConvert($result->main->feels_like);
$result->main->temp_min = $this->tempConvert($result->main->temp_min);
$result->main->temp_max = $this->tempConvert($result->main->temp_max);
$result->main->temp_max = $this->tempConvert($result->main->temp_max);

// modify date in given format
$result->sys->sunrise = date($this->date_time_format, $result->sys->sunrise+$result->timezone);
$result->sys->sunset = date($this->date_time_format, $result->sys->sunset+$result->timezone);

return $result;
}
}
catch (ClientException | RequestException | ConnectException | ServerException | TooManyRedirectsException $e) {
throw new WeatherException($e->getMessage());
}
}

public function getCurrentByCity($city)
{
return $this->getCurrent('q='.$city);
}

public function getTemperature($city)
{

}

public function getCurrentByCoordinates($lat, $lon)
{
return $this->getCurrent($lat.','.$lon, $date);
}

}
31 changes: 31 additions & 0 deletions src/WeatherServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RakibDevs\Weather;

use Illuminate\Support\ServiceProvider;

class WeatherServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->make('RakibDevs\Weather\Weather');
$this->publishes([
__DIR__ . '/config/openweather.php' => config_path('openweather.php'),
]);
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
12 changes: 12 additions & 0 deletions src/config/openweather.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'api_key' => 'ae9f7b6a0cfc2563ec1d24f3c267ad42',
'lang' => 'en',
'date_format' => 'm/d/Y',
'time_format' => 'h:i A',
'day_format' => 'l',
'temp_format' => 'c'
];

?>

0 comments on commit fbddf8e

Please sign in to comment.