Class: BeltController::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



93
94
95
96
97
# File 'lib/belt_controller/base.rb', line 93

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.



14
15
16
# File 'lib/belt_controller/base.rb', line 14

def body
  @body
end

#eventObject (readonly)

Returns the value of attribute event.



14
15
16
# File 'lib/belt_controller/base.rb', line 14

def event
  @event
end

Class Method Details

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



37
38
39
# File 'lib/belt_controller/base.rb', line 37

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



33
34
35
# File 'lib/belt_controller/base.rb', line 33

def after_actions
  @after_actions ||= []
end

.all_after_actionsObject



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

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



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

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



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

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



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

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



29
30
31
# File 'lib/belt_controller/base.rb', line 29

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



25
26
27
# File 'lib/belt_controller/base.rb', line 25

def before_actions
  @before_actions ||= []
end

.rescue_from(*exceptions, with:) ⇒ Object



21
22
23
# File 'lib/belt_controller/base.rb', line 21

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

.rescue_handlersObject



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

def rescue_handlers
  @rescue_handlers ||= {}
end

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



45
46
47
# File 'lib/belt_controller/base.rb', line 45

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



41
42
43
# File 'lib/belt_controller/base.rb', line 41

def skipped_before_actions
  @skipped_before_actions ||= []
end

Instance Method Details

#action_nameObject



146
147
148
# File 'lib/belt_controller/base.rb', line 146

def action_name
  @current_action
end

#admin?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/belt_controller/base.rb', line 138

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

#authenticate!Object



123
124
125
126
127
128
# File 'lib/belt_controller/base.rb', line 123

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



130
131
132
# File 'lib/belt_controller/base.rb', line 130

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

#dispatch(action_name) ⇒ Object



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

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)


142
143
144
# File 'lib/belt_controller/base.rb', line 142

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

#paramsObject



99
100
101
# File 'lib/belt_controller/base.rb', line 99

def params
  @params ||= build_params
end

#user_groupsObject



134
135
136
# File 'lib/belt_controller/base.rb', line 134

def user_groups
  @user_groups ||= extract_user_groups
end