Module: Philiprehberger::Mask::Scrubber
- Defined in:
- lib/philiprehberger/mask/scrubber.rb
Overview
Scrub sensitive patterns from strings
Class Method Summary collapse
-
.call(string, patterns:, mode: :full) ⇒ String
Apply all patterns to a string.
-
.call_with_audit(string, patterns:) ⇒ Hash
Apply all patterns and collect audit trail.
-
.call_with_tokens(string, patterns:) ⇒ Hash
Apply all patterns and return tokenized result.
Class Method Details
.call(string, patterns:, mode: :full) ⇒ String
Apply all patterns to a string
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/philiprehberger/mask/scrubber.rb', line 13 def self.call(string, patterns:, mode: :full) return string unless string.is_a?(String) result = string.dup patterns.each do |pat| result = result.gsub(pat[:pattern]) do |match| apply_mode(match, pat, mode) end end result end |
.call_with_audit(string, patterns:) ⇒ Hash
Apply all patterns and collect audit trail
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/philiprehberger/mask/scrubber.rb', line 30 def self.call_with_audit(string, patterns:) return { result: string, audit: [] } unless string.is_a?(String) audit = [] result = string.dup patterns.each do |pat| result = result.gsub(pat[:pattern]) do |match| masked = pat[:replacer].call(match) audit << { detector: pat[:name], original: match, masked: masked, position: Regexp.last_match&.begin(0) } masked end end { result: result, audit: audit } end |
.call_with_tokens(string, patterns:) ⇒ Hash
Apply all patterns and return tokenized result
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/philiprehberger/mask/scrubber.rb', line 55 def self.call_with_tokens(string, patterns:) return { masked: string, tokens: {} } unless string.is_a?(String) tokens = {} counter = 0 result = string.dup patterns.each do |pat| result = result.gsub(pat[:pattern]) do |match| counter += 1 token = "<TOKEN_#{pat[:name].to_s.upcase}_#{counter}>" tokens[token] = match token end end { masked: result, tokens: tokens } end |