Class: Console::Output::Sensitive

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

Defined Under Namespace

Classes: Filter

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(output, **options) ⇒ Sensitive

Returns a new instance of Sensitive.



11
12
13
# File 'lib/console/output/sensitive.rb', line 11

def initialize(output, **options)
	@output = output
end

Instance Method Details

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



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

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
	
	@output.call(subject, *arguments, **options)
end

#redact(argument, filter) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/console/output/sensitive.rb', line 57

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/console/output/sensitive.rb', line 41

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

#redact_array(array, filter) ⇒ Object



51
52
53
54
55
# File 'lib/console/output/sensitive.rb', line 51

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

#redact_hash(arguments, filter) ⇒ Object



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

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