Class: RightSupport::Notifier::Blacklister::Wildcard

Inherits:
RegularExpression show all
Defined in:
lib/right_support/notifiers/blacklisters/wildcard.rb

Overview

implements a blacklister that matches key paths by wildcard pattern interpreted as a key/value hierarchy represented as slash (/) delimited parts of the pattern string. supports the usual [*,?] wildcard characters as well as the more advanced multiple-indirection “**/” pattern.

example:

patterns = ['a/*/c']
data = {a: {b: c: 42}}
result == {a: {b: c: 'HIDDEN'}}

Instance Attribute Summary

Attributes inherited from RegularExpression

#delimiter

Attributes inherited from Base

#replacement_value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RegularExpression

#filter, normalize_regular_expressions, to_regexp

Methods inherited from Base

#filter

Constructor Details

#initialize(patterns, options = {}) ⇒ Wildcard

Returns a new instance of Wildcard.

Parameters:

  • patterns (Array|String)

    to blacklist

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :replacement_value (Object)

    of any kind including nil. default=‘HIDDEN’.



39
40
41
42
43
44
45
46
# File 'lib/right_support/notifiers/blacklisters/wildcard.rb', line 39

def initialize(patterns, options = {})
  if options[:delimiter]
    raise ::ArgumentError, 'options[:delimiter] is not allowed for wildcards.'
  end
  super(
    ::RightSupport::Notifier::Blacklister::Wildcard.to_regular_expressions(Array(patterns)),
    options)
end

Class Method Details

.to_regular_expressions(patterns) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/right_support/notifiers/blacklisters/wildcard.rb', line 48

def self.to_regular_expressions(patterns)
  patterns.map do |pattern|
    # remove leading/trailing slashes
    here = pattern == 'a/b/*'
    pattern = pattern.to_s.gsub(/^[\/]+(.*)/, '\2').gsub(/(.*)[\/]+$/, '\1')

    # remove trailing "/*" as matching the parent will obfuscate all children.
    while pattern.end_with?('/*')
      pattern = pattern[0...-2]
    end
    pattern = ::Regexp.escape(pattern)      # escape all
    pattern = pattern.gsub('\?', '[^/]')    # question mark == match any single non-slash character
    pattern = pattern.gsub('\*\*/', '.*/')  # '**/' == match at any depth
    pattern = pattern.gsub('\*', '[^/]*')   # asterisk == match zero or more non-slash characters
    ::Regexp.compile(pattern)
  end
end