Skip to content

Instantly share code, notes, and snippets.

@CodySwannGT
Last active August 29, 2015 14:07
Show Gist options
  • Save CodySwannGT/c98b97243e5a92319cae to your computer and use it in GitHub Desktop.
Save CodySwannGT/c98b97243e5a92319cae to your computer and use it in GitHub Desktop.
Facebook Flow
var User = angular.extend(Resource,{});
var responseFromFacebook = angular.request.params;
var user = new User({
facebookAccessToken: responseFromFacebook.token,
facebookTokenExpiresAt: responseFromFacebook.expires_at,
facebookId: responseFromFacebook.profile.id
});
user.postToServer();
class User
def facebook
fa_token = facebook_access_token
if facebook_expires_at && facebook_expires_at < Time.now
# IF WE THINK THE TOKEN HAS EXPIRED, TRY TO UPDATE IT
oauth = Koala::Facebook::OAuth.new(ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'])
new_access_info = oauth.exchange_access_token_info(facebook_access_token)
new_access_token = new_access_info["access_token"]
new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
fa_token = new_access_token
update_attributes!(:facebook_access_token => new_access_token,
:facebook_expires_at => new_access_expires_at )
end
@facebook ||= Koala::Facebook::API.new(fa_token)
block_given? ? yield(@facebook) : @facebook
rescue Koala::Facebook::APIError => e
# IF WE STILL FAIL, THE REFRESH OF THE TOKEN FAILED, SO CLEAR IT OUT AND THE USER WILL HAVE TO RECONNECT, BUT DON'T
# LET THE FAILURE THROW AN ERROR VISIBLE TO THE USER
update_attributes(facebook_access_token: nil)
logger.info e.to_s
nil
end
end
class War
def post_to_facebook
response = user.facebook.put_connections("me", "links",
link: "http://#{ENV['HOST']}/bets/#{id}".gsub(/localhost/,"test.com"),
name: "I bet ",
message: "I bet #{facebook_title}",
picture: user.avatar.thumb.url
)
if response.token_expires_at
update_attributes(facebook_expires_at: response.token_expires_at)
end
response
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment