forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.js
More file actions
162 lines (140 loc) · 2.89 KB
/
twitter.js
File metadata and controls
162 lines (140 loc) · 2.89 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Twitter
//
(function(hello){
function formatUser(o){
if(o.id){
if(o.name){
var m = o.name.split(" ");
o.first_name = m[0];
o.last_name = m[1];
}
o.thumbnail = o.profile_image_url;
}
}
function formatFriends(o){
formaterror(o);
paging(o);
if(o.users){
o.data = o.users;
for(var i=0;i<o.data.length;i++){
formatUser(o.data[i]);
}
delete o.users;
}
return o;
}
function formaterror(o){
if(o.errors){
var e = o.errors[0];
o.error = {
code : "request_failed",
message : e.message
};
}
}
//
// Paging
// Take a cursor and add it to the path
function paging(res){
// Does the response include a 'next_cursor_string'
if("next_cursor_str" in res){
// https://dev.twitter.com/docs/misc/cursoring
res['paging'] = {
next : "?cursor=" + res.next_cursor_str
};
}
}
/*
// THE DOCS SAY TO DEFINE THE USER IN THE REQUEST
// ... although its not actually required.
var user_id;
function withUserId(callback){
if(user_id){
callback(user_id);
}
else{
hello.api('twitter:/me', function(o){
user_id = o.id;
callback(o.id);
});
}
}
function sign(url){
return function(p, callback){
withUserId(function(user_id){
callback(url+'?user_id='+user_id);
});
};
}
*/
var base = "https://api.twitter.com/";
hello.init({
'twitter' : {
// Ensure that you define an oauth_proxy
oauth : {
version : "1.0a",
auth : base + "oauth/authenticate",
request : base + "oauth/request_token",
token : base + "oauth/access_token"
},
base : base + "1.1/",
get : {
"me" : 'account/verify_credentials.json',
"me/friends" : 'friends/list.json?count=@{limit|200}',
"me/following" : 'friends/list.json?count=@{limit|200}',
"me/followers" : 'followers/list.json?count=@{limit|200}',
// https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
"me/share" : 'statuses/user_timeline.json?count=@{limit|200}'
},
post : {
'me/share' : function(p,callback){
var data = p.data;
p.data = null;
// TWEET MEDIA
if( data.file ){
p.data = {
status : data.message,
"media[]" : data.file
};
callback('statuses/update_with_media.json');
}
// RETWEET?
else if( data.id ){
callback('statuses/retweet/'+data.id+'.json');
}
// TWEET
else{
callback( 'statuses/update.json?include_entities=1&status='+data.message );
}
}
},
wrap : {
me : function(res){
formaterror(res);
formatUser(res);
return res;
},
"me/friends" : formatFriends,
"me/followers" : formatFriends,
"me/following" : formatFriends,
"me/share" : function(res){
formaterror(res);
paging(res);
if(!res.error&&"length" in res){
return {data : res};
}
return res;
},
"default" : function(res){
paging(res);
return res;
}
},
xhr : function(p){
// Rely on the proxy for non-GET requests.
return (p.method!=='get');
}
}
});
})(hello);