diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91e1c68 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1d58af --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +## Laravel Open Weather API + + + + +## License + +Laravel Open Weather API is licensed under [The MIT License (MIT)](LICENSE). + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ee1ba02 --- /dev/null +++ b/composer.json @@ -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": "rakib1708@gmail.com", + "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" +} diff --git a/src/Exceptions/WeatherException.php b/src/Exceptions/WeatherException.php new file mode 100644 index 0000000..2b6656a --- /dev/null +++ b/src/Exceptions/WeatherException.php @@ -0,0 +1,9 @@ +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); + } + +} diff --git a/src/WeatherServiceProvider.php b/src/WeatherServiceProvider.php new file mode 100644 index 0000000..808c7d5 --- /dev/null +++ b/src/WeatherServiceProvider.php @@ -0,0 +1,31 @@ +app->make('RakibDevs\Weather\Weather'); + $this->publishes([ + __DIR__ . '/config/openweather.php' => config_path('openweather.php'), + ]); + } + + /** + * Bootstrap services. + * + * @return void + */ + public function boot() + { + // + } +} diff --git a/src/config/openweather.php b/src/config/openweather.php new file mode 100644 index 0000000..30b61c4 --- /dev/null +++ b/src/config/openweather.php @@ -0,0 +1,12 @@ + 'ae9f7b6a0cfc2563ec1d24f3c267ad42', + 'lang' => 'en', + 'date_format' => 'm/d/Y', + 'time_format' => 'h:i A', + 'day_format' => 'l', + 'temp_format' => 'c' +]; + +?> \ No newline at end of file