-
Notifications
You must be signed in to change notification settings - Fork 548
/
bikeindex.js
117 lines (90 loc) · 2.01 KB
/
bikeindex.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// BikeIndex
// Https://bikeindex.org/documentation/api_v2
(function(hello) {
hello.init({
bikeindex: {
name: 'BikeIndex',
oauth: {
version: 2,
auth: 'https://bikeindex.org/oauth/authorize/',
grant: 'https://api.bikeindex.org/oauth/access_token'
},
scope: {
basic: 'read_user,read_bikes',
email: 'read_user'
// Read_bikes: 'View user's bikes user owned',
// Write_bikes: 'Edit and create bikes'
},
scope_delim: '+',
// Refresh the access_token once expired
refresh: true,
base: 'https://bikeindex.org/api/v2/',
// There aren't many routes that map to the hello.api so I included me/bikes
// ... because, bikes
get: {
me: 'me',
'me/bikes': 'me/bikes'
},
post: {},
del: {},
wrap: {
me: function(o, headers) {
formatError(o, headers);
formatUser(o);
return o;
},
'default': function(o, headers, req) {
formatError(o, headers);
if (Array.isArray(o)) {
o = {data: o};
paging(o, headers, req);
o.data.forEach(formatUser);
}
return o;
}
},
xhr: function(p) {
if (p.method !== 'get' && p.data) {
// Serialize payload as JSON
p.headers = p.headers || {};
p.headers['Content-Type'] = 'application/json';
if (typeof (p.data) === 'object') {
p.data = JSON.stringify(p.data);
}
}
return true;
}
}
});
function formatError(o) {
if (o && o.error) {
o.error = {
code: (o.error === 'OAuth error: unauthorized' ? 'access_denied' : 'server_error'),
message: o.error
};
}
}
function formatUser(o) {
if (o.id) {
o.thumbnail = o.image;
}
if (o.user) {
hello.utils.extend(o, o.user);
delete o.user;
}
if (o.image) {
o.thumbnail = o.image;
}
return o;
}
function paging(res, headers, req) {
if (res.data && res.data.length && headers && headers.Link) {
var next = headers.Link.match(/<(.*?)>;\s*rel=\"next\"/);
if (next) {
res.paging = {
next: next[1]
};
}
}
}
})(hello);