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
output
Console::Output
The output to wrap.
-
parameter
redact
Regexp
The pattern to redact.
-
parameter
options
Hash
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
text
String
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
arguments
Hash
The hash to redact.
-
parameter
filter
Proc
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
array
Array
The array to redact.
-
parameter
filter
Proc
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
argument
String | Array | Hash
The argument to redact.
-
parameter
filter
Proc
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
subject
String
The subject of the message.
-
parameter
arguments
Array
The arguments to output.
-
parameter
sensitive
Boolean | Filter | Hash
Whether to filter sensitive information.
-
parameter
options
Hash
Additional options to pass to the output.
-
parameter
block
Proc
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