Module: IuguLogger::TenantContext

Defined in:
lib/iugu_logger/tenant_context.rb

Overview

Tenant (iugu domain) context extractor.

Reads tenant identifiers from a Rack env hash and builds a Hash compatible with the canonical ‘iugu.*` block of the schema. Convention is for platform/core to populate the env in middleware (e.g. after authentication) under the `iugu.current_*` namespace.

Default key mapping per IUGU_LOGGING_STANDARD.md §6.3:

env['iugu.current_account_id']      → iugu.account_id
env['iugu.current_subaccount_id']   → iugu.subaccount_id
env['iugu.current_organization_id'] → iugu.organization_id
env['iugu.current_user_id']         → iugu.user_id
env['iugu.current_tier']            → iugu.tier
env['iugu.current_feature']         → iugu.feature

Spec: IUGU_LOGGING_STANDARD.md §6.3

Constant Summary collapse

DEFAULT_RACK_KEYS =
{
  'iugu.current_account_id'      => 'account_id',
  'iugu.current_subaccount_id'   => 'subaccount_id',
  'iugu.current_organization_id' => 'organization_id',
  'iugu.current_user_id'         => 'user_id',
  'iugu.current_tier'            => 'tier',
  'iugu.current_feature'         => 'feature'
}.freeze

Class Method Summary collapse

Class Method Details

.from_rack(rack_env, key_mapping: DEFAULT_RACK_KEYS) ⇒ Hash

Extract tenant context from a Rack env. Returns an empty hash when nothing matched — Logger then emits without ‘iugu` block.

Parameters:

  • rack_env (Hash, nil)

    env hash; nil returns {}

  • key_mapping (Hash) (defaults to: DEFAULT_RACK_KEYS)

    override the default rack-key → iugu-field map

Returns:

  • (Hash)

    string-keyed iugu context (suitable to pass as ‘iugu:` kwarg)



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/iugu_logger/tenant_context.rb', line 39

def from_rack(rack_env, key_mapping: DEFAULT_RACK_KEYS)
  return {} if rack_env.nil?

  result = {}
  key_mapping.each do |rack_key, iugu_key|
    value = rack_env[rack_key]
    next if value.nil?
    next if value.respond_to?(:empty?) && value.empty?

    result[iugu_key] = value
  end
  result
end