Created
November 2, 2011 13:39
-
-
Save waffle2k/1333638 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'json' | |
class AuthenticationException < StandardError ; end | |
module RequireAuth | |
def requireauth ( *args ) | |
args.each do |field| | |
old_method = instance_method(field) | |
puts "rebind stuff" | |
define_method( field ) { |*varg| | |
jsonstr = *varg.first | |
j = JSON.parse( jsonstr ) | |
raise AuthenticationException, "Not Authenticated" if j["authenticated"] < 1 | |
old_method.bind(self).call(*varg) | |
} | |
end | |
end | |
end | |
class Foo | |
extend RequireAuth | |
def bar( json ) | |
puts "This is bar" | |
end | |
requireauth :bar | |
end | |
jsonstr = '{ "foo": "bar", "authenticated": 0 }' | |
fb = Foo.new | |
fb.bar( jsonstr ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment