Sha256: c9f7ed1a80546539b2b36c2ca5feccc51a835f2eb23cd8a7a4fba136ce6b8c99

Contents?: true

Size: 1.8 KB

Versions: 20

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2022, by Samuel Williams.

require_relative '../serialized/logger'

module Console
	module Output
		class Sensitive
			def initialize(output, **options)
				@output = output
			end
			
			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
			
			def redact?(text)
				text.match?(REDACT)
			end
			
			def redact_hash(arguments, filter)
				arguments.transform_values do |value|
					redact(value, filter)
				end
			end
			
			def redact_array(array, filter)
				array.map do |value|
					redact(value, filter)
				end
			end
			
			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
			
			class Filter
				def initialize(substitutions)
					@substitutions = substitutions
					@pattern = Regexp.union(substitutions.keys)
				end
				
				def call(text)
					text.gsub(@pattern, @substitutions)
				end
			end
			
			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
		end
	end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
console-1.24.0 lib/console/output/sensitive.rb
console-1.23.7 lib/console/output/sensitive.rb
console-1.23.6 lib/console/output/sensitive.rb
console-1.23.5 lib/console/output/sensitive.rb
console-1.23.4 lib/console/output/sensitive.rb
console-1.23.3 lib/console/output/sensitive.rb
console-1.23.2 lib/console/output/sensitive.rb
console-1.23.1 lib/console/output/sensitive.rb
console-1.23.0 lib/console/output/sensitive.rb
console-1.22.0 lib/console/output/sensitive.rb
console-1.21.0 lib/console/output/sensitive.rb
console-1.20.0 lib/console/output/sensitive.rb
console-1.19.0 lib/console/output/sensitive.rb
console-1.18.0 lib/console/output/sensitive.rb
console-1.17.4 lib/console/output/sensitive.rb
console-1.17.3 lib/console/output/sensitive.rb
console-1.17.2 lib/console/output/sensitive.rb
console-1.17.1 lib/console/output/sensitive.rb
console-1.17.0 lib/console/output/sensitive.rb
console-1.16.2 lib/console/output/sensitive.rb