Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* 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...
  • Loading branch information...
commit 3e085f52dbfa8f96325cc57e61465d0a1a958bad 1 parent 0315f4b
blake.mizerany@gmail.com authored
View
7 examples/calendar/calendar.rb
@@ -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
6 examples/hello/test.rb
@@ -1,6 +0,0 @@
-$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
-require 'sinatra'
-
-get '/test/:name' do
- params[:name]
-end
View
1  examples/hello/views/index.erb
@@ -1 +0,0 @@
-<%= 1 + 3 %>
View
17 lib/sinatra/route.rb
@@ -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 @@ def recognize(path)
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 @@ def genereate_route_without_format
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
14 test/sinatra/route_test.rb
@@ -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 @@
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
3  vendor/responder/init.rb
@@ -1,3 +0,0 @@
-require File.dirname(__FILE__) + '/lib/responder'
-
-Sinatra::EventContext.send(:include, Sinatra::Responder)
View
42 vendor/responder/lib/responder.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.