Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

FB keys kept in keys.yaml file. FB urls and koala integration

  • Loading branch information...
commit a3f60e867589c92adc9d5faa4710fd5112990029 1 parent 0a32764
@zmcartor authored
Showing with 111 additions and 4 deletions.
  1. +71 −4 sinatra_social/app.rb
  2. +40 −0 sinatra_social/views/index.erb
View
75 sinatra_social/app.rb
@@ -1,18 +1,85 @@
require 'sinatra/base'
require './mid'
+require 'sinatra/assetpack'
+require 'yaml'
+require 'koala'
+
class Social < Sinatra::Base
use Middleware_basic
+ register Sinatra::AssetPack
+
+ assets {
+ serve '/js', from: 'app/js'
+ serve '/css', from: 'app/css'
+ serve '/images', from: 'app/images'
+
+ js_compression :jsmin # Optional
+ }
set :rdoc, :layout_engine => :erb
set :public_folder, File.dirname(__FILE__) + '/public'
+ set :port , 9000
- set :blah , 0
+FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags'
+ # the facebook session expired! reset ours and restart the process
+ error(Koala::Facebook::APIError) do
+ session[:access_token] = nil
+ redirect "/auth/facebook"
+ end
+
+ #helpers block. functions are available only in request blocks
+ helpers do
+ def authenticator
+ @keys = YAML::load_file('keys.yaml')
+ @authenticator ||= Koala::Facebook::OAuth.new(@keys["app_id"], @keys["secret"], url("/auth/facebook/callback"))
+ end
- get '/' do
- 'oks then'
end
-end
+ get "/" do
+ # Get base API Connection
+ @graph = Koala::Facebook::API.new(session[:access_token])
+ # Get public details of current application
+ @app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
+
+ 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 = @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
+
+ # used by Canvas apps - redirect the POST to be a regular GET
+ post "/" do
+ redirect "/"
+ end
+
+ # used to close the browser window opened to post to wall/send to friends
+ get "/close" do
+ "<body onload='window.close();'/>"
+ end
+
+ get "/sign_out" do
+ session[:access_token] = nil
+ redirect '/'
+ end
+
+ get "/auth/facebook" do
+ session[:access_token] = nil
+ redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
+ end
+
+ get '/auth/facebook/callback' do
+ session[:access_token] = authenticator.get_access_token(params[:code])
+ redirect '/'
+ end
+
+end
View
40 sinatra_social/views/index.erb
@@ -0,0 +1,40 @@
+<header class="clearfix">
+ <% if @user %>
+ <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>
+ <p class="tagline">
+ POWERED BY SINATRA
+ <a href="<%= @app['@app'] %>"><%= @app['name'] %></a>
+ </p>
+ <div id="share-app">
+ <p>Share your app:</p>
+ <ul>
+ <li>
+ <a href="#" class="facebook-button" id="postToWall" data-url="<%= url %>">
+ <span class="plus">Post to Wall</span>
+ </a>
+ </li>
+ <li>
+ <a href="#" class="facebook-button speech-bubble" id="sendToFriends" data-url="<%= url %>">
+ <span class="speech-bubble">Send Message</span>
+ </a>
+ </li>
+ <li>
+ <a href="#" class="facebook-button apprequests" id="sendRequest" data-message="Test this awesome app">
+ <span class="apprequests">Send Requests</span>
+ </a>
+ </li>
+ </ul>
+ </div>
+ </div>
+ <% else %>
+ <div>
+ <h1>Welcome</h1>
+ <a href='/auth/facebook' class="fb_button fb_button_medium">
+ <span class="fb_button_text">Log In</span>
+ </a>
+ </div>
+
+ <% end %>
+ </header>
Please sign in to comment.
Something went wrong with that request. Please try again.