Skip to content

Instantly share code, notes, and snippets.

@bulters
Created January 29, 2014 14:04
Show Gist options
  • Save bulters/8688613 to your computer and use it in GitHub Desktop.
Save bulters/8688613 to your computer and use it in GitHub Desktop.
My Magic Authenticator implementation framework reference skeleton
class FBAuthenticator
def initialize(uid, token)
@uid = uid
@token = token
end
def authenticate(args)
raise ArgumentError unless args.has_key?(:success)
raise ArgumentError unless args.has_key?(:fail)
authenticated = magic_authentication
if Success === authenticated
args[:success].new(@uid, @token).handle
else
args[:fail].new(authenticated.to_s).handle
end
end
def magic_authentication
outer_block do |outer_response|
inner_block do |inner_response|
if outer_response && inner_response
return Success.new
else
return Error.new("VERY WOW, MUCH ERROR")
end
end
end
end
def outer_block
yield false
end
def inner_block
yield true
end
class Success
end
class Error
def initialize(msg)
@message = msg
end
def to_s
@message
end
end
end
class AuthenticatedHandler
def initialize(uid, token)
@uid, @token = uid, token
end
def handle
puts "AUTHENTICATED WITH #{@uid}/#{@token}"
end
end
class NotAuthenticatedHandler
def initialize(err)
@error = err
end
def handle
puts "BIG FAT NONO: #{@error}"
end
end
FBAuthenticator.new(123, 456).authenticate(
success: AuthenticatedHandler,
fail: NotAuthenticatedHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment