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.



26
27
28
# File 'lib/console/output/sensitive.rb', line 26

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

Instance Method Details

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/console/output/sensitive.rb', line 102

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/console/output/sensitive.rb', line 72

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)


56
57
58
# File 'lib/console/output/sensitive.rb', line 56

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

#redact_array(array, filter) ⇒ Object



66
67
68
69
70
# File 'lib/console/output/sensitive.rb', line 66

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

#redact_hash(arguments, filter) ⇒ Object



60
61
62
63
64
# File 'lib/console/output/sensitive.rb', line 60

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