4 require 'sinatra/mime_types'
7 old_verbose, $VERBOSE = $VERBOSE, nil
10 $VERBOSE = old_verbose
25 map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
29 self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
33 reject { |k,v| !keys.include?(k) }
39 Proc.new { |*args| args.shift.__send__(self, *args) }
45 self.inject({}) { |h, (k, v)| h[k] = v; h }
49 Proc.new { |*args| args.shift.__send__(self[0], args + self[1..-1]) }
61 find { |e| result = block[e] and break result }
68 EventContext = Struct.new(:request, :response) do
69 def method_missing(name, *args)
70 if args.size == 1 && response.respond_to?("#{name}=")
71 response.send("#{name}=", args.first)
73 response.send(name, *args)
78 def setup_default_events!
80 "#{$!.message}\n\t#{$!.backtrace.join("\n\t")}"
89 @request_types ||= [:get, :put, :post, :delete]
93 @routes ||= Hash.new do |hash, key|
94 hash[key] = [] if request_types.include?(key)
99 @config ||= @default_config.dup
107 @default_config ||= {
109 :raise_errors => false,
110 :env => :development,
111 :root => File.dirname($0),
112 :default_static_mime_type => 'text/plain'
116 def determine_route(verb, path)
117 routes[verb].eject { |r| r.match(path) } || routes[404]
120 def content_type_for(path)
121 ext = File.extname(path)[1..-1]
122 Sinatra.mime_types[ext] || config[:default_static_mime_type]
125 def serve_static_file(path)
126 path = Sinatra.config[:root] + '/public' + path
129 'Content-Type' => Array(content_type_for(path)),
130 'Content-Length' => Array(File.size(path))
132 [200, headers, File.read(path)]
137 request = Rack::Request.new(env)
139 if found = serve_static_file(request.path_info)
143 response = Rack::Response.new
144 route = determine_route(
145 request.request_method.downcase.to_sym,
148 context = EventContext.new(request, response)
151 result = context.instance_eval(&route.block)
152 context.status ||= route.default_status
153 context.body = Array(result.to_s)
156 raise e if config[:raise_errors]
157 route = Sinatra.routes[500]
159 context.body Array(context.instance_eval(&route.block))
164 def define_route(verb, path, &b)
165 routes[verb] << route = Route.new(path, &b)
169 def define_error(code, &b)
170 routes[code] = Error.new(code, &b)
175 URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
176 PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
178 Result = Struct.new(:path, :block, :params, :default_status)
180 attr_reader :block, :path
182 def initialize(path, &b)
183 @path, @block = path, b
185 regex = path.to_s.gsub(PARAM) do
186 @param_keys << $1.intern
189 @pattern = /^#{regex}$/
193 return nil unless path =~ @pattern
194 params = @param_keys.zip($~.captures.map(&:from_param)).to_hash
195 Result.new(@path, @block, params, 200)
204 def initialize(code, &b)
205 @code, @block = code, b
217 paths.map { |path| Sinatra.define_route(:get, path, &b) }
220 def error(*codes, &b)
221 raise 'You must specify a block to assciate with an error' if b.nil?
222 codes.each { |code| Sinatra.define_error(code, &b) }
225 def mime_type(content_type, *exts)
226 exts.each { |ext| Sinatra::MIME_TYPES.merge(ext.to_s, content_type) }
229 Sinatra.setup_default_events!