Class: Sidewalk::Controller

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, logger) ⇒ Controller

Initialize a new controller instance.

Parameters:

  • request (Request)

    has information on the HTTP request.

  • logger (Logger)

    is something implement the same interface as Ruby’s Logger class.



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

#headersObject (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

#loggerObject (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

#requestObject (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

#statusObject

The numeric HTTP status to return.

In most cases, you won’t actually want to change this - you might want to raise an instance of a subclass of HttpError or Redirect instead.



52
53
54
# File 'lib/sidewalk/controller.rb', line 52

def status
  @status
end

Class Method Details

.currentController

The current Controller.

Returns:

  • (Controller)

    the current Controller if #call is in the stack.

  • nil otherwise.



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

#callObject

Actually respond to the request.

This calls #response, then ties it together with #status and #content_type.

Returns:

  • a response in Rack’s Array format.



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_typeObject

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

#responseString

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.

Returns:

  • (String)

    the body of the HTTP response

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/sidewalk/controller.rb', line 108

def response
  raise NotImplementedError.new
end

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.

Parameters:

  • key (String)

    is the name of the cookie to set

  • value (Object)

    is the value to set - as long as it responds to #to_s, it’s fine.



68
69
70
71
72
# File 'lib/sidewalk/controller.rb', line 68

def set_cookie key, value, options = {}
  rack_value = options.dup
  rack_value[:value] = value.to_s
  Rack::Utils.set_cookie_header! self.headers, key.to_s, rack_value
end