Class: PiiScrubber::Detectors::IpAddress

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

Constant Summary collapse

IPV4_REGEX =
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
IPV6_REGEX =
/\b(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b/

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Constructor Details

#initializeIpAddress

Returns a new instance of IpAddress.



11
12
13
# File 'lib/pii_scrubber/detectors/ip_address.rb', line 11

def initialize
  super(name: :ip_address)
end

Instance Method Details

#patternsObject



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

def patterns
  [IPV4_REGEX, IPV6_REGEX]
end

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/pii_scrubber/detectors/ip_address.rb', line 24

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

  if match.include?(".") # IPv4
    octets = match.split(".")
    "#{octets[0]}.#{octets[1]}.#{mask_char * 3}.#{mask_char * 3}"
  else
    super
  end
end

#valid?(match) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(match)
  # Optional check: skip localhost (127.0.0.1 or ::1) if needed, otherwise valid IP
  true
end