Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from jbarnette/config.ru
Created July 9, 2010 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tenderlove/469871 to your computer and use it in GitHub Desktop.
Save tenderlove/469871 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.map(&:to_s)
@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
result = @app.call env
session.after if @after
result
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])
end
def anonymous?
!user
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment