Class: BeltController::Base

Inherits:
Object
  • Object
show all
Includes:
Belt::Helpers::Response, Belt::Rendering
Defined in:
lib/belt_controller/base.rb

Direct Known Subclasses

Belt::WelcomeController

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Belt::Rendering

included

Methods included from Belt::Helpers::Response

#cors_headers, #error_response, #handle_error_and_respond, #html_response, #success_response

Constructor Details

#initialize(event:, body:) ⇒ Base

Returns a new instance of Base.



95
96
97
98
99
# File 'lib/belt_controller/base.rb', line 95

def initialize(event:, body:)
  @event = event
  @raw_body = deep_transform_keys_to_snake_case(body || {})
  @params = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

Class Method Details

.after_action(method_name, only: nil, except: nil) ⇒ Object



39
40
41
# File 'lib/belt_controller/base.rb', line 39

def after_action(method_name, only: nil, except: nil)
  after_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end

.after_actionsObject



35
36
37
# File 'lib/belt_controller/base.rb', line 35

def after_actions
  @after_actions ||= []
end

.all_after_actionsObject



59
60
61
62
63
64
65
# File 'lib/belt_controller/base.rb', line 59

def all_after_actions
  if superclass.respond_to?(:all_after_actions)
    superclass.all_after_actions + after_actions
  else
    after_actions
  end
end

.all_before_actionsObject



51
52
53
54
55
56
57
# File 'lib/belt_controller/base.rb', line 51

def all_before_actions
  if superclass.respond_to?(:all_before_actions)
    superclass.all_before_actions + before_actions
  else
    before_actions
  end
end

.all_rescue_handlersObject



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

def all_rescue_handlers
  if superclass.respond_to?(:all_rescue_handlers)
    superclass.all_rescue_handlers.merge(rescue_handlers)
  else
    rescue_handlers
  end
end

.all_skipped_before_actionsObject



67
68
69
70
71
72
73
# File 'lib/belt_controller/base.rb', line 67

def all_skipped_before_actions
  if superclass.respond_to?(:all_skipped_before_actions)
    superclass.all_skipped_before_actions + skipped_before_actions
  else
    skipped_before_actions
  end
end

.before_action(method_name, only: nil, except: nil) ⇒ Object



31
32
33
# File 'lib/belt_controller/base.rb', line 31

def before_action(method_name, only: nil, except: nil)
  before_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end

.before_actionsObject



27
28
29
# File 'lib/belt_controller/base.rb', line 27

def before_actions
  @before_actions ||= []
end

.rescue_from(*exceptions, with:) ⇒ Object



23
24
25
# File 'lib/belt_controller/base.rb', line 23

def rescue_from(*exceptions, with:)
  exceptions.each { |e| rescue_handlers[e] = with }
end

.rescue_handlersObject



19
20
21
# File 'lib/belt_controller/base.rb', line 19

def rescue_handlers
  @rescue_handlers ||= {}
end

.skip_before_action(method_name, only: nil, except: nil) ⇒ Object



47
48
49
# File 'lib/belt_controller/base.rb', line 47

def skip_before_action(method_name, only: nil, except: nil)
  skipped_before_actions << { method: method_name, only: only&.map(&:to_sym), except: except&.map(&:to_sym) }
end

.skipped_before_actionsObject



43
44
45
# File 'lib/belt_controller/base.rb', line 43

def skipped_before_actions
  @skipped_before_actions ||= []
end

Instance Method Details

#action_nameObject



148
149
150
# File 'lib/belt_controller/base.rb', line 148

def action_name
  @current_action
end

#admin?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/belt_controller/base.rb', line 140

def admin?
  user_groups.include?('Admin')
end

#authenticate!Object



125
126
127
128
129
130
# File 'lib/belt_controller/base.rb', line 125

def authenticate!
  user_id = event.dig('requestContext', 'authorizer', 'claims', 'sub')
  raise Belt::AuthenticationError, 'Authentication required' unless user_id

  @current_user_id = user_id
end

#current_user_idObject



132
133
134
# File 'lib/belt_controller/base.rb', line 132

def current_user_id
  @current_user_id ||= event.dig('requestContext', 'authorizer', 'claims', 'sub')
end

#dispatch(action_name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/belt_controller/base.rb', line 109

def dispatch(action_name)
  action_sym = action_name.to_sym
  @current_action = action_sym

  unless respond_to?(action_sym)
    raise Belt::ActionNotFound, "The action '#{action_sym}' could not be found for #{self.class.name}"
  end

  run_before_actions(action_sym)
  result = send(action_sym)
  run_after_actions(action_sym)
  result
rescue StandardError => e
  handle_exception(e)
end

#employee?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/belt_controller/base.rb', line 144

def employee?
  user_groups.include?('Employee')
end

#paramsObject



101
102
103
# File 'lib/belt_controller/base.rb', line 101

def params
  @params ||= build_params
end

#user_groupsObject



136
137
138
# File 'lib/belt_controller/base.rb', line 136

def user_groups
  @user_groups ||= extract_user_groups
end