Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Forked from tenderlove/config.ru
Created July 9, 2010 19:01
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 jbarnette/469876 to your computer and use it in GitHub Desktop.
Save jbarnette/469876 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
unless (session = env["rack.session"]).nil?
session.extend @behavior
end
session.before unless @before.nil?
@app.call(env).tap { session.after unless @after.nil? }
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