Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created November 2, 2011 13:39
Show Gist options
  • Save waffle2k/1333638 to your computer and use it in GitHub Desktop.
Save waffle2k/1333638 to your computer and use it in GitHub Desktop.
#!/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