Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created July 15, 2011 18:25
Show Gist options
  • Select an option

  • Save timuruski/1085227 to your computer and use it in GitHub Desktop.

Select an option

Save timuruski/1085227 to your computer and use it in GitHub Desktop.
A modular way to write helpers
module Authorization
module Service
class << self
def authorize(name)
case name
when 'timuruski', 'stevejobs' then "authorized"
else "unauthorized"
end
end
end
end
module Helpers
def authorize
Service.authorize(params[:name])
end
end
end
require 'rubygems'
require 'sinatra'
class MyApp < Sinatra::Base
helpers Authorization::Helpers
before do
@authorized = authorize
end
get "/" do
@authorized
end
run! if app_file == $0
end
require 'helpers'
require 'rack/test'
def app
MyApp
end
describe Authorization::Helpers do
include Rack::Test::Methods
it "can be stubbed" do
Authorization::Service.stub(:authorize).and_return { 'walrus' }
get "/", {:name => 'timuruski' }
last_response.body.should == 'walrus'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment