Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Port from Mogli to Koala #3

Merged
merged 1 commit into from

3 participants

@schneems
Owner

I ported over the code to use the Koala library instead of Mogli. You can see this code running here: https://sharp-dusk-7286.herokuapp.com/

I also added a convenience sign out url https://sharp-dusk-7286.herokuapp.com/sign_out for testing purposes.

Let me know if you have any problems

@pedro
Owner

Looks great to me!
Do we have green lights from Morten to merge/deploy?

@mortenbagai
Owner

Yes, this looks good to me. I would merge but please DO NOT DEPLOY to production before I've QA'ed with Facebook.

@pedro
Owner

Cool, just let me know when to deploy.

@schneems schneems merged commit 580da27 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2012
  1. @schneems
This page is out of date. Refresh to see the latest.
Showing with 52 additions and 63 deletions.
  1. +1 −1  Gemfile
  2. +10 −6 Gemfile.lock
  3. +19 −34 app.rb
  4. +22 −22 views/index.erb
View
2  Gemfile
@@ -1,7 +1,7 @@
source :gemcutter
gem "sinatra"
-gem "mogli"
+gem "koala"
gem "json"
gem "httparty"
gem "thin"
View
16 Gemfile.lock
@@ -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)
@@ -29,6 +33,6 @@ PLATFORMS
DEPENDENCIES
httparty
json
- mogli
+ koala
sinatra
thin
View
53 app.rb
@@ -1,5 +1,5 @@
require "sinatra"
-require "mogli"
+require 'koala'
enable :sessions
set :raise_errors, false
@@ -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
@@ -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
View
44 views/index.erb
@@ -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" />
@@ -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>
@@ -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
@@ -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>
@@ -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 %>
@@ -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 %>
@@ -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 %>
Something went wrong with that request. Please try again.