Class: Console::Output::Sensitive

Inherits:
Wrapper
  • 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

Methods inherited from Wrapper

#verbose!

Constructor Details

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

Returns a new instance of Sensitive.



37
38
39
40
41
# File 'lib/console/output/sensitive.rb', line 37

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

Instance Method Details

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



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

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



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

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)


43
44
45
# File 'lib/console/output/sensitive.rb', line 43

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

#redact_array(array, filter) ⇒ Object



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

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

#redact_hash(arguments, filter) ⇒ Object



47
48
49
50
51
# File 'lib/console/output/sensitive.rb', line 47

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