Created
January 29, 2014 14:04
-
-
Save bulters/8688613 to your computer and use it in GitHub Desktop.
My Magic Authenticator implementation framework reference skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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