Class: Lyra::Privacy::PIIDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/lyra/privacy/pii_detector.rb

Overview

Detects personally identifiable information (PII) in data

This class delegates to PamDsl::PIIDetector for the core PII detection logic, providing a unified implementation across both Lyra and PAM DSL.

Examples:

Detect PII in attributes

PIIDetector.detect({ email: "[email protected]", name: "John" })
# => { email: { type: :email, value: "[email protected]", sensitive: false }, ... }

Check if a field contains PII

PIIDetector.contains_pii?(:email)  # => true
PIIDetector.contains_pii?(:count)  # => false

Class Method Summary collapse

Class Method Details

.contains_pii?(field_name) ⇒ Boolean

Check if a field contains PII

Parameters:

  • field_name (String, Symbol) —

    Field name to check

Returns:

  • (Boolean) —

    true if field contains PII



32
33
34
# File 'lib/lyra/privacy/pii_detector.rb', line 32

def contains_pii?(field_name)
  PamDsl::PIIDetector.contains_pii?(field_name)
end

.detect(attributes) ⇒ Hash

Detect PII in attributes

Parameters:

  • attributes (Hash) —

    Key-value pairs to check for PII

Returns:

  • (Hash) —

    Keys that contain PII, with their type, value, and sensitivity



23
24
25
# File 'lib/lyra/privacy/pii_detector.rb', line 23

def detect(attributes)
  PamDsl::PIIDetector.detect(attributes)
end

.extract_from_event_stream(events) ⇒ Hash

Extract all PII from event stream

This is a convenience wrapper around PamDsl::PIIDetector.extract_pii_from_records that provides Lyra-specific extractors for Lyra::Event objects.

Examples:

events = Lyra.config.event_store.read.stream("User$123").to_a
pii = PIIDetector.extract_from_event_stream(events)
# => { email: [{ field: :email, value: "...", event_id: "...", ... }], ... }

Parameters:

  • events (Array<Lyra::Event>) —

    Events to scan for PII

Returns:

  • (Hash) —

    PII inventory grouped by type



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lyra/privacy/pii_detector.rb', line 59

def extract_from_event_stream(events)
  PamDsl::PIIDetector.extract_pii_from_records(
    events,
    attribute_extractor: ->(e) { e.attributes },
    metadata_extractor: ->(e) {
      {
        event_id: e.event_id,
        timestamp: e.timestamp,
        model_class: e.model_class,
        model_id: e.model_id
      }
    }
  )
end

.mask(value, pii_type) ⇒ String

Mask PII for display

Parameters:

  • value (Object) —

    The value to mask

  • pii_type (Symbol) —

    The type of PII

Returns:

  • (String) —

    Masked value



42
43
44
# File 'lib/lyra/privacy/pii_detector.rb', line 42

def mask(value, pii_type)
  PamDsl::PIIDetector.mask(value, pii_type)
end

.sensitive?(pii_type) ⇒ Boolean

Check if a PII type is considered sensitive

Parameters:

  • pii_type (Symbol) —

    The PII type

Returns:

  • (Boolean)


79
80
81
# File 'lib/lyra/privacy/pii_detector.rb', line 79

def sensitive?(pii_type)
  PamDsl::PIIDetector.sensitive?(pii_type)
end