Class: Pancake::Stacks::Short::Controller
- Extended by:
- Mixins::Publish
- Defined in:
- lib/pancake/stacks/short/controller.rb
Constant Summary collapse
- DEFAULT_EXCEPTION_HANDLER =
lambda do |error| "#{error.name}: #{error.description}" end
Constants included from Mixins::RequestHelper
Mixins::RequestHelper::VAULT_KEY
Constants included from Mixins::Render
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #_tempate_name_for(name, opts) ⇒ Object
- #content_type ⇒ Object
-
#dispatch! ⇒ Object
Dispatches to an action based on the params parameter.
- #handle_request_exception(error) ⇒ Object
-
#initialize(env) ⇒ Controller
constructor
A new instance of Controller.
-
#params ⇒ Object
Provides access to the request params.
Methods included from Mixins::Publish
as, extended, method_added, provides, publish, validate_and_coerce_params
Methods included from Mixins::StackHelper
Methods included from Mixins::ResponseHelper
Methods included from Mixins::RequestHelper
#base_url, #env, #env=, #logger, #request, #url, #url_for, #vault
Methods included from Mixins::Render
Constructor Details
#initialize(env) ⇒ Controller
Returns a new instance of Controller.
29 30 31 32 |
# File 'lib/pancake/stacks/short/controller.rb', line 29 def initialize(env) @env, @request = env, Rack::Request.new(env) @status = 200 end |
Instance Attribute Details
#status ⇒ Object
27 28 29 |
# File 'lib/pancake/stacks/short/controller.rb', line 27 def status @status end |
Class Method Details
.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.
21 22 23 24 |
# File 'lib/pancake/stacks/short/controller.rb', line 21 def self.call(env) app = new(env) app.dispatch! end |
.handle_exception(&block) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/pancake/stacks/short/controller.rb', line 97 def self.handle_exception(&block) if block_given? self._handle_exception = block else self._handle_exception || DEFAULT_EXCEPTION_HANDLER end end |
.roots ⇒ Object
118 119 120 |
# File 'lib/pancake/stacks/short/controller.rb', line 118 def self.roots stack_class.roots end |
Instance Method Details
#_tempate_name_for(name, opts) ⇒ Object
122 123 124 125 |
# File 'lib/pancake/stacks/short/controller.rb', line 122 def _tempate_name_for(name, opts) opts[:format] ||= content_type "#{name}.#{opts[:format]}" end |
#content_type ⇒ Object
93 94 95 |
# File 'lib/pancake/stacks/short/controller.rb', line 93 def content_type @content_type end |
#dispatch! ⇒ Object
Dispatches to an action based on the params parameter
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pancake/stacks/short/controller.rb', line 41 def dispatch! params["action"] ||= params[:action] params[:format] ||= params["format"] if logger logger.info "Request: #{request.path}" logger.info "Params: #{params.inspect}" end # Check that the action is available raise Errors::NotFound, "No Action Found" unless allowed_action?(params["action"]) @action_opts = actions[params["action"]] if params[:format] @content_type, ct, @mime_type = Pancake::MimeTypes.negotiate_by_extension(params[:format].to_s, @action_opts.formats) else @content_type, ct, @mime_type = Pancake::MimeTypes.negotiate_accept_type(env["HTTP_ACCEPT"], @action_opts.formats) end raise Errors::NotAcceptable unless @content_type logger.info "Dispatching to #{params["action"].inspect}" if logger # set the response header headers["Content-Type"] = ct result = self.send(params['action']) case result when Array result when Rack::Response result.finish else Rack::Response.new(self.send(params["action"]), status, headers).finish end rescue Errors::HttpError => e if logger logger.error "Exception: #{e.}" logger.error e.backtrace.join("\n") end handle_request_exception(e) rescue Exception => e if Pancake.handle_errors? server_error = Errors::Server.new server_error.exceptions << e else server_error = e end handle_request_exception(server_error) end |
#handle_request_exception(error) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/pancake/stacks/short/controller.rb', line 105 def handle_request_exception(error) raise(error.class, error., 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 |
#params ⇒ Object
Provides access to the request params
36 37 38 |
# File 'lib/pancake/stacks/short/controller.rb', line 36 def params request.params end |