forked from danieljin/yelpv3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (57 loc) · 1.67 KB
/
index.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
var request = require('request');
const baseUrl = 'https://api.yelp.com/v3/';
class Yelpv3 {
constructor(opts) {
this.API_KEY = opts.API_KEY;
}
get(resource, params, callback) {
params = (typeof params === 'undefined') ? {} : params;
const promise = new Promise((resolve, reject) => {
request.get({
url: baseUrl + resource + jsonToQueryString(params),
headers: {
'Authorization': 'Bearer ' + this.API_KEY
}
}, (err, response, data) => {
if (!err && response.statusCode == 200) {
resolve(JSON.parse(data));
}
reject(err);
});
});
if (typeof callback === 'function') {
promise
.then((res) => callback(null, res))
.catch((err) => callback(err));
return null;
}
return promise;
}
search(params, callback) {
return this.get('businesses/search', params, callback);
}
phoneSearch(params, callback) {
return this.get('businesses/search/phone', params, callback);
}
transactionSearch(transactionType, params, callback) {
return this.get(`transactions/${transactionType}/search`, params, callback);
}
business(id, callback) {
return this.get(`businesses/${id}`, undefined, callback);
}
reviews(id, callback) {
return this.get(`businesses/${id}/reviews`, undefined, callback);
}
autocomplete(params, callback) {
return this.get('autocomplete', params, callback);
}
}
function jsonToQueryString(json) {
return '?' +
Object.keys(json).map(function(key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(json[key]);
}).join('&');
}
module.exports = Yelpv3;