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:
parent
0315f4bfb2
commit
3e085f52db
7 changed files with 19 additions and 71 deletions
|
@ -1,7 +0,0 @@
|
|||
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
|
||||
require 'sinatra'
|
||||
|
||||
get '/' do
|
||||
format.html { body 'blake' }
|
||||
format.xml { body 'test' }
|
||||
end
|
|
@ -1,6 +0,0 @@
|
|||
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
|
||||
require 'sinatra'
|
||||
|
||||
get '/test/:name' do
|
||||
params[:name]
|
||||
end
|
|
@ -1 +0,0 @@
|
|||
<%= 1 + 3 %>
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
3
vendor/responder/init.rb
vendored
3
vendor/responder/init.rb
vendored
|
@ -1,3 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/lib/responder'
|
||||
|
||||
Sinatra::EventContext.send(:include, Sinatra::Responder)
|
42
vendor/responder/lib/responder.rb
vendored
42
vendor/responder/lib/responder.rb
vendored
|
@ -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
|
Loading…
Reference in a new issue