Skip to content

Instantly share code, notes, and snippets.

@shuma
Created June 30, 2012 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuma/3025599 to your computer and use it in GitHub Desktop.
Save shuma/3025599 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :friends
def add_friends
@facebook.get_connections("me", "friends").each do |hash|
self.friends.find(:name => hash['name'], :uid => hash['id']).first_or_create
end
end
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.first_name = auth["info"]["first_name"]
user.last_name = auth["info"]["last_name"]
user.image = auth["info"]["image"]
user.email = auth["info"]["email"]
user.gender = auth["extra"]["raw_info"]["gender"]
user.location = auth["extra"]["raw_info"]["location"]["name"]
user.token = auth["credentials"]["token"]
if auth["extra"]["raw_info"]["education"]
# highschool data
user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"]
user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"]
# graduate school data
user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"]
user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"]
# school forms
#user.schoolforms = auth["extra"]["raw_info"]["education"]["type"].to_string
end
user.add_friends
user.save!
user
end
private
def facebook
@facebook ||= Koala::Facebook::API.new(token)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment