-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
85 lines (77 loc) · 2.07 KB
/
twitter.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
load('oauth.js');
var Twitter = function (key, secret, token, token_secret) {
var self = this;
this.api_url = 'https://api.twitter.com/1.1';
this.key = key;
this.secret = secret;
this.token = token;
this.token_secret = token_secret;
var endpoints = {
search : {
tweets : {
method : 'search',
required : { q : '' },
http_method : 'get'
}
},
statuses : {
update : {
method : 'tweet',
required : { status : '' },
http_method : 'post'
},
user_timeline : {
method : 'get_user_timeline',
required : { screen_name : '' },
http_method : 'get'
}
}
};
/* Send a signed OAuth1 POST request to this.api_url + 'path'.
POST data will be constructed from key/value pairs in 'obj'. */
this.post = function (path, obj) {
return JSON.parse(
(new OAuth1_Client()).post(
this.api_url + path, obj,
this.key, this.secret, this.token, this.token_secret
)
);
}
/* Send a signed OAuth1 GET request to this.api_url + 'path'.
Query parameters will be constructed from key/value pairs in 'obj'. */
this.get = function (path, obj) {
return JSON.parse(
(new OAuth1_Client()).get(
this.api_url + path + '?' + param_stringify(obj, false, '&'),
this.key, this.secret, this.token, this.token_secret
)
);
}
// Populate methods from described REST API endpoints
function methodist(obj, path) {
for (var property in obj) {
if (typeof obj[property].method === 'undefined') {
methodist(obj[property], path + property + '/');
} else {
(function (obj, property, path) {
self[obj[property].method] = function (data) {
if (typeof data === 'undefined') var data = {};
if (typeof obj[property].required === 'object') {
for (var r in obj[property].required) {
if (typeof data[r] !==
typeof obj[property].required[r]
) {
throw obj[property].method + ': missing ' + r;
}
}
}
return self[obj[property].http_method](
path + property + '.json', data
);
}
})(obj, property, path);
}
}
}
methodist(endpoints, '/');
}