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

* If given format on path, extract and set in params; default to html if none specified.

This commit is contained in:
blake.mizerany@gmail.com 2007-09-11 23:16:44 +00:00
parent 4e1fffc061
commit b215850cbd
5 changed files with 28 additions and 6 deletions

View file

@ -26,7 +26,7 @@ module Sinatra
end
def not_found
Event.new(:get, nil, false) do
Event.new(:get, 'not_found', false) do
status 404
views_dir SINATRA_ROOT + '/files'

View file

@ -3,6 +3,8 @@ module Sinatra
class PrettyUrl
attr_reader :path, :vars
VALID_FORMATS = %w( xml html )
def initialize(path)
@path = path
@param_keys = []
@ -20,11 +22,17 @@ module Sinatra
@vars[v] = matches[i]
end
end
vars[:format] = extract_format(path)
vars
end
private
def extract_format(path)
format = (path).split('.')[-1]
VALID_FORMATS.include?(format) ? format : 'html'
end
def create_regex_from_path(path)
path = path.dup
path.gsub!(/:(\w+)/) { @param_keys << $1.intern; '([\w\d-]+)' }

View file

@ -9,11 +9,11 @@ describe "Event" do
it "should return 500 if exception thrown" do
Sinatra::Environment.set_loggers stub_everything
event = Sinatra::Event.new(:get, nil) do
event = Sinatra::Event.new(:get, '/') do
raise 'whaaaa!'
end
result = event.attend(stub_everything(:params => {}))
result = event.attend(stub_everything(:params => {}, :path_info => '/'))
result.status.should.equal 500
end
@ -21,7 +21,7 @@ describe "Event" do
it "custom error if present" do
Sinatra::Environment.set_loggers stub_everything
event = Sinatra::Event.new(:get, 404) do
event = Sinatra::Event.new(:get, '404') do
body 'custom 404'
end

View file

@ -16,7 +16,21 @@ describe "When a PrettyUrl is created" do
it "should extract vars as params" do
url = Sinatra::PrettyUrl.new('/foo/:name.:format')
params = url.extract_params('/foo/blake2-test.xml')
params.should.equal :format => 'xml', :name => 'blake2-test'
params.should.equal :format => "xml", :name => 'blake2-test'
end
it "should always add a format" do
url = Sinatra::PrettyUrl.new('/foo/:name')
params = url.extract_params('/foo/blake')
params[:format].should.equal "html"
params = url.extract_params('/foo/blake.xml')
params[:format].should.equal "xml"
end
it "should default to html format if not a valid format" do
url = Sinatra::PrettyUrl.new('/foo/:name')
params = url.extract_params('/foo/blake.mizerany')
params[:format].should.equal "html"
end
end