Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created December 24, 2011 16:58
Show Gist options
  • Save gumayunov/1517786 to your computer and use it in GitHub Desktop.
Save gumayunov/1517786 to your computer and use it in GitHub Desktop.
SocialHamster lib contept
# app/controllers/social_friendlists_controller.rb
def show
frineds = SocialService.get_friends(user_id, params[:provider])
rescue SocialService::NoCredentialsError
redirect_to auth_path(prams[:provider]) #to omniauth's /auth/facebook/
rescue SocialService::Error
flash[:error] = "Cant access #{params[:provider].capitalize}. Try again later"
redirect_to social_friendlists_path
end
# lib/social_hamster.rb
class SocialHamster
def self.connect(provider, credentials)
provider.constantize.new(credentials)
end
end
# lib/social_hamster/base_gateway.rb
class SocialHamster::BaseGateway
def initialize(credentials)
end
def get_friends
end
def invite(uid)
end
end
# lib/social_hamster/base_profile.rb
# Profile can be user to unify omniauth extra in application.
require "forwardable"
class SocialHamster::BaseProfile
extend Forwardable
def_delegator :@data, :first_name, :last_name, :birthday
def_delegator :@data, :uin, :email, :avatar_url, :profile_url
def intialize(raw_profile)
@data = OpenScruct.new(convert(raw_profile.with_indifferent_access))
end
end
# lib/social_hamster/facebook/gateway.rb
class SocialHamster::Facebook::Gateway < SocialHamster::BaseGateway
end
# lib/social_hamster/facebook/profile.rb
class SocialHamster::Facebook::Profile < SocialHamster::BaseProfile
def convert(raw)
{
:gender => raw[:gender],
:birthday => raw[:birthday].present? ? raw["birthday"].sub(/^(\d\d)\/(\d\d)\//, '\2.\1.') : nil
....
}
end
end
# app/services/social_service.rb
class SocialService
def self.get_friends(user_id, provider)
auth = User.find(user_id).authentications.find_by_provider(provider)
if auth.blank? || auth.credentials.blank?
raise NoCredentialsError.new
end
connection = SocialHamster.connect(provider, auth.credentials)
return connection.get_friends
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment