Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Port from Mogli to Koala #3

Merged
merged 1 commit into from over 2 years ago

3 participants

Richard Schneeman Pedro Belo Morten Bagai
Richard Schneeman
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 Belo
Owner

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

Morten Bagai
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 Belo
Owner

Cool, just let me know when to deploy.

Richard Schneeman schneems merged commit 580da27 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 25, 2012
Richard Schneeman schneems initial port from mogli to koala 3777269
This page is out of date. Refresh to see the latest.

Showing 4 changed files with 52 additions and 63 deletions. Show diff stats Hide diff stats

  1. +1 1  Gemfile
  2. +10 6 Gemfile.lock
  3. +19 34 app.rb
  4. +22 22 views/index.erb
2  Gemfile
... ... @@ -1,7 +1,7 @@
1 1 source :gemcutter
2 2
3 3 gem "sinatra"
4   -gem "mogli"
  4 +gem "koala"
5 5 gem "json"
6 6 gem "httparty"
7 7 gem "thin"
16 Gemfile.lock
... ... @@ -1,18 +1,22 @@
1 1 GEM
2 2 remote: http://rubygems.org/
3 3 specs:
  4 + addressable (2.2.7)
4 5 crack (0.1.8)
5 6 daemons (1.1.3)
6 7 eventmachine (0.12.10)
7   - hashie (1.1.0)
  8 + faraday (0.7.6)
  9 + addressable (~> 2.2)
  10 + multipart-post (~> 1.1)
  11 + rack (~> 1.1)
8 12 httparty (0.7.8)
9 13 crack (= 0.1.8)
10 14 json (1.5.4)
11   - mogli (0.0.32)
12   - hashie (~> 1.1.0)
13   - httparty (>= 0.4.3)
14   - multi_json (~> 1.0.3)
  15 + koala (1.3.0)
  16 + faraday (~> 0.7.0)
  17 + multi_json (~> 1.0)
15 18 multi_json (1.0.3)
  19 + multipart-post (1.1.5)
16 20 rack (1.3.2)
17 21 sinatra (1.2.6)
18 22 rack (~> 1.1)
@@ -29,6 +33,6 @@ PLATFORMS
29 33 DEPENDENCIES
30 34 httparty
31 35 json
32   - mogli
  36 + koala
33 37 sinatra
34 38 thin
53 app.rb
... ... @@ -1,5 +1,5 @@
1 1 require "sinatra"
2   -require "mogli"
  2 +require 'koala'
3 3
4 4 enable :sessions
5 5 set :raise_errors, false
@@ -42,47 +42,33 @@ def url(path = '')
42 42 "#{scheme}://#{host}#{path}"
43 43 end
44 44
45   - def post_to_wall_url
46   - "https://www.facebook.com/dialog/feed?redirect_uri=#{url("/close")}&display=popup&app_id=#{@app.id}";
47   - end
48   -
49   - def send_to_friends_url
50   - "https://www.facebook.com/dialog/send?redirect_uri=#{url("/close")}&display=popup&app_id=#{@app.id}&link=#{url('/')}";
51   - end
52   -
53 45 def authenticator
54   - @authenticator ||= Mogli::Authenticator.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
  46 + @authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
55 47 end
56 48
57   - def first_column(item, collection)
58   - return ' class="first-column"' if collection.index(item)%4 == 0
59   - end
60 49 end
61 50
62 51 # the facebook session expired! reset ours and restart the process
63   -error(Mogli::Client::HTTPException) do
64   - session[:at] = nil
  52 +error(Koala::Facebook::APIError) do
  53 + session[:access_token] = nil
65 54 redirect "/auth/facebook"
66 55 end
67 56
68 57 get "/" do
69   - @client = Mogli::Client.new(session[:at])
70   -
71   - # limit queries to 15 results
72   - @client.default_params[:limit] = 15
  58 + # Get base API Connection
  59 + @graph = Koala::Facebook::API.new(session[:access_token])
73 60
74   - @app = Mogli::Application.find(ENV["FACEBOOK_APP_ID"], @client)
  61 + # Get public details of current application
  62 + @app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
75 63
76   - if session[:at]
77   - @user = Mogli::User.find("me", @client)
78   -
79   - # access friends, photos and likes directly through the user instance
80   - @friends = @user.friends[0, 4]
81   - @photos = @user.photos[0, 16]
82   - @likes = @user.likes[0, 4]
  64 + if session[:access_token]
  65 + @user = @graph.get_object("me")
  66 + @friends = @graph.get_connections('me', 'friends')
  67 + @photos = @graph.get_connections('me', 'photos')
  68 + @likes = @graph.get_connections('me', 'likes').first(4)
83 69
84 70 # for other data you can always run fql
85   - @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")
  71 + @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")
86 72 end
87 73 erb :index
88 74 end
@@ -98,17 +84,16 @@ def first_column(item, collection)
98 84 end
99 85
100 86 get "/sign_out" do
101   - session[:at] = nil
  87 + session[:access_token] = nil
102 88 redirect '/'
103 89 end
104 90
105 91 get "/auth/facebook" do
106   - session[:at] = nil
107   - redirect authenticator.authorize_url(:scope => FACEBOOK_SCOPE, :display => 'page')
  92 + session[:access_token] = nil
  93 + redirect authenticator.url_for_oauth_code
108 94 end
109 95
110 96 get '/auth/facebook/callback' do
111   - client = Mogli::Client.create_from_code_and_authenticator(params[:code], authenticator)
112   - session[:at] = client.access_token
113   - redirect '/'
  97 + session[:access_token] = authenticator.get_access_token(params[:code])
  98 + redirect '/'
114 99 end
44 views/index.erb
@@ -4,7 +4,7 @@
4 4 <meta charset="utf-8" />
5 5 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
6 6
7   - <title><%= @app.name %></title>
  7 + <title><%= @app['name'] %></title>
8 8 <link rel="stylesheet" href="stylesheets/screen.css" media="Screen" type="text/css" />
9 9 <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" />
10 10
@@ -17,13 +17,13 @@
17 17 <!-- over facebook. You should fill these tags in with -->
18 18 <!-- your data. To learn more about Open Graph, visit -->
19 19 <!-- 'https://developers.facebook.com/docs/opengraph/' -->
20   - <meta property="og:title" content="<%= @app.name %>" />
  20 + <meta property="og:title" content="<%= @app['name'] %>" />
21 21 <meta property="og:type" content="website" />
22 22 <meta property="og:url" content="<%= url %>" />
23 23 <meta property="og:image" content="<%= url('/logo.png') %>" />
24   - <meta property="og:site_name" content="<%= @app.name %>" />
  24 + <meta property="og:site_name" content="<%= @app['name'] %>" />
25 25 <meta property="og:description" content="My First App" />
26   - <meta property="fb:app_id" content="<%= @app.id %>" />
  26 + <meta property="fb:app_id" content="<%= @app['name'] %>" />
27 27
28 28 <script type="text/javascript" src="/javascripts/jquery-1.7.1.min.js"></script>
29 29
@@ -96,11 +96,11 @@
96 96 <script type="text/javascript">
97 97 window.fbAsyncInit = function() {
98 98 FB.init({
99   - appId : '<%= @app.id %>', // App ID
100   - channelUrl : '<%= url_no_scheme('/channel.html') %>', // Channel File
101   - status : true, // check login status
102   - cookie : true, // enable cookies to allow the server to access the session
103   - xfbml : true // parse XFBML
  99 + appId : "<%= @app['id'] %>", // App ID
  100 + channelUrl : "<%= url_no_scheme('/channel.html') %>", // Channel File
  101 + status : true, // check login status
  102 + cookie : true, // enable cookies to allow the server to access the session
  103 + xfbml : true // parse XFBML
104 104 });
105 105
106 106 // Listen to the auth.login which will be called when the user logs in
@@ -129,12 +129,12 @@
129 129
130 130 <header class="clearfix">
131 131 <% if @user %>
132   - <p id="picture" style="background-image: url(https://graph.facebook.com/<%= @user.id %>/picture?type=normal)"></p>
  132 + <p id="picture" style="background-image: url(https://graph.facebook.com/<%= @user['id'] %>/picture?type=normal)"></p>
133 133 <div>
134   - <h1>Welcome, <strong><%= @user.name %></strong></h1>
  134 + <h1>Welcome, <strong><%= @user['name'] %></strong></h1>
135 135 <p class="tagline">
136 136 This is your app
137   - <a href="<%= @app.link %>"><%= @app.name %></a>
  137 + <a href="<%= @app['@app'] %>"><%= @app['name'] %></a>
138 138 </p>
139 139 <div id="share-app">
140 140 <p>Share your app:</p>
@@ -183,9 +183,9 @@
183 183 <ul class="friends">
184 184 <% @friends.each do |friend| %>
185 185 <li>
186   - <a href="#" onclick="window.open('http://www.facebook.com/<%= friend.id %>')">
187   - <img src="https://graph.facebook.com/<%= friend.id %>/picture?type=square" alt="<%= friend.name %>">
188   - <%= friend.name %>
  186 + <a href="#" onclick="window.open('http://www.facebook.com/<%= friend['id']%>')">
  187 + <img src="https://graph.facebook.com/<%= friend['id'] %>/picture?type=square" alt="<%= friend['name'] %>">
  188 + <%= friend['name'] %>
189 189 </a>
190 190 </li>
191 191 <% end %>
@@ -195,10 +195,10 @@
195 195 <div class="list inline">
196 196 <h3>Recent photos</h3>
197 197 <ul class="photos">
198   - <% @photos.each do |photo| %>
199   - <li style="background-image: url(<%= photo.picture %>)"<%= first_column(photo, @photos) %>>
200   - <a href="#" onclick="window.open('http://www.facebook.com/<%= photo.id %>')">
201   - <%= photo.name %>
  198 + <% @photos.each_with_index do |photo, index| %>
  199 + <li style="background-image: url(<%= photo['picture'] %>)" class='<%= 'first-column' if (index%4).zero? %>'>
  200 + <a href="#" onclick="window.open('http://www.facebook.com/<%= photo['id'] %>')">
  201 + <%= photo['name'] %>
202 202 </a>
203 203 </li>
204 204 <% end %>
@@ -210,9 +210,9 @@
210 210 <ul class="things">
211 211 <% @likes.each do |like| %>
212 212 <li>
213   - <a href="#" onclick="window.open('http://www.facebook.com/<%= like.id %>')">
214   - <img src="https://graph.facebook.com/<%= like.id %>/picture?type=square" alt="<%= like.name %>">
215   - <%= like.name %>
  213 + <a href="#" onclick="window.open('http://www.facebook.com/<%= like['id'] %>')">
  214 + <img src="https://graph.facebook.com/<%= like['id'] %>/picture?type=square" alt="<%= like['name'] %>">
  215 + <%= like['name'] %>
216 216 </a>
217 217 </li>
218 218 <% end %>

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.