Module: FrederickOperationsLogger

Defined in:
lib/frederick_operations_logger.rb,
lib/frederick_operations_logger/version.rb,
lib/frederick_operations_logger/active_record/helper.rb

Overview

Base gem module

Defined Under Namespace

Modules: ActiveRecord

Constant Summary collapse

REQUEST_STORE_KEY =
'frederick_operations_logger_context'
ALLOWED_ACTIVITIES =
[
  're_accept_tos',                      # when subscribers re-accept TOS from ui dialog
  'update_communication_preferences',   # when consumers update preferences from a ui
].freeze
AUTH_INFO_KEYS =
%i[user_id application_id application_name ip_address].freeze
VERSION =

Current gem version

'1.1.2'

Class Method Summary collapse

Class Method Details

.contextObject

FrederickOperationsLogger.context



70
71
72
# File 'lib/frederick_operations_logger.rb', line 70

def self.context
  RequestStore.store.dig(REQUEST_STORE_KEY, :context) || {}
end

.context=(activity: nil, auth_info: nil, extras: nil, merge: true) ⇒ Object

FrederickOperationsLogger.context = { activity: ‘foo’, auth_info: { user_id: ‘some…uuid’ } } using applications can set local context without concern for empty context in consumed message TODO: auth_info should be set automatically from frederick_api_auth_gem for REST calls auth_info:

user_id: The user id performing the action (if not performed by a system function),
application_id: The Doorkeeper application ID that relates to the access token being used,
application_name: The Doorkeeper application name or internal application name performing the action
ip_address: The IP address of the authorized entity performing the action (user or application)

Use merge: false to replace an already set context instead of merging new info



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/frederick_operations_logger.rb', line 26

def self.context=(activity: nil, auth_info: nil, extras: nil, merge: true)
  auth_info = auth_info.compact.symbolize_keys if auth_info.present?
  extras = extras.compact.deep_symbolize_keys if extras.present?

  return if activity.nil? && auth_info.blank? && extras.blank? # safeguard against setting empty context
  validate_context(activity, auth_info)

  RequestStore.store[REQUEST_STORE_KEY] ||= {}
  RequestStore.store[REQUEST_STORE_KEY][:context] = if merge
                                                      merge_context(
                                                        activity: activity,
                                                        auth_info: auth_info,
                                                        extras: extras
                                                      )
                                                    else
                                                      {
                                                        activity: activity,
                                                        auth_info: auth_info,
                                                        extras: extras
                                                      }.compact
                                                    end
end

.merge_context(activity: nil, auth_info: nil, extras: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/frederick_operations_logger.rb', line 49

def self.merge_context(activity: nil, auth_info: nil, extras: nil)
  previous = self.context
  new_auth_info = if auth_info.present?
                    previous[:auth_info].present? ? previous[:auth_info].merge(auth_info) : auth_info
                  else
                    previous[:auth_info]
                  end
  new_extras = if extras.present?
                 previous[:extras].present? ? previous[:extras].merge(extras) : extras
               else
                 previous[:extras]
               end

  {
    activity: activity || previous[:activity],
    auth_info: new_auth_info,
    extras: new_extras
  }.compact
end

.validate_context(activity, auth_info) ⇒ Object



74
75
76
77
78
79
# File 'lib/frederick_operations_logger.rb', line 74

def self.validate_context(activity, auth_info)
  raise "Invalid activity: #{activity}" unless activity.nil? || ALLOWED_ACTIVITIES.include?(activity)
  return unless auth_info.present? && (auth_info.keys - AUTH_INFO_KEYS).any?

  raise "Invalid auth_info: only #{AUTH_INFO_KEYS.join(', ')} are allowed"
end