Skip to content

Commit

Permalink
initial port from mogli to koala
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Feb 25, 2012
1 parent 0da555a commit 3777269
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source :gemcutter

gem "sinatra"
gem "mogli"
gem "koala"
gem "json"
gem "httparty"
gem "thin"
16 changes: 10 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.7)
crack (0.1.8)
daemons (1.1.3)
eventmachine (0.12.10)
hashie (1.1.0)
faraday (0.7.6)
addressable (~> 2.2)
multipart-post (~> 1.1)
rack (~> 1.1)
httparty (0.7.8)
crack (= 0.1.8)
json (1.5.4)
mogli (0.0.32)
hashie (~> 1.1.0)
httparty (>= 0.4.3)
multi_json (~> 1.0.3)
koala (1.3.0)
faraday (~> 0.7.0)
multi_json (~> 1.0)
multi_json (1.0.3)
multipart-post (1.1.5)
rack (1.3.2)
sinatra (1.2.6)
rack (~> 1.1)
Expand All @@ -29,6 +33,6 @@ PLATFORMS
DEPENDENCIES
httparty
json
mogli
koala
sinatra
thin
53 changes: 19 additions & 34 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "sinatra"
require "mogli"
require 'koala'

enable :sessions
set :raise_errors, false
Expand Down Expand Up @@ -42,47 +42,33 @@ def url(path = '')
"#{scheme}://#{host}#{path}"
end

def post_to_wall_url
"https://www.facebook.com/dialog/feed?redirect_uri=#{url("/close")}&display=popup&app_id=#{@app.id}";
end

def send_to_friends_url
"https://www.facebook.com/dialog/send?redirect_uri=#{url("/close")}&display=popup&app_id=#{@app.id}&link=#{url('/')}";
end

def authenticator
@authenticator ||= Mogli::Authenticator.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
@authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
end

def first_column(item, collection)
return ' class="first-column"' if collection.index(item)%4 == 0
end
end

# the facebook session expired! reset ours and restart the process
error(Mogli::Client::HTTPException) do
session[:at] = nil
error(Koala::Facebook::APIError) do
session[:access_token] = nil
redirect "/auth/facebook"
end

get "/" do
@client = Mogli::Client.new(session[:at])

# limit queries to 15 results
@client.default_params[:limit] = 15
# Get base API Connection
@graph = Koala::Facebook::API.new(session[:access_token])

@app = Mogli::Application.find(ENV["FACEBOOK_APP_ID"], @client)
# Get public details of current application
@app = @graph.get_object(ENV["FACEBOOK_APP_ID"])

if session[:at]
@user = Mogli::User.find("me", @client)

# access friends, photos and likes directly through the user instance
@friends = @user.friends[0, 4]
@photos = @user.photos[0, 16]
@likes = @user.likes[0, 4]
if session[:access_token]
@user = @graph.get_object("me")
@friends = @graph.get_connections('me', 'friends')
@photos = @graph.get_connections('me', 'photos')
@likes = @graph.get_connections('me', 'likes').first(4)

# for other data you can always run fql
@friends_using_app = @client.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
@friends_using_app = @graph.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
end
erb :index
end
Expand All @@ -98,17 +84,16 @@ def first_column(item, collection)
end

get "/sign_out" do
session[:at] = nil
session[:access_token] = nil
redirect '/'
end

get "/auth/facebook" do
session[:at] = nil
redirect authenticator.authorize_url(:scope => FACEBOOK_SCOPE, :display => 'page')
session[:access_token] = nil
redirect authenticator.url_for_oauth_code
end

get '/auth/facebook/callback' do
client = Mogli::Client.create_from_code_and_authenticator(params[:code], authenticator)
session[:at] = client.access_token
redirect '/'
session[:access_token] = authenticator.get_access_token(params[:code])
redirect '/'
end
44 changes: 22 additions & 22 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />

<title><%= @app.name %></title>
<title><%= @app['name'] %></title>
<link rel="stylesheet" href="stylesheets/screen.css" media="Screen" type="text/css" />
<link rel="stylesheet" href="stylesheets/mobile.css" media="handheld, only screen and (max-width: 480px), only screen and (max-device-width: 480px)" type="text/css" />

Expand All @@ -17,13 +17,13 @@
<!-- over facebook. You should fill these tags in with -->
<!-- your data. To learn more about Open Graph, visit -->
<!-- 'https://developers.facebook.com/docs/opengraph/' -->
<meta property="og:title" content="<%= @app.name %>" />
<meta property="og:title" content="<%= @app['name'] %>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<%= url %>" />
<meta property="og:image" content="<%= url('/logo.png') %>" />
<meta property="og:site_name" content="<%= @app.name %>" />
<meta property="og:site_name" content="<%= @app['name'] %>" />
<meta property="og:description" content="My First App" />
<meta property="fb:app_id" content="<%= @app.id %>" />
<meta property="fb:app_id" content="<%= @app['name'] %>" />

<script type="text/javascript" src="/javascripts/jquery-1.7.1.min.js"></script>

Expand Down Expand Up @@ -96,11 +96,11 @@
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<%= @app.id %>', // App ID
channelUrl : '<%= url_no_scheme('/channel.html') %>', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
appId : "<%= @app['id'] %>", // App ID
channelUrl : "<%= url_no_scheme('/channel.html') %>", // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

// Listen to the auth.login which will be called when the user logs in
Expand Down Expand Up @@ -129,12 +129,12 @@

<header class="clearfix">
<% if @user %>
<p id="picture" style="background-image: url(https://graph.facebook.com/<%= @user.id %>/picture?type=normal)"></p>
<p id="picture" style="background-image: url(https://graph.facebook.com/<%= @user['id'] %>/picture?type=normal)"></p>
<div>
<h1>Welcome, <strong><%= @user.name %></strong></h1>
<h1>Welcome, <strong><%= @user['name'] %></strong></h1>
<p class="tagline">
This is your app
<a href="<%= @app.link %>"><%= @app.name %></a>
<a href="<%= @app['@app'] %>"><%= @app['name'] %></a>
</p>
<div id="share-app">
<p>Share your app:</p>
Expand Down Expand Up @@ -183,9 +183,9 @@
<ul class="friends">
<% @friends.each do |friend| %>
<li>
<a href="#" onclick="window.open('http://www.facebook.com/<%= friend.id %>')">
<img src="https://graph.facebook.com/<%= friend.id %>/picture?type=square" alt="<%= friend.name %>">
<%= friend.name %>
<a href="#" onclick="window.open('http://www.facebook.com/<%= friend['id']%>')">
<img src="https://graph.facebook.com/<%= friend['id'] %>/picture?type=square" alt="<%= friend['name'] %>">
<%= friend['name'] %>
</a>
</li>
<% end %>
Expand All @@ -195,10 +195,10 @@
<div class="list inline">
<h3>Recent photos</h3>
<ul class="photos">
<% @photos.each do |photo| %>
<li style="background-image: url(<%= photo.picture %>)"<%= first_column(photo, @photos) %>>
<a href="#" onclick="window.open('http://www.facebook.com/<%= photo.id %>')">
<%= photo.name %>
<% @photos.each_with_index do |photo, index| %>
<li style="background-image: url(<%= photo['picture'] %>)" class='<%= 'first-column' if (index%4).zero? %>'>
<a href="#" onclick="window.open('http://www.facebook.com/<%= photo['id'] %>')">
<%= photo['name'] %>
</a>
</li>
<% end %>
Expand All @@ -210,9 +210,9 @@
<ul class="things">
<% @likes.each do |like| %>
<li>
<a href="#" onclick="window.open('http://www.facebook.com/<%= like.id %>')">
<img src="https://graph.facebook.com/<%= like.id %>/picture?type=square" alt="<%= like.name %>">
<%= like.name %>
<a href="#" onclick="window.open('http://www.facebook.com/<%= like['id'] %>')">
<img src="https://graph.facebook.com/<%= like['id'] %>/picture?type=square" alt="<%= like['name'] %>">
<%= like['name'] %>
</a>
</li>
<% end %>
Expand Down

0 comments on commit 3777269

Please sign in to comment.