Last active
October 24, 2015 19:44
-
-
Save jaenmedina/deabff07732cd4b50220 to your computer and use it in GitHub Desktop.
A simple facebook api wrapper I've been using for a couple of projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is called when the response is positive (for example when the status is 'connected'). | |
* @callback successCallback | |
*/ | |
/** | |
* This is called when the response is negative (for example when the status is not 'connected'). | |
* @callback failureCallback | |
*/ | |
/** | |
* Generic callback function. | |
* @callback callback | |
*/ | |
function FacebookApp(){ | |
/** | |
* @see {@link https://developers.facebook.com/apps} | |
*/ | |
this.appId = ''; | |
/** | |
* @see {@link https://developers.facebook.com/docs/facebook-login/permissions/v2.2} | |
*/ | |
this.scope = 'public_profile,email'; | |
/** | |
* @see {@link https://developers.facebook.com/docs/games/requests/v2.2} | |
*/ | |
this.inviteTitle = ''; | |
this.inviteMessage = ''; | |
this.inviteMaxRecipients = 5; | |
/** | |
* @see {@link https://developers.facebook.com/docs/javascript/reference/FB.init/v2.2} | |
*/ | |
this.initApp = function(){ | |
FB.init({ | |
appId : this.appId, | |
xfbml : true, | |
version : 'v2.2' | |
}); | |
}; | |
/** | |
* @param {successCallback} isLoggedInCallback | |
* @param {failureCallback} notLoggedInCallback | |
* @see {@link https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus} | |
*/ | |
this.isLoggedIntoApp = function(isLoggedInCallback, notLoggedInCallback){ | |
FB.getLoginStatus(function(response) { | |
if(response.status === 'connected'){ | |
isLoggedInCallback(response); | |
} | |
else{ | |
notLoggedInCallback(); | |
} | |
}); | |
}; | |
/** | |
* @param {successCallback} loginAcceptedCallback | |
* @param {failureCallback} loginRejectedCallback | |
* @see {@link https://developers.facebook.com/docs/reference/javascript/FB.login/v2.2} | |
*/ | |
this.login = function(loginAcceptedCallback, loginRejectedCallback){ | |
FB.login(function(response){ | |
if(response.status === 'connected'){ | |
loginAcceptedCallback(response); | |
} | |
else{ | |
loginRejectedCallback(response); | |
} | |
},{scope: this.scope}); | |
}; | |
/** | |
* @param {callback} callback | |
* @see {@link https://developers.facebook.com/docs/javascript/reference/FB.api} | |
* @see {@link https://developers.facebook.com/docs/graph-api/reference/v2.2/user} | |
*/ | |
this.getUserInfo = function(callback){ | |
FB.api('/me', function(response) { | |
callback(response); | |
}); | |
}; | |
/** | |
* @param {callback} callback | |
* @see {@link https://developers.facebook.com/docs/javascript/reference/FB.ui} | |
* @see {@link https://developers.facebook.com/docs/games/requests/v2.2} | |
*/ | |
this.inviteFriends = function(callback){ | |
FB.ui({ | |
method: 'apprequests', | |
filters: ['app_non_users'], | |
title: this.inviteTitle, | |
message: this.inviteMessage, | |
max_recipients: this.inviteMaxRecipients | |
}, | |
function(response){ | |
callback(); | |
}); | |
}; | |
/** | |
* @param {callback} callback | |
* @see {@link https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.3} | |
*/ | |
this.addAfterPluginRenderCallback = function(callback){ | |
FB.Event.subscribe("xfbml.render", callback); | |
}; | |
/** | |
* @param {Object} parameters | |
* @param {successCallback} successCallback | |
* @param {failureCallback} failureCallback | |
* @see {@link https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.3} | |
*/ | |
this.showFeedDialog = function(parameters, successCallback, failureCallback){ | |
parameters.method = 'feed'; | |
FB.ui(parameters, function(response) { | |
if(response && response.post_id){ | |
successCallback(); | |
} | |
else{ | |
failureCallback(); | |
} | |
}); | |
}; | |
/** | |
* @param {string} photoUrl | |
* @param {successCallback} successCallback | |
* @param {failureCallback} failureCallback | |
* @see {@link https://developers.facebook.com/docs/graph-api/reference/user/photos/} | |
*/ | |
this.uploadPhoto = function(photoUrl, successCallback, failureCallback){ | |
FB.api( | |
"/me/photos", | |
"POST", | |
{ | |
"url": photoUrl | |
}, | |
function (response) { | |
if (response && !response.error) { | |
successCallback(); | |
} | |
else{ | |
failureCallback(); | |
} | |
} | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment