Class: PiiScrubber::Detectors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pii_scrubber/detectors/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/pii_scrubber/detectors/base.rb', line 10

def initialize(name:)
  @name = name.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/pii_scrubber/detectors/base.rb', line 8

def name
  @name
end

Instance Method Details

#patternsObject

Returns array of Regexp objects for matching



15
16
17
# File 'lib/pii_scrubber/detectors/base.rb', line 15

def patterns
  []
end

#replace(match, strategy: :placeholder, mask_char: "*", hmac_salt: nil) ⇒ Object

Generates replacement text for the match based on strategy



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pii_scrubber/detectors/base.rb', line 25

def replace(match, strategy: :placeholder, mask_char: "*", hmac_salt: nil)
  if strategy.is_a?(Proc)
    return strategy.call(match, name)
  end

  case strategy
  when :placeholder
    "[REDACTED:#{name.to_s.upcase}]"
  when :mask
    mask_value(match, mask_char: mask_char)
  when :hash
    digest = hmac_salt ? Digest::SHA256.hexdigest("#{hmac_salt}:#{match}")[0..7] : Digest::SHA256.hexdigest(match)[0..7]
    "[ANON:#{digest}]"
  else
    "[REDACTED:#{name.to_s.upcase}]"
  end
end

#valid?(_match) ⇒ Boolean

Validates if the regex match is genuinely a PII instance (prevents false positives)

Returns:

  • (Boolean)


20
21
22
# File 'lib/pii_scrubber/detectors/base.rb', line 20

def valid?(_match)
  true
end