mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
start 2
This commit is contained in:
parent
b62130824d
commit
03626a1ec4
1 changed files with 65 additions and 0 deletions
65
lib/sinatra2.rb
Normal file
65
lib/sinatra2.rb
Normal file
|
@ -0,0 +1,65 @@
|
|||
require "rubygems"
|
||||
require "rack"
|
||||
|
||||
class String
|
||||
def to_param
|
||||
URI.escape(self)
|
||||
end
|
||||
|
||||
def from_param
|
||||
URI.unescape(self)
|
||||
end
|
||||
end
|
||||
|
||||
class Symbol
|
||||
def to_proc
|
||||
Proc.new { |*args| args.shift.__send__(self, *args) }
|
||||
end
|
||||
end
|
||||
|
||||
class Array
|
||||
def to_hash
|
||||
self.inject({}) { |h, (k, v)| h[k] = v; h }
|
||||
end
|
||||
end
|
||||
|
||||
module Sinatra
|
||||
extend self
|
||||
|
||||
def request_types
|
||||
@request_types ||= %w(GET PUT POST DELETE)
|
||||
end
|
||||
|
||||
def events
|
||||
@events ||= Hash.new do |hash, key|
|
||||
hash[key] = [] if request_types.include?(key)
|
||||
end
|
||||
end
|
||||
|
||||
class Route
|
||||
|
||||
URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
|
||||
PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
|
||||
|
||||
def initialize(path, &b)
|
||||
@path, @block = path, b
|
||||
@param_keys = []
|
||||
regex = path.to_s.gsub(PARAM) do
|
||||
@param_keys << $1.intern
|
||||
"(#{URI_CHAR}+)"
|
||||
end
|
||||
@pattern = /^#{regex}$/
|
||||
@struct = Struct.new(:block, :params)
|
||||
end
|
||||
|
||||
def match(path)
|
||||
return nil unless path =~ @pattern
|
||||
params = @param_keys.zip($~.captures.map(&:from_param)).to_hash
|
||||
@struct.new(@block, params)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
Loading…
Reference in a new issue