Skip to content

Instantly share code, notes, and snippets.

@mokevnin
Created February 4, 2012 07:03
Show Gist options
  • Select an option

  • Save mokevnin/1735981 to your computer and use it in GitHub Desktop.

Select an option

Save mokevnin/1735981 to your computer and use it in GitHub Desktop.
require "errors"
class GoogleService
private_class_method :new
class << self
def register(data_hash)
data = AuthData.new(data_hash)
auth = User::Google.find_by_uid(data.uid)
if auth
raise UserBlockedError if auth.user.blocked?
return auth.user
end
if user = User.find_by_email(data.email)
raise UserBlockedError if user.blocked?
else
user = UserGoogleRegistrationType.new
user.password = SecureApp.generate_password
end
UserPopulator.via_google(user, data)
user.active? ? user.save! : user.activate!
user
end
def create_link(user, data_hash)
data = AuthData.new(data_hash)
auth = User::Google.find_by_uid(data.uid)
raise AlreadyLinkedError if auth && auth.user == user
if auth && auth.user != user
raise UserBlockedError if auth.user.blocked?
auth.destroy
user.google.destroy if user.google
end
UserPopulator.via_google(user, data)
user.save!
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment