Class: Console::Output::Sensitive

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/console/output/sensitive.rb

Overview

Redact sensitive information from output.

Defined Under Namespace

Classes: Filter

Constant Summary collapse

REDACT =

Default redaction pattern.

/
	phone
	| email
	| full_?name
	| first_?name
	| last_?name
	
	| device_name
	| user_agent
	
	| zip
	| address
	| location
	| latitude
	| longitude
	
	| ip
	| gps
	
	| sex
	| gender
	
	| token
	| password
/xi

Instance Attribute Summary

Attributes inherited from Wrapper

#delegate

Instance Method Summary collapse

Methods inherited from Wrapper

#The output to delegate to.=, #last_output, #verbose!

Constructor Details

#initialize(output, redact: REDACT, **options) ⇒ Sensitive

Create a new sensitive output wrapper.



44
45
46
47
48
# File 'lib/console/output/sensitive.rb', line 44

def initialize(output, redact: REDACT, **options)
	super(output, **options)
	
	@redact = redact
end

Instance Method Details

#call(subject = nil, *arguments, sensitive: true, **options, &block) ⇒ Object

Write a message to the output, filtering sensitive information if necessary.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/console/output/sensitive.rb', line 130

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

#redact(argument, filter) ⇒ Object

Redact sensitive information from the given argument.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/console/output/sensitive.rb', line 85

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

#redact?(text) ⇒ Boolean

Check if the given text should be redacted.

Returns:

  • (Boolean)


54
55
56
# File 'lib/console/output/sensitive.rb', line 54

def redact?(text)
	text.match?(@redact)
end

#redact_array(array, filter) ⇒ Object

Redact sensitive information from an array.



74
75
76
77
78
# File 'lib/console/output/sensitive.rb', line 74

def redact_array(array, filter)
	array.map do |value|
		redact(value, filter)
	end
end

#redact_hash(arguments, filter) ⇒ Object

Redact sensitive information from a hash.



63
64
65
66
67
# File 'lib/console/output/sensitive.rb', line 63

def redact_hash(arguments, filter)
	arguments.transform_values do |value|
		redact(value, filter)
	end
end