class Sensitive
Redact sensitive information from output.
Nested
Definitions
REDACT
Default redaction pattern.
Implementation
REDACT = /
phone
| email
| full_?name
| first_?name
| last_?name
| device_name
| user_agent
| zip
| address
| location
| latitude
| longitude
| ip
| gps
| sex
| gender
| token
| password
/xi
def initialize(output, redact: REDACT, **options)
Create a new sensitive output wrapper.
Signature
-
parameter
outputConsole::Output The output to wrap.
-
parameter
redactRegexp The pattern to redact.
-
parameter
optionsHash Additional options to pass to the output.
Implementation
def initialize(output, redact: REDACT, **options)
super(output, **options)
@redact = redact
end
def redact?(text)
Check if the given text should be redacted.
Signature
-
parameter
textString The text to check.
-
returns
Boolean Whether the text should be redacted.
Implementation
def redact?(text)
text.match?(@redact)
end
def redact_hash(arguments, filter)
Redact sensitive information from a hash.
Signature
-
parameter
argumentsHash The hash to redact.
-
parameter
filterProc An optional filter to apply to redacted text.
-
returns
Hash The redacted hash.
Implementation
def redact_hash(arguments, filter)
arguments.transform_values do |value|
redact(value, filter)
end
end
def redact_array(array, filter)
Redact sensitive information from an array.
Signature
-
parameter
arrayArray The array to redact.
-
parameter
filterProc An optional filter to apply to redacted text.
-
returns
Array The redacted array.
Implementation
def redact_array(array, filter)
array.map do |value|
redact(value, filter)
end
end
def redact(argument, filter)
Redact sensitive information from the given argument.
Signature
-
parameter
argumentString | Array | Hash The argument to redact.
-
parameter
filterProc An optional filter to apply to redacted text.
-
returns
String | Array | Hash The redacted argument.
Implementation
def redact(argument, filter)
case argument
when String
if filter
filter.call(argument)
elsif redact?(argument)
"[REDACTED]"
else
argument
end
when Array
redact_array(argument, filter)
when Hash
redact_hash(argument, filter)
else
redact(argument.to_s, filter)
end
end
def call(subject = nil, *arguments, sensitive: true, **options, &block)
Write a message to the output, filtering sensitive information if necessary.
Signature
-
parameter
subjectString The subject of the message.
-
parameter
argumentsArray The arguments to output.
-
parameter
sensitiveBoolean | Filter | Hash Whether to filter sensitive information.
-
parameter
optionsHash Additional options to pass to the output.
-
parameter
blockProc An optional block to pass to the output.
Implementation
def call(subject = nil, *arguments, sensitive: true, **options, &block)
if sensitive
if sensitive.respond_to?(:call)
filter = sensitive
elsif sensitive.is_a?(Hash)
filter = Filter.new(sensitive)
end
subject = redact(subject, filter)
arguments = redact_array(arguments, filter)
end
super(subject, *arguments, **options)
end