Skip to content

Commit 8eb8294

Browse files
committed
fix: slight refactor to handle data processing better
1 parent 8c306a6 commit 8eb8294

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

index.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,7 @@ api.get('/', (req, res) => {
3232
// If the geolocation is successful, format the name of the returned location,
3333
// then call the weather API with the coordinates and timezone.
3434

35-
if ("regionName" in coords && "city" in coords && "country" in coords) {
36-
renderValues.locationName = `${coords.city}, ${coords.regionName}, ${coords.country}`
37-
} else if ("country" in coords) {
38-
if ("city" in coords) {
39-
renderValues.locationName = `${coords.city}, ${coords.country}`
40-
} else if ("regionName" in coords) {
41-
renderValues.locationName = `${coords.regionName}, ${coords.country}`
42-
} else {
43-
renderValues.locationName = coords.country
44-
}
45-
} else if ("city" in coords) {
46-
renderValues.locationName = coords.city
47-
} else {
48-
renderValues.locationName = coords.regionName
49-
}
50-
35+
renderValues.locationName = coords.locationName
5136
return weather(coords.lat, coords.lon, coords.timezone)
5237
}).catch(() => {
5338
// If the geolocation fails, default to Toronto, Ontario, Canada, then call
@@ -92,11 +77,9 @@ api.get('/geolocate', (req, res) => {
9277
// will send a request to `/geolocate` to get the estimated coordinates
9378
// of the client's IP address. This will then return the coordinates to the
9479
// client, which will use them to call the weather API as it normally would.
95-
geolocateFromIP(req.ip).then((coords) => {
96-
res.json(coords)
97-
}).catch((e) => {
98-
res.json({status: 'error', code: 500, message: e.message})
99-
})
80+
geolocateFromIP(req.ip)
81+
.then(coords => res.json(coords))
82+
.catch(e => res.json({status: 'error', code: 500, message: e.message}))
10083
})
10184

10285
api.get('/weather', (req, res) => {

src/geoipAPI.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ function apiRequest(url) {
6161
coordsResponse = JSON.parse(responseData)
6262
// console.log("coordsResponse: ", coordsResponse)
6363

64+
let locationName = ''
65+
if ("regionName" in coordsResponse && "city" in coordsResponse && "country" in coordsResponse) {
66+
locationName = `${coordsResponse.city}, ${coordsResponse.regionName}, ${coordsResponse.country}`
67+
} else if ("country" in coordsResponse) {
68+
if ("city" in coordsResponse) {
69+
locationName = `${coordsResponse.city}, ${coordsResponse.country}`
70+
} else if ("regionName" in coordsResponse) {
71+
locationName = `${coordsResponse.regionName}, ${coordsResponse.country}`
72+
} else {
73+
locationName = coordsResponse.country
74+
}
75+
} else if ("city" in coordsResponse) {
76+
locationName = coordsResponse.city
77+
} else {
78+
locationName = coordsResponse.regionName
79+
}
80+
81+
coordsResponse.locationName = locationName
82+
6483
// Because this sample app is used in live demos, we want to
6584
// anonymize the coordinates returned to the client side.
6685
if ("lat" in coordsResponse && "lon" in coordsResponse) {

0 commit comments

Comments
 (0)