Class: PiiScrubber::Detectors::UkNino

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

Constant Summary collapse

UK_NINO_REGEX =

Matches UK National Insurance numbers (e.g., QQ123456A, QQ 12 34 56 A, QQ-12-34-56-A)

/\b[A-Za-z]{2}[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[0-9]{2}[ -]?[A-Da-d]\b/
DISALLOWED_PREFIXES =
%w[GB BG NK KN TN NT ZZ].freeze
DISALLOWED_CHARS =
%w[D F I U V].freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Constructor Details

#initializeUkNino

Returns a new instance of UkNino.



14
15
16
# File 'lib/pii_scrubber/detectors/uk_nino.rb', line 14

def initialize
  super(name: :uk_nino)
end

Instance Method Details

#patternsObject



18
19
20
# File 'lib/pii_scrubber/detectors/uk_nino.rb', line 18

def patterns
  [UK_NINO_REGEX]
end

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/pii_scrubber/detectors/uk_nino.rb', line 37

def replace(match, strategy: :placeholder, mask_char: "*", hmac_salt: nil)
  return super unless strategy == :mask

  sanitized = match.gsub(/[\s-]/, "").upcase
  return super if sanitized.length != 9

  prefix = sanitized[0..1]
  suffix = sanitized[-1]
  "#{prefix}#{mask_char * 6}#{suffix}"
end

#valid?(match) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pii_scrubber/detectors/uk_nino.rb', line 22

def valid?(match)
  sanitized = match.gsub(/[\s-]/, "").upcase
  return false unless sanitized.length == 9

  prefix = sanitized[0..1]
  return false if DISALLOWED_PREFIXES.include?(prefix)

  # Characters D, F, I, U, V are disallowed in first or second position
  # Character O is disallowed in second position
  return false if DISALLOWED_CHARS.include?(sanitized[0])
  return false if (DISALLOWED_CHARS + ["O"]).include?(sanitized[1])

  true
end