From b215850cbdc6a11553a059ba8e314cb6b61850c0 Mon Sep 17 00:00:00 2001 From: "blake.mizerany@gmail.com" Date: Tue, 11 Sep 2007 23:16:44 +0000 Subject: [PATCH] * If given format on path, extract and set in params; default to html if none specified. --- lib/sinatra/event.rb | 2 +- lib/sinatra/pretty_url.rb | 8 ++++++++ test/sinatra/dispatcher_test.rb | 2 +- test/sinatra/event_test.rb | 6 +++--- test/sinatra/pretty_url_test.rb | 16 +++++++++++++++- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/sinatra/event.rb b/lib/sinatra/event.rb index b8cd342d..a8020824 100644 --- a/lib/sinatra/event.rb +++ b/lib/sinatra/event.rb @@ -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' diff --git a/lib/sinatra/pretty_url.rb b/lib/sinatra/pretty_url.rb index 2c870de1..f118b793 100644 --- a/lib/sinatra/pretty_url.rb +++ b/lib/sinatra/pretty_url.rb @@ -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-]+)' } diff --git a/test/sinatra/dispatcher_test.rb b/test/sinatra/dispatcher_test.rb index 0faad104..939d2da4 100644 --- a/test/sinatra/dispatcher_test.rb +++ b/test/sinatra/dispatcher_test.rb @@ -56,5 +56,5 @@ describe "When a dispatcher receives a request" do get_it '/blake' Sinatra::EventManager.events.size.should.equal 0 end - + end \ No newline at end of file diff --git a/test/sinatra/event_test.rb b/test/sinatra/event_test.rb index 7a4f22d5..ed8a9561 100644 --- a/test/sinatra/event_test.rb +++ b/test/sinatra/event_test.rb @@ -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 diff --git a/test/sinatra/pretty_url_test.rb b/test/sinatra/pretty_url_test.rb index bb4dbfca..6fd5283e 100644 --- a/test/sinatra/pretty_url_test.rb +++ b/test/sinatra/pretty_url_test.rb @@ -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