-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourSqaure.js
99 lines (85 loc) · 3.21 KB
/
FourSqaure.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* The FourSquare API module
*/
var FourSquare = (function (window, $, undefined) {
'use strict';
// the public returned object
var FourSquare = {},
_apiKeys = {
VERSION: '20150717'
},
_urls = {
RESTAURANTS: 'https://api.foursquare.com/v2/venues/search',
BASE: 'https://api.foursquare.com/v2/venues/',
TIPS: function (restaurantId) {
return this.BASE + restaurantId + '/tips/';
},
MENU: function (restaurantId) {
return this.BASE + restaurantId + '/menu/';
}
};
/**
* A initialization function to setup api keys. Must be called before calling any other function.
* @param oauthParams
*/
FourSquare.init = function (oauthParams) {
if (!oauthParams || !oauthParams.id || !oauthParams.secret) throw "Init object must contain both client id and client secret";
_apiKeys.CLIENT_ID = oauthParams.id;
_apiKeys.CLIENT_SECRET = oauthParams.secret;
// return this for chainability
return this;
};
/**
* Used to make a endpoint call to the FourSquare API
* @param url - the FourSquare endpoint url
* @param queryParams - additional query parameters to be added (api_key already included)
* @returns {Promise}
* @private
*/
function _fourSquareGet(url, queryParams) {
var ajaxArgs = {
crossDomain: true,
dataType: 'jsonp',
data: {
'client_id': _apiKeys.CLIENT_ID,
'client_secret': _apiKeys.CLIENT_SECRET,
'v': _apiKeys.VERSION
}
};
queryParams = queryParams || {};
Object.keys(queryParams).forEach(function (key) {
ajaxArgs.data[key] = queryParams[key];
});
return $.ajax(url, ajaxArgs);
}
/**
* Get restaurant data from the API
* @param queryArgs - optional query parameters to be passed with the request
* @returns {Promise}
*/
FourSquare.getRestaurants = function (queryArgs) {
queryArgs = queryArgs || {};
return _fourSquareGet(_urls.RESTAURANTS, queryArgs);
};
/**
* Get 'tips' from the FourSquare API for a particular restaurant given a restaurant JSON object
* @param restaurant - the JSON restaurant object returned from the Foursquare API
* @param queryArgs - optional query parameters to be passed with the request
* @returns {Promise}
*/
FourSquare.getRestaurantReviews = function (restaurant, queryArgs) {
queryArgs = queryArgs || {};
return _fourSquareGet(_urls.TIPS(restaurant.id), queryArgs);
};
/**
* Get menus from the FourSquare API for a particular restaurant given a restaurant JSON object
* @param restaurant - the JSON restaurant object returned from the Foursquare API
* @param queryArgs - optional query parameters to be passed with the request
* @returns {Promise}
*/
FourSquare.getRestaurantMenu = function (restaurant, queryArgs) {
queryArgs = queryArgs || {};
return _fourSquareGet(_urls.MENU(restaurant.id), queryArgs);
};
return FourSquare;
})(window, jQuery);