-
Notifications
You must be signed in to change notification settings - Fork 549
/
foursquare.js
89 lines (72 loc) · 1.68 KB
/
foursquare.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
(function(hello) {
hello.init({
foursquare: {
name: 'Foursquare',
oauth: {
// See: https://developer.foursquare.com/overview/auth
version: 2,
auth: 'https://foursquare.com/oauth2/authenticate',
grant: 'https://foursquare.com/oauth2/access_token'
},
// Refresh the access_token once expired
refresh: true,
base: 'https://api.foursquare.com/v2/',
get: {
me: 'users/self',
'me/friends': 'users/self/friends',
'me/followers': 'users/self/friends',
'me/following': 'users/self/friends'
},
wrap: {
me: function(o) {
formatError(o);
if (o && o.response) {
o = o.response.user;
formatUser(o);
}
return o;
},
'default': function(o) {
formatError(o);
// Format friends
if (o && 'response' in o && 'friends' in o.response && 'items' in o.response.friends) {
o.data = o.response.friends.items;
o.data.forEach(formatUser);
delete o.response;
}
return o;
}
},
xhr: formatRequest,
jsonp: formatRequest
}
});
function formatError(o) {
if (o.meta && (o.meta.code === 400 || o.meta.code === 401)) {
o.error = {
code: 'access_denied',
message: o.meta.errorDetail
};
}
}
function formatUser(o) {
if (o && o.id) {
o.thumbnail = o.photo.prefix + '100x100' + o.photo.suffix;
o.name = o.firstName + ' ' + o.lastName;
o.first_name = o.firstName;
o.last_name = o.lastName;
if (o.contact) {
if (o.contact.email) {
o.email = o.contact.email;
}
}
}
}
function formatRequest(p, qs) {
var token = qs.access_token;
delete qs.access_token;
qs.oauth_token = token;
qs.v = 20121125;
return true;
}
})(hello);