Class: GuardrailsRuby::Redactors::PIIRedactor

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails_ruby/redactors/pii_redactor.rb

Constant Summary collapse

PATTERNS =

Reuse patterns from Checks::PII

GuardrailsRuby::Checks::PII::PATTERNS
REDACT_MAP =
GuardrailsRuby::Checks::PII::REDACT_MAP

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types: nil) ⇒ PIIRedactor

Initialize with optional type filter.

redactor = PIIRedactor.new(types: [:ssn, :email])
redactor.redact("Call 555-123-4567, SSN 123-45-6789")
# => "Call 555-123-4567, SSN [SSN REDACTED]"


25
26
27
# File 'lib/guardrails_ruby/redactors/pii_redactor.rb', line 25

def initialize(types: nil)
  @types = types&.map(&:to_sym)
end

Class Method Details

.redact(text, types: nil) ⇒ Object

Redact all PII from the given text, returning a new string.

GuardrailsRuby::Redactors::PIIRedactor.redact("My SSN is 123-45-6789")
# => "My SSN is [SSN REDACTED]"


15
16
17
# File 'lib/guardrails_ruby/redactors/pii_redactor.rb', line 15

def self.redact(text, types: nil)
  new(types: types).redact(text)
end

Instance Method Details

#detect(text) ⇒ Object

Detect PII in text without redacting. Returns a hash of { type => [matches] }.



39
40
41
42
43
44
45
46
# File 'lib/guardrails_ruby/redactors/pii_redactor.rb', line 39

def detect(text)
  found = {}
  active_patterns.each do |type, pattern|
    matches = text.scan(pattern)
    found[type] = matches if matches.any?
  end
  found
end

#redact(text) ⇒ Object

Redact PII from text. Returns a new string with PII replaced by placeholders.



30
31
32
33
34
35
36
# File 'lib/guardrails_ruby/redactors/pii_redactor.rb', line 30

def redact(text)
  result = text.dup
  active_patterns.each do |type, pattern|
    result.gsub!(pattern, REDACT_MAP[type])
  end
  result
end