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.



28
29
30
# File 'lib/console/output/sensitive.rb', line 28

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

Instance Method Details

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



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

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



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

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)


58
59
60
# File 'lib/console/output/sensitive.rb', line 58

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

#redact_array(array, filter) ⇒ Object



68
69
70
71
72
# File 'lib/console/output/sensitive.rb', line 68

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

#redact_hash(arguments, filter) ⇒ Object



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

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