Created
May 18, 2013 19:32
-
-
Save thaniaclair/5605531 to your computer and use it in GitHub Desktop.
Using Facebook Graph API
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
/** | |
* Efetua operações relacionadas ao feed de notícias do SIS na rede social Facebook. | |
*/ | |
var feed = { | |
/** | |
* Inicializa os eventos relacionados ao Facebook. | |
*/ | |
init: function() { | |
page.loadAccessToken(); | |
}, | |
/** | |
* Carrega os dados do feed da página do Facebook sendo lida. | |
*/ | |
load: function() { | |
var url = feed.generateURL(); | |
SIS.basics.enableSpinnerAjax(false); | |
$.ajax({ "type" : "get", "url" : url, "dataType" : "json", | |
success: function(json) { | |
feed.show(json.data); | |
} | |
}); | |
SIS.basics.enableSpinnerAjax(true); | |
}, | |
/** | |
* Gera a URL para acesso aos posts no Facebook. | |
* @param page página do Facebook a ter URL gerada. | |
* @returns {String} | |
*/ | |
generateURL: function() { | |
return "https://graph.facebook.com/" + page.id + "/feed?limit=" + page.limit + | |
"&access_token=" + page.accessToken; | |
}, | |
/** | |
* Apresenta na interface os dados dos posts efetuados. | |
* @param data dados dos posts. | |
*/ | |
show: function(data) { | |
$.each(data, function(index, post) { | |
var $block = $("#fb-posts .block:first").clone(); | |
$block.find(".user a").text(post.from.name); | |
$block.find(".text").html(linkUtil.addAnchors(post.message)); | |
$block.find(".data span").text(dateUtil.formatTime(post.created_time)); | |
$("#fb-posts").append($block); | |
}); | |
$("#fb-posts .block:first").remove(); | |
$("#fb-posts").fadeIn(); | |
} | |
}; | |
/** | |
* Carrega informações da página do SIS no Facebook. | |
*/ | |
var page = { | |
id : null, | |
limit : null, | |
clientID : null, | |
clientSecret : null, | |
accessToken : null, | |
/** | |
* Carrega as informações do cliente. | |
*/ | |
loadClientInfo : function() { | |
var clientInfo = $.parseJSON($("#fb-posts .client-info").text()); | |
page.id = clientInfo.pageName; | |
page.limit = clientInfo.pageLimit; | |
page.clientID = clientInfo.clientID; | |
page.clientSecret = clientInfo.clientSecret; | |
}, | |
/** | |
* Carrega o token de acesso a página do SIS no Facebook. | |
*/ | |
loadAccessToken : function() { | |
page.loadClientInfo(); | |
var url = page.generateURL(); | |
SIS.basics.enableSpinnerAjax(false); | |
$.ajax({ "type" : "get", "url" : url, "dataType" : "text", | |
success: function(text) { | |
page.accessToken = text.replace("access_token=", ""); | |
feed.load(); | |
} | |
}); | |
SIS.basics.enableSpinnerAjax(true); | |
}, | |
/** | |
* Gera uma URL para recuperar o token de acesso da página do SIS. | |
* @returns {String} | |
*/ | |
generateURL: function() { | |
return "https://graph.facebook.com/oauth/access_token?grant_type=client_credentials" + | |
"&client_id=" + page.clientID + "&client_secret=" + page.clientSecret; | |
} | |
}; | |
/** | |
* Objeto utilitário para construção de datas. | |
*/ | |
var dateUtil = { | |
/** | |
* Adiciona um zero extra à esquerda quando menor de dez. | |
* @param number número a ter o zero extra adicionado. | |
* @returns {String} número com extra zero. | |
*/ | |
addZero: function(number) { | |
if (number < 10) return "0" + number; | |
return number; | |
}, | |
/** | |
* Formata uma data. | |
* @param strDate data em forma {String} | |
* @returns {String} a data formatada. | |
*/ | |
formatTime: function(strDate) { | |
var date = new Date(strDate); | |
return dateUtil.addZero(date.getUTCDate()) + "/" + | |
dateUtil.addZero(date.getUTCMonth() + 1) + "/" + date.getUTCFullYear() + " " + | |
dateUtil.addZero(date.getUTCHours()) + ":" + dateUtil.addZero(date.getUTCMinutes()); | |
} | |
}; | |
/** | |
* Objeto utilitário para construção de links. | |
*/ | |
var linkUtil = { | |
protocol : "http", | |
/** | |
* Adiciona links em um texto html. | |
* @param pText texto a ter links adicionados. | |
*/ | |
addAnchors : function(pText) { | |
var words = pText.trim().split(/\s+/g); | |
$.each(words, function(index, word) { | |
word = word.replace("\n", " ").replace("\r", " "); | |
if (word.indexOf(linkUtil.protocol) == 0) { | |
word = "<a href='" + word + "' target='_blank'>" + word + "</a>"; | |
words[index] = word; | |
} | |
}); | |
return words.join(" "); | |
} | |
}; | |
/** | |
* Inicia os eventos relacionados ao acesso ao Facebook. | |
*/ | |
$(function() { | |
feed.init(); | |
}); |
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
<%@ tag body-content="scriptless" pageEncoding="UTF-8" %> | |
<%@ taglib prefix="s" uri="/struts-tags" %> | |
<div id="fb-posts"> | |
<div class="header"></div> | |
<div class="block"> | |
<div class="image"> | |
<a href="https://www.facebook.com/sis.sebrae" target="_blank" title='<s:text name="social.network.fb.title" />'> | |
<img src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/373152_437150073034168_1854517345_q.jpg" /> | |
</a> | |
</div> | |
<div class="content"> | |
<span class="user"> | |
<a href="https://www.facebook.com/sis.sebrae" target="_blank" title='<s:text name="social.network.fb.title" />'></a> | |
</span> | |
<span class="text"></span> | |
<span class="data"> | |
<img src="https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif" /> | |
<span></span> | |
</span> | |
</div> | |
</div> | |
<div class="client-info"><s:property value="#application['fbClientInfo']" /></div> | |
</div> | |
<script src='<s:url value="/js/tags/facebook_feed.js"/>'></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment