Custom 404
[sinatra.git] / lib / sinatra.rb
bloba52eedcd2f009b9f703b442b46012cdfee9d71e4
1 require 'rubygems'
3 if ENV['SWIFT']
4  require 'swiftcore/swiftiplied_mongrel'
5  puts "Using Swiftiplied Mongrel"
6 elsif ENV['EVENT']
7   require 'swiftcore/evented_mongrel' 
8   puts "Using Evented Mongrel"
9 end
11 require 'rack'
12 require 'ostruct'
14 class Class
15   def dslify_writter(*syms)
16     syms.each do |sym|
17       class_eval <<-end_eval
18         def #{sym}(v=nil)
19           self.send "#{sym}=", v if v
20           v
21         end
22       end_eval
23     end
24   end
25 end
27 module Sinatra
28   extend self
30   Result = Struct.new(:block, :params, :status) unless defined?(Result)
31   
32   def application
33     @app ||= Application.new
34   end
35   
36   def application=(app)
37     @app = app
38   end
39   
40   def port
41     application.options.port
42   end
43   
44   def env
45     application.options.env
46   end
47   
48   def run
49     
50     begin
51       puts "== Sinatra has taken the stage on port #{port} for #{env}"
52       require 'pp'
53       Rack::Handler::Mongrel.run(application, :Port => port) do |server|
54         trap(:INT) do
55           server.stop
56           puts "\n== Sinatra has ended his set (crowd applauds)"
57         end
58       end
59     rescue Errno::EADDRINUSE => e
60       puts "== Someone is already performing on port #{port}!"
61     end
62     
63   end
64       
65   class Event
67     URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
68     PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
69     
70     attr_reader :path, :block, :param_keys, :pattern
71     
72     def initialize(path, &b)
73       @path = path
74       @block = b
75       @param_keys = []
76       regex = @path.to_s.gsub(PARAM) do
77         @param_keys << $1.intern
78         "(#{URI_CHAR}+)"
79       end
80       @pattern = /^#{regex}$/
81     end
82         
83     def invoke(env)
84       return unless pattern =~ env['PATH_INFO'].squeeze('/')
85       params = param_keys.zip($~.captures.map(&:from_param)).to_hash
86       Result.new(block, params, 200)
87     end
88     
89   end
90   
91   class Error
92     
93     attr_reader :code, :block
94     
95     def initialize(code, &b)
96       @code, @block = code, b
97     end
98     
99     def invoke(env)
100       Result.new(block, {}, 404)
101     end
102     
103   end
104   
105   module ResponseHelpers
107     def redirect(path)
108       throw :halt, Redirect.new(path)
109     end
111   end
112   
113   module RenderingHelpers
114     
115     def render(content, options={})
116       template = resolve_template(content, options)
117       @content = _evaluate_render(template)
118       layout = resolve_layout(options[:layout], options)
119       @content = _evaluate_render(layout) if layout
120       @content
121     end
122     
123     private
124       
125       def _evaluate_render(content, options={})
126         case content
127         when String
128           instance_eval(%Q{"#{content}"})
129         when Proc
130           instance_eval(&content)
131         when File
132           instance_eval(%Q{"#{content.read}"})
133         end
134       end
135       
136       def resolve_template(content, options={})
137         case content
138         when String
139           content
140         when Symbol
141           File.new(filename_for(content, options))
142         end
143       end
144     
145       def resolve_layout(name, options={})
146         return if name == false
147         if layout = layouts[name || :layout]
148           return layout
149         end
150         if File.file?(filename = filename_for(name, options))
151           File.new(filename)
152         end
153       end
154       
155       def filename_for(name, options={})
156         (options[:views_directory] || 'views') + "/#{name}.#{ext}"
157       end
158               
159       def ext
160         :html
161       end
163       def layouts
164         Sinatra.application.layouts
165       end
166     
167   end
169   class EventContext
170     
171     include ResponseHelpers
172     include RenderingHelpers
173     
174     attr_accessor :request, :response
175     
176     dslify_writter :status, :body
177     
178     def initialize(request, response, route_params)
179       @request = request
180       @response = response
181       @route_params = route_params
182       @response.body = nil
183     end
184     
185     def params
186       @params ||= @route_params.merge(@request.params).symbolize_keys
187     end
188     
189     def stop(content)
190       throw :halt, content
191     end
192     
193     def complete(returned)
194       @response.body || returned
195     end
196     
197     private
199       def method_missing(name, *args, &b)
200         @response.send(name, *args, &b)
201       end
202     
203   end
204   
205   class Redirect
206     def initialize(path)
207       @path = path
208     end
209     
210     def to_result(cx, *args)
211       cx.status(302)
212       cx.header.merge!('Location' => @path)
213       cx.body = ''
214     end
215   end
216     
217   class Application
218     
219     attr_reader :events, :layouts, :default_options
220     
221     def self.default_options
222       @@default_options ||= {
223         :run => true,
224         :port => 4567,
225         :env => :development
226       }
227     end
228     
229     def default_options
230       self.class.default_options
231     end
232         
233     def initialize
234       @events = Hash.new { |hash, key| hash[key] = [] }
235       @layouts = Hash.new
236     end
237     
238     def define_event(method, path, &b)
239       events[method] << event = Event.new(path, &b)
240       event
241     end
242     
243     def define_layout(name=:layout, &b)
244       layouts[name] = b
245     end
246     
247     def define_error(code, &b)
248       events[:errors][code] = Error.new(code, &b)
249     end
250     
251     def lookup(env)
252       e = events[env['REQUEST_METHOD'].downcase.to_sym].eject(&[:invoke, env])
253       e ||= (events[:errors][404] || basic_not_found).invoke(env)
254     end
255     
256     def basic_not_found
257       Error.new(404) do
258         '<h1>Not Found</h1>'
259       end
260     end
262     def options
263       @options ||= OpenStruct.new(default_options)
264     end
265     
266     def call(env)
267       result = lookup(env)
268       context = EventContext.new(
269         Rack::Request.new(env), 
270         Rack::Response.new,
271         result.params
272       )
273       context.status(result.status)
274       returned = catch(:halt) do
275         [:complete, context.instance_eval(&result.block)]
276       end
277       result = returned.to_result(context)
278       context.body = String === result ? [*result] : result
279       context.finish
280     end
281         
282   end
283   
286 def get(path, &b)
287   Sinatra.application.define_event(:get, path, &b)
290 def post(path, &b)
291   Sinatra.application.define_event(:post, path, &b)
294 def put(path, &b)
295   Sinatra.application.define_event(:put, path, &b)
298 def delete(path, &b)
299   Sinatra.application.define_event(:delete, path, &b)
302 def helpers(&b)
303   Sinatra::EventContext.class_eval(&b)
306 def error(code, &b)
307   Sinatra.application.define_error(code, &b)
310 def layout(name = :layout, &b)
311   Sinatra.application.define_layout(name, &b)
314 ### Misc Core Extensions
316 module Kernel
318   def silence_warnings
319     old_verbose, $VERBOSE = $VERBOSE, nil
320     yield
321   ensure
322     $VERBOSE = old_verbose
323   end
327 class String
329   # Converts +self+ to an escaped URI parameter value
330   #   'Foo Bar'.to_param # => 'Foo%20Bar'
331   def to_param
332     URI.escape(self)
333   end
334   
335   # Converts +self+ from an escaped URI parameter value
336   #   'Foo%20Bar'.from_param # => 'Foo Bar'
337   def from_param
338     URI.unescape(self)
339   end
340   
343 class Hash
344   
345   def to_params
346     map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
347   end
348   
349   def symbolize_keys
350     self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
351   end
352   
353   def pass(*keys)
354     reject { |k,v| !keys.include?(k) }
355   end
356   
359 class Symbol
360   
361   def to_proc 
362     Proc.new { |*args| args.shift.__send__(self, *args) }
363   end
364   
367 class Array
368   
369   def to_hash
370     self.inject({}) { |h, (k, v)|  h[k] = v; h }
371   end
372   
373   def to_proc
374     Proc.new { |*args| args.shift.__send__(self[0], *(args + self[1..-1])) }
375   end
376   
379 module Enumerable
380   
381   def eject(&block)
382     find { |e| result = block[e] and break result }
383   end
384   
387 ### Core Extension results for throw :halt
389 class Proc
390   def to_result(cx, *args)
391     cx.instance_eval(&self)
392   end
395 class String
396   def to_result(cx, *args)
397     cx.body = self
398   end
401 class Array
402   def to_result(cx, *args)
403     self.shift.to_result(cx, *self)
404   end
407 class Symbol
408   def to_result(cx, *args)
409     cx.send(self, *args)
410   end
413 class Fixnum
414   def to_result(cx, *args)
415     cx.status self
416     cx.body args.first
417   end
420 class NilClass
421   def to_result(cx, *args)
422     cx.body = ''
423     # log warning here
424   end
427 at_exit do
428   Sinatra.run if Sinatra.application.options.run