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

* Only add :format to routes if otherwise not explicitly set.

-- This allows us to remove the need to responds_to.
-- In other words... In the event of /foo.xml.. In the event of /foo.json...
This commit is contained in:
blake.mizerany@gmail.com 2007-09-25 00:03:04 +00:00
parent 0315f4bfb2
commit 3e085f52db
7 changed files with 19 additions and 71 deletions

View file

@ -1,7 +0,0 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
require 'sinatra'
get '/' do
format.html { body 'blake' }
format.xml { body 'test' }
end

View file

@ -1,6 +0,0 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
require 'sinatra'
get '/test/:name' do
params[:name]
end

View file

@ -1 +0,0 @@
<%= 1 + 3 %>

View file

@ -2,15 +2,14 @@ module Sinatra
class Route
DEFAULT_PARAMS = { :format => 'html' }
attr_reader :regex, :params
SYMBOL_FIND = /:[a-z_]+/.freeze
PARENTHETICAL_SEGMENT_STRING = "([^\/.,;?]+)".freeze
attr_reader :regex, :params
def initialize(template)
@template = template.to_s.strip
@default_params = { :format => 'html' }
@params = {}
extract_keys
genereate_route
@ -23,7 +22,7 @@ module Sinatra
if param_values
keys = @keys.size < param_values.size ? @keys.concat([:format]) : @keys
@params = DEFAULT_PARAMS.merge(@keys.zip(param_values).to_hash)
@params = @default_params.merge(@keys.zip(param_values).to_hash)
true
else
false
@ -45,12 +44,16 @@ module Sinatra
def genereate_route_with_format
template = @template.dup
template << '.:format' unless template =~ /\.:format$/
if template =~ /\.:format$|\.([\w\d]+)$/
@default_params[:format] = $1 if $1
else
template << '.:format'
end
to_regex_route(template)
end
def to_regex_route(template)
/^#{template.gsub(/\./, '\.').gsub(SYMBOL_FIND, PARENTHETICAL_SEGMENT_STRING)}$/
/^#{template.gsub(/\./, '\.').gsub(SYMBOL_FIND, PARENTHETICAL_SEGMENT_STRING)}$/
end
def genereate_route

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../helper'
describe "Spike" do
it "start" do
describe "Route" do
it "gives :format for free" do
route = Sinatra::Route.new('/foo/:test/:blake')
route.recognize('/foo/bar/baz').should.equal true
@ -11,8 +11,12 @@ describe "Spike" do
route.params.should.equal :test => 'bar', :blake => 'baz', :format => 'xml'
end
# it "test" do
# p /^(\w)$|^(\w\.\w)$/.match('b').captures rescue 'NOTHING'
# end
it "doesn't auto add :format for routes with explicit formats" do
route = Sinatra::Route.new('/foo/:test.xml')
route.recognize('/foo/bar').should.equal false
route.recognize('/foo/bar.xml').should.equal true
route.params.should.equal :test => 'bar', :format => 'xml'
end
end

View file

@ -1,3 +0,0 @@
require File.dirname(__FILE__) + '/lib/responder'
Sinatra::EventContext.send(:include, Sinatra::Responder)

View file

@ -1,42 +0,0 @@
# taken from Cheat
# get '/foo/(\w+)'
# ... important code ...
#
# respond_to do |wants|
# wants.html { render :something }
# wants.text { "Just some text." }
# wants.yaml { "Something neat!".to_yaml }
# wants.xml { "Also, XML.".to_xml }
# end
# end
module Sinatra
module Responder
def respond_to
yield response = Response.new(request.env["HTTP_ACCEPT"])
headers 'Content-Type' => response.content_type
body response.body
end
class Response
attr_reader :body, :content_type
def initialize(accept) @accept = accept end
TYPES = {
:yaml => %w[application/yaml text/yaml],
:text => %w[text/plain],
:html => %w[text/html */* application/html],
:xml => %w[application/xml],
:json => %w[application/json]
}
def method_missing(method, *args)
if TYPES[method] && @accept =~ Regexp.union(*TYPES[method])
@content_type = TYPES[method].first
@body = yield if block_given?
end
end
end
end
end