-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyelpSearch.js
More file actions
39 lines (34 loc) · 1005 Bytes
/
Copy pathyelpSearch.js
File metadata and controls
39 lines (34 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const yelp = require('yelp-fusion')
const yelpApiKey = process.env.YELP_API_KEY
const client = yelp.client(yelpApiKey)
const yelpHoursLookup = function(yelpData) {
return client
.business(yelpData.id)
.then(res => res.jsonBody)
.then(({ hours }) => {
if (hours && hours[0] && hours[0].hasOwnProperty('is_open_now')) {
return {
...yelpData,
is_open_now: hours[0].is_open_now,
}
}
// no 'hours' info present, so return yelpData unmodified
else return yelpData
})
}
const yelpSearch = function(term, latitude, longitude) {
return client
.search({
term,
latitude,
longitude,
})
.then(response => response.jsonBody)
.then(({ businesses }) => {
// Check if search result exists
if (businesses[0] !== undefined) return yelpHoursLookup(businesses[0])
// Otherwise send back empty object
else return {} // businesses[0] === undefined
})
}
module.exports = yelpSearch