1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

less code = great code

This commit is contained in:
Blake Mizerany 2008-02-20 23:46:29 -08:00
parent becd6d8ab0
commit d41cccae7b
2 changed files with 16 additions and 25 deletions

View file

@ -134,12 +134,14 @@ module Sinatra
module ResponseHelpers module ResponseHelpers
def redirect(path) def redirect(path, *args)
throw :halt, Redirect.new(path) status(302)
headers 'Location' => path
throw :halt, *args
end end
def send_file(filename) def send_file(filename)
throw :halt, SendFile.new(filename) throw :halt, File.read(filename)
end end
def headers(header = nil) def headers(header = nil)
@ -258,28 +260,6 @@ module Sinatra
end end
class Redirect
def initialize(path)
@path = path
end
def to_result(cx, *args)
cx.status(302)
cx.header.merge!('Location' => @path)
cx.body = ''
end
end
class SendFile
def initialize(filename)
@filename = filename
end
def to_result(cx, *args)
cx.body = File.read(@filename)
end
end
class Application class Application
attr_reader :events, :layouts, :default_options, :filters attr_reader :events, :layouts, :default_options, :filters

View file

@ -45,6 +45,17 @@ context "Sinatra" do
body.should.equal 'Mizerany' body.should.equal 'Mizerany'
end end
specify "renders a body with a redirect" do
Sinatra::EventContext.any_instance.expects(:foo).returns('blah')
get "/" do
redirect 'foo', :foo
end
get_it '/'
should.be.redirection
headers['Location'].should.equal 'foo'
body.should.equal 'blah'
end
specify "body sets content and ends event" do specify "body sets content and ends event" do
Sinatra::EventContext.any_instance.expects(:foo).never Sinatra::EventContext.any_instance.expects(:foo).never