hellojs-xiaotian
Version:
A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)
223 lines (181 loc) • 4.65 kB
JavaScript
(function(hello) {
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'
},
login: function(p) {
// Reauthenticate
// https://dev.twitter.com/oauth/reference/get/oauth/authenticate
var prefix = '?force_login=true';
this.oauth.auth = this.oauth.auth.replace(prefix, '') + (p.options.force ? prefix : '');
},
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}',
// Https://dev.twitter.com/rest/reference/get/favorites/list
'me/like': 'favorites/list.json?count=@{limit|200}'
},
post: {
'me/share': function(p, callback) {
var data = p.data;
p.data = null;
var status = [];
// Change message to status
if (data.message) {
status.push(data.message);
delete data.message;
}
// If link is given
if (data.link) {
status.push(data.link);
delete data.link;
}
if (data.picture) {
status.push(data.picture);
delete data.picture;
}
// Compound all the components
if (status.length) {
data.status = status.join(' ');
}
// Tweet media
if (data.file) {
data['media[]'] = data.file;
delete data.file;
p.data = data;
callback('statuses/update_with_media.json');
}
// Retweet?
else if ('id' in data) {
callback('statuses/retweet/' + data.id + '.json');
}
// Tweet
else {
// Assign the post body to the query parameters
hello.utils.extend(p.query, data);
callback('statuses/update.json?include_entities=1');
}
},
// See: https://dev.twitter.com/rest/reference/post/favorites/create
'me/like': function(p, callback) {
var id = p.data.id;
p.data = null;
callback('favorites/create.json?id=' + id);
}
},
del: {
// See: https://dev.twitter.com/rest/reference/post/favorites/destroy
'me/like': function() {
p.method = 'post';
var id = p.data.id;
p.data = null;
callback('favorites/destroy.json?id=' + id);
}
},
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) {
res = arrayToDataResponse(res);
paging(res);
return res;
}
},
xhr: function(p) {
// Rely on the proxy for non-GET requests.
return (p.method !== 'get');
}
}
});
function formatUser(o) {
if (o.id) {
if (o.name) {
var m = o.name.split(' ');
o.first_name = m.shift();
o.last_name = m.join(' ');
}
// See: https://dev.twitter.com/overview/general/user-profile-images-and-banners
o.thumbnail = o.profile_image_url_https || o.profile_image_url;
}
return o;
}
function formatFriends(o) {
formatError(o);
paging(o);
if (o.users) {
o.data = o.users.map(formatUser);
delete o.users;
}
return o;
}
function formatError(o) {
if (o.errors) {
var e = o.errors[0];
o.error = {
code: 'request_failed',
message: e.message
};
}
}
// 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) {
// See: https://dev.twitter.com/docs/misc/cursoring
res.paging = {
next: '?cursor=' + res.next_cursor_str
};
}
}
function arrayToDataResponse(res) {
return Array.isArray(res) ? {data: res} : res;
}
/**
// The documentation says to define 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);
});
};
}
*/
})(hello);