Created
April 28, 2011 15:06
-
-
Save serradura/946522 to your computer and use it in GitHub Desktop.
Arquivos do post "Desenvolvendo uma Aplicação Facebook do Zero com RAILS 3 - Parte 1", link: http://blog.serraduralabs.com/desenvolvendo-uma-aplicacao-facebook-do-zero-2
This file contains hidden or 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
class DashboardController < ApplicationController | |
FB_APP_ID = 'Application ID' | |
FB_APP_SECRET = 'App Secret' | |
def index | |
if authenticated? | |
@user = graph_api_client.get_object('me') | |
@friends = graph_api_client.get_connections('me', 'friends') | |
friend_ids = @friends.collect { |friend| friend['id'] } | |
@genders = rest_api_client.rest_call('users.getInfo', {:uids => friend_ids, :fields => 'sex'}) | |
@males = @genders.count{ |friend| friend['sex'] == 'male'} | |
@females = @genders.size - @males | |
else | |
require_authentication | |
end | |
end | |
def auth | |
session[:facebook_token] = oauth.get_access_token(params['code']) | |
redirect_to root_path | |
end | |
private | |
def authenticated? | |
session[:facebook_token] | |
end | |
def require_authentication | |
redirect_to oauth.url_for_oauth_code | |
end | |
def graph_api_client | |
Koala::Facebook::GraphAPI.new(session[:facebook_token]) | |
end | |
def rest_api_client | |
Koala::Facebook::RestAPI.new(session[:facebook_token]) | |
end | |
def oauth | |
Koala::Facebook::OAuth.new(FB_APP_ID, FB_APP_SECRET, callback_url) | |
end | |
def callback_url | |
'http://localhost:3000/auth' | |
end | |
end |
This file contains hidden or 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
source 'http://rubygems.org' | |
gem 'rails', '3.0.7' | |
gem 'koala', '1.0.0.rc' | |
group :development, :test do | |
gem 'sqlite3' | |
gem 'mongrel' | |
end | |
# A gem Koala é a responsável em abstrair a API do Facebook. |
This file contains hidden or 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
</h1> | |
<p> | |
Olá <%=@user['first_name']%>, você tem <%= @friends.count %> amigos. | |
<ul> | |
<li> | |
Homens: <%= @males %> (<%= ((@males / @genders.size.to_f) * 100).round(2) %>%) | |
</li> | |
<li> | |
Mulheres: <%= @females %> (<%= ((@females / @genders.size.to_f) * 100).round(2) %>%) | |
</li> | |
</ul> | |
</p> |
This file contains hidden or 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
FbAppClassificadorDeAmigor::Application.routes.draw do | |
match "/auth" => "dashboard#auth" | |
root :to => "dashboard#index" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment