Class: Jets::Controller::Base
- Inherits:
-
Lambda::Functions
- Object
- Lambda::Functions
- Jets::Controller::Base
- Includes:
- ActiveSupport::Rescuable, Callbacks, Cookies, ForgeryProtection, Layout, Params, Rendering, Router::Helpers
- Defined in:
- lib/jets/controller/base.rb
Direct Known Subclasses
ApplicationController, MailersController, PublicController, RackController
Constant Summary
Constants included from Router::Helpers::CoreHelper
Router::Helpers::CoreHelper::CONTROLLER_DELEGATES
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#session ⇒ Object
Returns the value of attribute session.
Attributes inherited from Lambda::Functions
Class Method Summary collapse
- .api_key_required(value = nil) ⇒ Object
- .authorization_type(value = nil) ⇒ Object
- .controller_path ⇒ Object
- .helper_method(*meths) ⇒ Object
- .internal(value = nil) ⇒ Object
- .process(event, context = {}, meth) ⇒ Object
Instance Method Summary collapse
- #action_name ⇒ Object
- #controller_paths ⇒ Object
- #dispatch! ⇒ Object
-
#initialize(event, context = {}, meth) ⇒ Base
constructor
A new instance of Base.
-
#json_dump(data) ⇒ Object
Handles binary data safely.
- #log_info_start ⇒ Object
-
#process! ⇒ Object
One key difference between process! vs dispatch!.
Methods included from ForgeryProtection
Methods included from Router::Helpers::NamedRoutesHelper
Methods included from Router::Helpers::CoreHelper
Methods included from Rendering
#actual_host, #add_stage_name, #adjust_content_type!, #default_layout, #ensure_render, #managed_options, #normalize_options, #render, #url_for
Methods included from Redirection
#ensure_protocol, #redirect_back, #redirect_to
Methods included from Params
#body_params, #params, #query_parameters
Methods included from Cookies
Methods inherited from Lambda::Functions
inherited, output_keys, subclasses
Methods included from Lambda::Dsl
Constructor Details
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
18 19 20 |
# File 'lib/jets/controller/base.rb', line 18 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
18 19 20 |
# File 'lib/jets/controller/base.rb', line 18 def response @response end |
#session ⇒ Object
Returns the value of attribute session.
19 20 21 |
# File 'lib/jets/controller/base.rb', line 19 def session @session end |
Class Method Details
.api_key_required(value = nil) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/jets/controller/base.rb', line 136 def self.api_key_required(value=nil) if !value.nil? self.api_key_needed = value else self.api_key_needed end end |
.authorization_type(value = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/jets/controller/base.rb', line 127 def self.(value=nil) if !value.nil? self.auth_type = value else self.auth_type end end |
.controller_path ⇒ Object
104 105 106 |
# File 'lib/jets/controller/base.rb', line 104 def self.controller_path name.sub(/Controller$/, "".freeze).underscore end |
.helper_method(*meths) ⇒ Object
144 145 146 147 148 |
# File 'lib/jets/controller/base.rb', line 144 def self.helper_method(*meths) meths.each do |meth| Jets::Router::Helpers.define_helper_method(meth) end end |
.internal(value = nil) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/jets/controller/base.rb', line 118 def self.internal(value=nil) if !value.nil? self.internal_controller = value else self.internal_controller end end |
.process(event, context = {}, meth) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/jets/controller/base.rb', line 108 def self.process(event, context={}, meth) controller = new(event, context, meth) # Using send because process! is private method in Jets::RackController so # it doesnt create a lambda function. It's doesnt matter what scope process! # is in Controller::Base because Jets lambda functions inheritance doesnt # include methods in Controller::Base. controller.send(:process!) end |
Instance Method Details
#action_name ⇒ Object
100 101 102 |
# File 'lib/jets/controller/base.rb', line 100 def action_name @meth end |
#controller_paths ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/jets/controller/base.rb', line 90 def controller_paths paths = [] klass = self.class while klass != Jets::Controller::Base paths << klass.controller_path klass = klass.superclass end paths end |
#dispatch! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jets/controller/base.rb', line 46 def dispatch! t1 = Time.now log_info_start begin if run_before_actions(break_if: -> { @rendered }) send(@meth) action_completed = true else Jets.logger.info "Filter chain halted as #{@last_callback_name} rendered or redirected" end triplet = ensure_render run_after_actions if action_completed rescue Exception => exception rescue_with_handler(exception) || raise triplet = ensure_render end took = Time.now - t1 status = triplet[0] Jets.logger.info "Completed Status Code #{status} in #{took}s" triplet # status, headers, body end |
#json_dump(data) ⇒ Object
Handles binary data safely
83 84 85 86 87 88 |
# File 'lib/jets/controller/base.rb', line 83 def json_dump(data) JSON.dump(data) rescue Encoding::UndefinedConversionError data['body'] = '[BINARY]' JSON.dump(data) end |
#log_info_start ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jets/controller/base.rb', line 71 def log_info_start display_event = @event.dup display_event['body'] = '[BASE64_ENCODED]' if @event['isBase64Encoded'] # JSON.dump makes logging look pretty in CloudWatch logs because it keeps it on 1 line ip = request.ip Jets.logger.info "Started #{@event['httpMethod']} \"#{@event['path']}\" for #{ip} at #{Time.now}" Jets.logger.info "Processing #{self.class.name}##{@meth}" Jets.logger.info " Event: #{json_dump(display_event)}" Jets.logger.info " Parameters: #{JSON.dump(params(raw: true).to_h)}" end |
#process! ⇒ Object
One key difference between process! vs dispatch!
process! - takes the request through the middleware stack
dispatch! - does not
Most of the time, you want process! instead of dispatch!
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jets/controller/base.rb', line 33 def process! adapter = Jets::Controller::Rack::Adapter.new(event, context, meth) adapter.rack_vars( 'jets.controller' => self, 'lambda.context' => context, 'lambda.event' => event, 'lambda.meth' => meth, ) # adapter.process ultimately calls app controller action at the very last # middleware stack. adapter.process # Returns API Gateway hash structure end |