Skip to content

Commit

Permalink
Add facade for Geonames API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
skybluesofa committed Feb 4, 2024
1 parent 2dff4c6 commit 2fe2d17
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
26 changes: 26 additions & 0 deletions app/Apis/GeoNames/GeoNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Apis\GeoNames;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class GeoNames
{
protected $ttl = 60 * 60 * 24;

public function get(string $geonameId): array
{
return Cache::remember('geoName.'.$geonameId, $this->ttl, function () use ($geonameId) {
$url = 'https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/geonames-all-cities-with-a-population-500/records?select=coordinates,timezone&where=geoname_id%3D'.$geonameId.'&limit=1' ?? null;
$response = Http::get($url)->body();

$json = json_decode($response, true);
if (! isset($json['results'][0])) {
return null;
}

return $json['results'][0];
});
}
}
20 changes: 2 additions & 18 deletions app/Apis/TemperatureBlanketDotCom/TemperatureBlanketDotCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace App\Apis\TemperatureBlanketDotCom;

use App\Facades\ColorNames;
use App\Facades\GeoNames;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;

class TemperatureBlanketDotCom
{
Expand Down Expand Up @@ -144,26 +143,11 @@ protected function parseMeta(array $config): array

return [
'geoname_id' => $geoNameId,
'location' => $this->getCoordinatesForGeonameId($geoNameId),
'location' => GeoNames::get($geoNameId),
'date' => rtrim($meta[1], '!'),
];
}

protected function getCoordinatesForGeonameId($geoNameId): ?array
{
return Cache::remember('geoName.'.$geoNameId, $this->cacheTime, function () use ($geoNameId) {
$url = 'https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/geonames-all-cities-with-a-population-500/records?select=coordinates,timezone&where=geoname_id%3D'.$geoNameId.'&limit=1' ?? null;
$response = Http::get($url)->body();

$json = json_decode($response, true);
if (! isset($json['results'][0])) {
return null;
}

return $json['results'][0];
});
}

protected function parseTemperaturesColors(array $config): array
{
if (! isset($config['temp'])) {
Expand Down
14 changes: 14 additions & 0 deletions app/Facades/GeoNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Facades;

use App\Apis\GeoNames\GeoNames as GeoNamesApi;
use Illuminate\Support\Facades\Facade;

class GeoNames extends Facade
{
protected static function getFacadeAccessor()
{
return GeoNamesApi::class;
}
}

0 comments on commit 2fe2d17

Please sign in to comment.