Class: Sidewalk::Controller
- Inherits:
-
Object
- Object
- Sidewalk::Controller
- Defined in:
- lib/sidewalk/controller.rb
Overview
Base class for page controllers.
UriMapper maps URIs to classes or Procs. If it maps to a class, that class needs to implement #initialize and #call in the same way as this class. This class provides some added convenience.
You might want to look at ControllerMixins for some additional optional features.
To handle an URL, you will usually want to:
-
subclass this
-
implement #response
-
add your class to your application URI map.
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
The response headers.
-
#logger ⇒ Object
readonly
An object implementing an interface that is compatible with
Logger
. -
#request ⇒ Object
readonly
The instance of Request corresponding to the current HTTP request.
-
#status ⇒ Object
The numeric HTTP status to return.
Class Method Summary collapse
-
.current ⇒ Controller
The current Controller.
Instance Method Summary collapse
-
#call ⇒ Object
Actually respond to the request.
-
#content_type ⇒ Object
What mime-type to return to the user agent.
- #content_type=(value) ⇒ Object
-
#initialize(request, logger) ⇒ Controller
constructor
Initialize a new controller instance.
-
#response ⇒ String
The body of the HTTP response to set.
-
#set_cookie(key, value, options = {}) ⇒ Object
Set a cookie :).
Constructor Details
#initialize(request, logger) ⇒ Controller
Initialize a new controller instance.
28 29 30 31 32 33 34 |
# File 'lib/sidewalk/controller.rb', line 28 def initialize request, logger @request, @logger = request, logger @status = 200 @headers = { 'Content-Type' => 'text/html' } end |
Instance Attribute Details
#headers ⇒ Object (readonly)
The response headers.
For request headers, see #request and Request#headers.
39 40 41 |
# File 'lib/sidewalk/controller.rb', line 39 def headers @headers end |
#logger ⇒ Object (readonly)
An object implementing an interface that is compatible with Logger
45 46 47 |
# File 'lib/sidewalk/controller.rb', line 45 def logger @logger end |
#request ⇒ Object (readonly)
The instance of Request corresponding to the current HTTP request.
42 43 44 |
# File 'lib/sidewalk/controller.rb', line 42 def request @request end |
Class Method Details
.current ⇒ Controller
The current Controller.
117 118 119 120 121 122 123 |
# File 'lib/sidewalk/controller.rb', line 117 def self.current begin callcc{|cc| throw(:sidewalk_controller_current, cc)} rescue NameError, ArgumentError # 1.8 and 1.9 are different nil end end |
Instance Method Details
#call ⇒ Object
Actually respond to the request.
This calls #response, then ties it together with #status and #content_type.
91 92 93 94 95 96 97 |
# File 'lib/sidewalk/controller.rb', line 91 def call cc = catch(:sidewalk_controller_current) do body = self.response return [status, {'Content-Type' => content_type}, [body]] end cc.call(self) if cc end |
#content_type ⇒ Object
What mime-type to return to the user agent.
“text/html” is the default.
77 78 79 |
# File 'lib/sidewalk/controller.rb', line 77 def content_type headers['Content-Type'] end |
#content_type=(value) ⇒ Object
81 82 83 |
# File 'lib/sidewalk/controller.rb', line 81 def content_type= value headers['Content-Type'] = value end |
#response ⇒ String
The body of the HTTP response to set.
In most cases, this is what you’ll want to implement in a subclass. You can call #status=, but you probably don’t want to - just raise an appropriate HttpError subclass instead.
You might be interested in Sidewalk::ControllerMixins::ViewTemplates.
108 109 110 |
# File 'lib/sidewalk/controller.rb', line 108 def response raise NotImplementedError.new end |
#set_cookie(key, value, options = {}) ⇒ Object
Set a cookie :)
Valid options are:
:expires
-
accepts a
Time
. Default is a session cookie. :secure
-
if
true
, only send the cookie via https :httponly
-
do not allow Flash, JavaScript etc to access the cookie
:domain
-
restrict the cookie to only be available on a given domain, and subdomains. Default is the request domain.
:path
-
make the cookie accessible to other paths - default is the request path.
68 69 70 71 72 |
# File 'lib/sidewalk/controller.rb', line 68 def key, value, = {} rack_value = .dup rack_value[:value] = value.to_s Rack::Utils. self.headers, key.to_s, rack_value end |