Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

urls are ready

urls are working
  • Loading branch information...
commit db8d6863b3c70c443bd5074c362a7c50d566dd06 1 parent 33fea0f
blake.mizerany@gmail.com authored
View
4 examples/hello/test.rb
@@ -1,6 +1,6 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
require 'sinatra'
-get '/test' do
- 'test'
+get '/test/:name' do
+ params[:name]
end
View
5 lib/sinatra/core_ext/array.rb
@@ -0,0 +1,5 @@
+class Array
+ def to_hash
+ self.inject({}) { |h, (k, v)| h[k] = v; h }
+ end
+end
View
5 lib/sinatra/core_ext/symbol.rb
@@ -0,0 +1,5 @@
+class Symbol
+ def to_proc
+ Proc.new { |*args| args.shift.__send__(self, *args) }
+ end
+end
View
8 lib/sinatra/dispatcher.rb
@@ -10,18 +10,16 @@ def default_headers
def call(env)
Loader.reload! if Options.environment == :development
-
+
@request = Rack::Request.new(env)
-
+
event = EventManager.determine_event(
@request.request_method.downcase.intern,
@request.path_info
)
-
+
result = event.attend(@request)
[result.status, default_headers.merge(result.headers), result.body]
- rescue => e
- logger.exception e
end
end
View
11 lib/sinatra/event.rb
@@ -17,7 +17,7 @@ def register_event(event)
def determine_event(verb, path, if_nil = :present_error)
event = events.find(method(if_nil)) do |e|
- e.verb == verb && e.path.matches?(path)
+ e.verb == verb && e.recognize(path)
end
end
@@ -112,13 +112,14 @@ def self.after_attend(filter)
def initialize(verb, path, register = true, &block)
@verb = verb
- @path = PrettyUrl.new(path.to_s)
+ @path = path
+ @route = Route.new(path)
@block = block
EventManager.register_event(self) if register
end
def attend(request)
- request.params.merge!(path.extract_params(request.path_info))
+ request.params.merge!(@route.params)
context = EventContext.new(request)
begin
result = context.instance_eval(&@block) if @block
@@ -131,6 +132,10 @@ def attend(request)
end
alias :call :attend
+ def recognize(path)
+ @route.recognize(path)
+ end
+
private
def run_through_after_filters(context)
View
62 lib/sinatra/route.rb
@@ -0,0 +1,62 @@
+module Sinatra
+
+ class Route
+
+ DEFAULT_PARAMS = { :format => 'html' }
+
+ attr_reader :regex, :params
+
+ SYMBOL_FIND = /:[a-z_]+/.freeze
+ PARENTHETICAL_SEGMENT_STRING = "([^\/.,;?]+)".freeze
+
+ def initialize(template)
+ @template = template.to_s.strip
+ @params = {}
+ extract_keys
+ genereate_route
+ end
+
+ def recognize(path)
+ @params.clear
+
+ param_values = path.match(@regex).captures.compact rescue nil
+
+ if param_values
+ keys = @keys.size < param_values.size ? @keys.concat([:format]) : @keys
+ @params = DEFAULT_PARAMS.merge(@keys.zip(param_values).to_hash)
+ true
+ else
+ false
+ end
+ end
+
+ private
+
+ def extract_keys
+ @keys = @template.scan(SYMBOL_FIND).map { |raw| eval(raw) }
+ @keys
+ end
+
+ def genereate_route_without_format
+ template = @template.dup
+ template.gsub!(/\.:format$/, '')
+ to_regex_route(template)
+ end
+
+ def genereate_route_with_format
+ template = @template.dup
+ template << '.:format' unless template =~ /\.:format$/
+ to_regex_route(template)
+ end
+
+ def to_regex_route(template)
+ /^#{template.gsub(/\./, '\.').gsub(SYMBOL_FIND, PARENTHETICAL_SEGMENT_STRING)}$/
+ end
+
+ def genereate_route
+ @regex = Regexp.union(genereate_route_without_format, genereate_route_with_format)
+ end
+
+ end
+
+end
View
9 test/sinatra/dispatcher_test.rb
@@ -67,5 +67,14 @@
status.should.equal 200
html.should.equal 'no body called'
end
+
+ it "should recognize pretty urls" do
+ Sinatra::Event.new(:get, '/test/:name') do
+ params[:name]
+ end
+
+ get_it '/test/blake'
+ body.should.equal 'blake'
+ end
end
View
18 test/sinatra/route_test.rb
@@ -0,0 +1,18 @@
+require File.dirname(__FILE__) + '/../helper'
+
+describe "Spike" do
+ it "start" do
+ route = Sinatra::Route.new('/foo/:test/:blake')
+
+ route.recognize('/foo/bar/baz').should.equal true
+ route.params.should.equal :test => 'bar', :blake => 'baz', :format => 'html'
+
+ route.recognize('/foo/bar/baz.xml').should.equal true
+ route.params.should.equal :test => 'bar', :blake => 'baz', :format => 'xml'
+ end
+
+ # it "test" do
+ # p /^(\w)$|^(\w\.\w)$/.match('b').captures rescue 'NOTHING'
+ # end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.