Skip to content

Commit

Permalink
fix: fix weather get attribute (#5705)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Nov 16, 2021
1 parent afe1428 commit 25e5e59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
17 changes: 11 additions & 6 deletions app/Models/Account/Weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function place()
/**
* Get the weather code.
*
* @return string
* @return string|null
*/
public function getSummaryCodeAttribute()
public function getSummaryCodeAttribute(): ?string
{
$json = $this->weather_json;

Expand All @@ -74,7 +74,7 @@ public function getSummaryCodeAttribute()
if (($text = Arr::get($json, 'current.condition.text')) === 'Partly cloudy') {
$icon = ((bool) Arr::get($json, 'current.is_day')) ? 'partly-cloudy-day' : 'partly-cloudy-night';
} else {
$icon = Str::of($text)->lower()->replace(' ', '-');
$icon = (string) Str::of($text)->lower()->replace(' ', '-');
}
}

Expand All @@ -84,17 +84,22 @@ public function getSummaryCodeAttribute()
/**
* Get the weather summary.
*
* @return string
* @return string|null
*/
public function getSummaryAttribute(): ?string
{
return trans('app.weather_'.$this->summary_code);
$summary_code = $this->summary_code;
if (empty($summary_code)) {
return null;
}

return trans('app.weather_'.$summary_code);
}

/**
* Get the weather location.
*
* @return string
* @return string|null
*/
public function getLocationAttribute(): ?string
{
Expand Down
16 changes: 11 additions & 5 deletions app/Services/Instance/Geolocalization/GetGPSCoordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ private function validateWeatherEnvVariables()
* Build the query to send with the API call.
*
* @param Place $place
* @return string
* @return string|null
*/
private function buildQuery(Place $place): string
private function buildQuery(Place $place): ?string
{
if (($q = $place->getAddressAsString()) === null) {
return null;
}

$query = http_build_query([
'format' => 'json',
'key' => config('monica.location_iq_api_key'),
'q' => $place->getAddressAsString(),
'q' => $q,
]);

return Str::finish(config('location.location_iq_url'), '/').'search.php?'.$query;
Expand All @@ -82,7 +86,9 @@ private function buildQuery(Place $place): string
*/
private function query(Place $place): ?Place
{
$query = $this->buildQuery($place);
if (($query = $this->buildQuery($place)) === null) {
return null;
}

try {
$response = Http::get($query);
Expand All @@ -96,7 +102,7 @@ private function query(Place $place): ?Place
} catch (RequestException $e) {
if ($e->response->status() === 429 && ($error = $e->response->json('error')) && $error === 'Rate Limited Second') {
throw new RateLimitedSecondException($e);
} else {
} elseif ($e->response->status() !== 404 && $e->response->status() !== 400) {
Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [
'query' => Str::of($query)->replace(config('monica.location_iq_api_key'), '******'),
$e,
Expand Down
2 changes: 1 addition & 1 deletion resources/views/people/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<div class="row">
<div class="col-12 col-sm-3 profile-sidebar">

@if (! is_null($weather))
@if (! is_null($weather) && $weather->summary)
<div class="ba b--near-white br2 bg-gray-monica pa3 mb3 f6">
<div class="w-100 dt">
<div class="dtc">
Expand Down

0 comments on commit 25e5e59

Please sign in to comment.