Skip to content

Instantly share code, notes, and snippets.

@kbaum
Created June 15, 2011 15:10
Show Gist options
  • Save kbaum/1027303 to your computer and use it in GitHub Desktop.
Save kbaum/1027303 to your computer and use it in GitHub Desktop.
class FacebookLike < ActiveRecord::Base
belongs_to :user
validates :facebook_id, :user_id, :facebook_created_at, :presence=>true
def self.graph
@graph ||= Koala::Facebook::GraphAPI.new(access_token)
end
def self.update_likes_for_user!(user, opts={})
results = graph.get_connections(user.facebook_id, 'likes', opts)
loop do
results.each do |hash|
unless user.facebook_likes.exists?(:facebook_id=>hash['id'])
user.facebook_likes.create!(
:facebook_id =>hash['id'],
:name =>hash['name'],
:category =>hash['category'],
:facebook_created_at=>hash["created_time"])
end
end
results = results.next_page
break if results.nil?
end
end
def self.real_time_update!(payload)
RealtimeUpdate.new(payload).enqueue_updates!
end
class RealtimeUpdate < Struct.new(:payload)
def enqueue_updates!
remove_duplicate_uids.each do |entry|
if (user = Authentication.find_by_uid_and_provider(entry['uid'], 'facebook').try(:user))
Resque.enqueue(ResqueJob::UpdateFacebookLikes, user.id, :since=>entry['time'])
end
end
end
protected
def remove_duplicate_uids
payload['entry'].each_with_object({}) do |entry, hash|
hash[entry['uid']] ||= [] << entry
end.values.collect { |update_payloads| update_payloads.min { |entry1, entry2| entry1['time']<=>entry2['time'] } }
end
end
end
{ "entry" : [ { "changed_fields" : [ "likes" ],
"id" : "100000177646258",
"time" : 1305944959,
"uid" : "100000177646258"
},
{ "changed_fields" : [ "likes" ],
"id" : "100001645019389",
"time" : 1305944959,
"uid" : "100001645019389"
},
{ "changed_fields" : [ "likes" ],
"id" : "100001645020199",
"time" : 1305944960,
"uid" : "100001645020199"
},
{ "changed_fields" : [ "likes" ],
"id" : "100001645021160",
"time" : 1305944961,
"uid" : "100001645021160"
},
{ "changed_fields" : [ "likes" ],
"id" : "100000965063713",
"time" : 1305944961,
"uid" : "100000965063713"
},
{ "changed_fields" : [ "likes" ],
"id" : "1143374968",
"time" : 1305944961,
"uid" : "1143374968"
},
{ "changed_fields" : [ "likes" ],
"id" : "100001645020645",
"time" : 1305944963,
"uid" : "100001645020645"
},
{ "changed_fields" : [ "likes" ],
"id" : "100001257359854",
"time" : 1305944962,
"uid" : "100001257359854"
}
],
"object" : "user"
}
class FacebookUpdatesController < ApplicationController
VERIFY_TOKEN = "wekghonepkehidmo"
layout nil
def create
FacebookLike.real_time_update!(params)
render :text=>'success'
end
def index
Rails.logger.info("FacebookUpdatesController verification")
render :text=>Koala::Facebook::RealtimeUpdates.meet_challenge(params, VERIFY_TOKEN)
end
end
gem 'koala'
gem 'resque'
Weshop::Application.routes.draw do
resources :facebook_updates, :only=>[:create, :index]
updates = Koala::Facebook::RealtimeUpdates.new(:app_id => app_id, :secret => secret_key)
updates.subscribe("user", "likes", "http://www.cowhoppedcommodore.biz/facebook_updates", FacebookUpdatesController::VERIFY_TOKEN)
class ResqueJob::UpdateFacebookLikes
@queue = "facebook_likes"
def self.perform(user_id, opts={})
::Timeout.timeout(1800) do
FacebookLike.update_likes_for_user!(User.find(user_id), opts)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment