Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from jbarnette/intercession.rb
Created July 9, 2010 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/469892 to your computer and use it in GitHub Desktop.
Save tenderlove/469892 to your computer and use it in GitHub Desktop.
require "intercession"
require "myapp"
require "myapp/session_extras"
use Rack::Session::Cookie, :secret => "I'll punchasize your face for FREE!"
use Intercession, MyApp::SessionExtras
run MyApp
class Intercession
def initialize app, behavior
@app = app
@behavior = behavior
methods = []
@behavior.instance_methods.each { |m| methods << "#{m}" }
@before = !!methods.include? "before"
@after = !!methods.include? "after"
end
def call env
if session = env["rack.session"]
session.extend @behavior
end
session.before if @before == true
@app.call(env).tap { session.after if @after == true }
end
end
class MyApp < Sinatra::Base
get "/" do
halt 403 if session.anonymous?
"Yay, I know you!"
end
end
class MyApp
module SessionExtras
def user
@user ||= self[:user_id] ? User.find_by_id(self[:user_id]) : nil
end
def anonymous?
!user.nil?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment