Class: BeltController::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
#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
#body ⇒ Object
Returns the value of attribute body.
16
17
18
|
# File 'lib/belt_controller/base.rb', line 16
def body
@body
end
|
#event ⇒ Object
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_actions ⇒ Object
35
36
37
|
# File 'lib/belt_controller/base.rb', line 35
def after_actions
@after_actions ||= []
end
|
.all_after_actions ⇒ Object
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_actions ⇒ Object
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_handlers ⇒ Object
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_actions ⇒ Object
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_actions ⇒ Object
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_handlers ⇒ Object
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_actions ⇒ Object
43
44
45
|
# File 'lib/belt_controller/base.rb', line 43
def skipped_before_actions
@skipped_before_actions ||= []
end
|
Instance Method Details
#action_name ⇒ Object
148
149
150
|
# File 'lib/belt_controller/base.rb', line 148
def action_name
@current_action
end
|
#admin? ⇒ 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_id ⇒ Object
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
144
145
146
|
# File 'lib/belt_controller/base.rb', line 144
def employee?
user_groups.include?('Employee')
end
|
#params ⇒ Object
101
102
103
|
# File 'lib/belt_controller/base.rb', line 101
def params
@params ||= build_params
end
|
#user_groups ⇒ Object
136
137
138
|
# File 'lib/belt_controller/base.rb', line 136
def user_groups
@user_groups ||=
end
|