Permalink
Browse files

Updates

  • Loading branch information...
1 parent dcd25fb commit 27de0b4f564ffafeb8fbd6fadfa388587fa9c1ca @alevy alevy committed Feb 2, 2012
View
@@ -5,3 +5,5 @@ gem "mogli"
gem "json"
gem "httparty"
gem "thin"
+gem "memcachier"
+gem "dalli"
View
@@ -3,11 +3,13 @@ GEM
specs:
crack (0.1.8)
daemons (1.1.3)
+ dalli (1.1.3)
eventmachine (0.12.10)
hashie (1.1.0)
httparty (0.7.8)
crack (= 0.1.8)
json (1.5.4)
+ memcachier (0.0.1)
mogli (0.0.32)
hashie (~> 1.1.0)
httparty (>= 0.4.3)
@@ -27,8 +29,10 @@ PLATFORMS
ruby
DEPENDENCIES
+ dalli
httparty
json
+ memcachier
mogli
sinatra
thin
View
79 app.rb
@@ -4,15 +4,7 @@
enable :sessions
set :raise_errors, false
set :show_exceptions, false
-
-# Scope defines what permissions that we are asking the user to grant.
-# In this example, we are asking for the ability to publish stories
-# about using the app, access to what the user likes, and to be able
-# to use their pictures. You should rewrite this scope with whatever
-# permissions your app needs.
-# See https://developers.facebook.com/docs/reference/api/permissions/
-# for a full list of permissions
-FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags'
+set :cache, Dalli::Client.new
unless ENV["FACEBOOK_APP_ID"] && ENV["FACEBOOK_SECRET"]
abort("missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET with your app credentials")
@@ -31,21 +23,22 @@ def url(path)
base + 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"))
end
-
- def first_column(item, collection)
- return ' class="first-column"' if collection.index(item)%4 == 0
+
+ def client
+ @client ||= Mogli::Client.new(session[:at])
end
+
+ def friends
+ @friends ||= settings.cache.fetch("#{session[:at]}:friends") do
+ friends = client.fql_query("SELECT uid, name, current_location FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())")
+ settings.cache.set("#{session[:at]}:friends", friends, :expires_in => 60)
+ friends
+ end
+ end
+
end
# the facebook session expired! reset ours and restart the process
@@ -58,40 +51,42 @@ def first_column(item, collection)
redirect "/auth/facebook" unless session[:at]
@client = Mogli::Client.new(session[:at])
- # limit queries to 15 results
- @client.default_params[:limit] = 15
-
- @app = Mogli::Application.find(ENV["FACEBOOK_APP_ID"], @client)
- @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]
-
- # 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")
+ @user = settings.cache.fetch("#{session[:at]}:me") do
+ user = Mogli::User.find("me", @client)
+ settings.cache.set("#{session[:at]}:me", user, :expires_in => 60)
+ user
+ end
+
+ friends
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();'/>"
+get "/search" do
+
+ content_type :json
+ query = params[:term].downcase
+ result = friends.select do |friend|
+ if friend["current_location"]
+ friend["name"].downcase.index(query)
+ else
+ nil
+ end
+ end
+ result = result.map do |friend|
+ {:label => friend["name"], :value => friend["current_location"]["name"] }
+ end
+ result.to_json
end
get "/auth/facebook" do
session[:at]=nil
- redirect authenticator.authorize_url(:scope => FACEBOOK_SCOPE, :display => 'page')
+ redirect authenticator.authorize_url(:scope => "user_location,friends_location", :display => 'page')
end
get '/auth/facebook/callback' do
client = Mogli::Client.create_from_code_and_authenticator(params[:code], authenticator)
session[:at] = client.access_token
+ puts session[:at]
redirect '/'
end
@@ -0,0 +1,12 @@
+$(function() {
+ $("#friend_name").autocomplete({
+ source: "/search",
+ select: function( event, ui ) {
+ if (ui.item) {
+ window.location.href = "mailto:" + ui.item.value;
+ }
+ }
+ }).data("autocomplete")._renderItem = function(ul, item) {
+ return $("<li></li>").append("<a>" + item.label + " (" + item.value + ")</a>").appendTo(ul);
+ };
+});
Oops, something went wrong.

0 comments on commit 27de0b4

Please sign in to comment.