Class: ShortStack::Controller

Inherits:
Object
  • Object
show all
Extended by:
Pancake::Mixins::Publish
Includes:
Pancake::Mixins::Render, Pancake::Mixins::RequestHelper, Pancake::Mixins::ResponseHelper, Pancake::Mixins::StackHelper
Defined in:
lib/short_stack/controller.rb

Constant Summary collapse

DEFAULT_EXCEPTION_HANDLER =
lambda do |error|
  if layout = env['layout']
    layout.content = render :error, :error => error
    layout
  else
    render :error, :error => error
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



64
65
66
67
# File 'lib/short_stack/controller.rb', line 64

def initialize(env)
  @env, @request = env, Rack::Request.new(env)
  @status = 200
end

Instance Attribute Details

#actionObject



76
77
78
79
80
81
# File 'lib/short_stack/controller.rb', line 76

def action
  @action ||=  begin
    rr = request.env['router.response']
    action = rr && rr.dest && rr.dest[:action]
  end
end

#statusObject



62
63
64
# File 'lib/short_stack/controller.rb', line 62

def status
  @status
end

Class Method Details

._template_name_for(name, opts) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/short_stack/controller.rb', line 172

def self._template_name_for(name, opts)
  opts[:format] ||= :html
  [
    "#{name}.#{opts[:format]}",
    "#{name}"
  ]
end

.call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.call(env)
  app = new(env)
  app.dispatch!
end

.handle_exception(&block) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/short_stack/controller.rb', line 147

def self.handle_exception(&block)
  if block_given?
    self._handle_exception = block
  else
    self._handle_exception || DEFAULT_EXCEPTION_HANDLER
  end
end

.rootsObject



168
169
170
# File 'lib/short_stack/controller.rb', line 168

def self.roots
  stack_class.roots
end

Instance Method Details

#_tempate_name_for(name, opts = {}) ⇒ Object



180
181
182
183
# File 'lib/short_stack/controller.rb', line 180

def _tempate_name_for(name, opts = {})
  opts[:format] ||= content_type
  self.class._template_name_for(name, opts)
end

#dispatch!Object

Dispatches to an action based on the params parameter



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/short_stack/controller.rb', line 84

def dispatch!
  if logger
    logger.info "Request: #{request.path}"
    logger.info "Params: #{params.inspect}"
  end


  # Check that the action is available
  raise Pancake::Errors::NotFound, "No Action Found" unless allowed_action?(action)

  @action_opts  = actions[action]

  negotiate_content_type!(@action_opts.formats, params)

  # Set the layout defaults before the action is rendered
  if layout && stack_class.default_layout
    layout.template_name = stack_class.default_layout
  end

  layout.format = params['format'] if layout

  logger.info "Dispatching to #{action.inspect}" if logger

  result = catch(:halt){ self.send(action) }

  case result
  when Array
    result
  when Rack::Response
    result.finish
  when String
    out = if layout
      layout.content = result
      layout
    else
      result
    end
    Rack::Response.new(out, status, headers).finish
  else
    Rack::Response.new((result || ""), status, headers).finish
  end

rescue Pancake::Errors::HttpError => e
  if logger && log_http_error?(e)
    logger.error "Exception: #{e.message}"
    logger.error e.backtrace.join("\n")
  end
  handle_request_exception(e)
rescue Exception => e
  if Pancake.handle_errors?
    server_error = Pancake::Errors::Server.new(e.message)
    server_error.exceptions << e
    server_error.set_backtrace e.backtrace
  else
    server_error = e
  end
  handle_request_exception(server_error)
end

#handle_request_exception(error) ⇒ Object

Raises:

  • (error.class)


155
156
157
158
159
160
# File 'lib/short_stack/controller.rb', line 155

def handle_request_exception(error)
  raise(error.class, error.message, error.backtrace) unless Pancake.handle_errors?
  self.status = error.code
  result = instance_exec error, &self.class.handle_exception
  Rack::Response.new(result, status, headers).finish
end

#layoutObject



57
58
59
# File 'lib/short_stack/controller.rb', line 57

def layout
  env['layout']
end

#log_http_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/short_stack/controller.rb', line 143

def log_http_error?(error)
  true
end

#paramsObject

Provides access to the request params



71
72
73
# File 'lib/short_stack/controller.rb', line 71

def params
  request.params
end