FIX: Removing sessions
[sinatra.git] / lib / sinatra / context.rb
blob87190eec69f607c640a038fc6c68ea64fa58e94a
1 require File.dirname(__FILE__) + '/context/renderer'
3 module Sinatra
5   class EventContext
6   
7     cattr_accessor :logger
8     attr_reader :request
10     include Sinatra::Renderer
11   
12     def initialize(request) #:nodoc:
13       @request = request
14       @headers = {}
15     end
16   
17     # Sets or returns the status
18     def status(value = nil)
19       @status = value if value
20       @status || 200
21     end
22   
23     # Sets or returns the body
24     # *Usage*
25     #   body 'test'
26     # or
27     #   body do
28     #     'test'
29     #   end
30     # both are the same
31     #
32     def body(value = nil, &block)
33       @body = value if value
34       @body = block.call if block
35       @body
36     end
37     
38     # Renders an exception to +body+ and sets status to 500
39     def error(value = nil)
40       if value
41         status 500
42         @error = value
43         erb :error, :views_directory => SINATRA_ROOT + '/files/'
44       end
45       @error
46     end
47       
48     # Sets or returns response headers
49     #
50     # *Usage*
51     #   header 'Content-Type' => 'text/html'
52     #   header 'Foo' => 'Bar'
53     # or
54     #   headers 'Content-Type' => 'text/html',
55     #           'Foo' => 'Bar'
56     # 
57     # Whatever blows your hair back
58     def headers(value = nil)
59       @headers.merge!(value) if value
60       @headers
61     end
62     alias :header :headers
63   
64     # Returns a Hash of params.  Keys are symbolized
65     def params
66       @params ||= @request.params.symbolize_keys
67     end
68     
69     # Redirect to a url
70     def redirect(path)
71       logger.info "Redirecting to: #{path}"
72       status 302
73       header 'Location' => path
74     end
75   
76     def log_event #:nodoc:
77       logger.info "#{request.request_method} #{request.path_info} | Status: #{status} | Params: #{params.inspect}"
78       logger.exception(error) if error
79     end
80   
81   end
83 end