-
-
Save tenderlove/469882 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
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 |
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
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 == true | |
@app.call(env).tap { session.after if @after == true } | |
end | |
end |
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
class MyApp < Sinatra::Base | |
get "/" do | |
halt 403 if session.anonymous? | |
"Yay, I know you!" | |
end | |
end |
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
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