Class: Jets::Controller::Base

Inherits:
Lambda::Functions show all
Includes:
ActiveSupport::Rescuable, Callbacks, Cookies, Layout, Params, Rendering
Defined in:
lib/jets/controller/base.rb

Instance Attribute Summary collapse

Attributes inherited from Lambda::Functions

#context, #event, #meth

Class Method Summary collapse

Instance Method Summary collapse

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_to

Methods included from Params

#body_params, #params, #query_parameters

Methods included from Cookies

#cookies

Methods inherited from Lambda::Functions

inherited, subclasses

Methods included from Lambda::Dsl

add_custom_resource_extensions, included, #lambda_functions

Constructor Details

#initialize(event, context = {}, meth) ⇒ Base

Returns a new instance of Base.



18
19
20
21
22
# File 'lib/jets/controller/base.rb', line 18

def initialize(event, context={}, meth)
  super
  @request = Request.new(event, context)
  @response = Response.new
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



16
17
18
# File 'lib/jets/controller/base.rb', line 16

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



16
17
18
# File 'lib/jets/controller/base.rb', line 16

def response
  @response
end

#sessionObject

Returns the value of attribute session.



17
18
19
# File 'lib/jets/controller/base.rb', line 17

def session
  @session
end

Class Method Details

.authorization_type(value = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/jets/controller/base.rb', line 102

def self.authorization_type(value=nil)
  if !value.nil?
    self.auth_type = value
  else
    self.auth_type
  end
end

.internal(value = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/jets/controller/base.rb', line 93

def self.internal(value=nil)
  if !value.nil?
    self.internal_controller = value
  else
    self.internal_controller
  end
end

.process(event, context = {}, meth) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/jets/controller/base.rb', line 83

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

#dispatch!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jets/controller/base.rb', line 37

def dispatch!
  Jets.loader.reload if Jets.env.development?

  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



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

def json_dump(data)
  JSON.dump(data)
rescue Encoding::UndefinedConversionError
  data['body'] = '[BINARY]'
  JSON.dump(data)
end

#log_info_startObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/jets/controller/base.rb', line 64

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jets/controller/base.rb', line 24

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