class Lenjador::Preprocessors::Whitelist

Constants

DEFAULT_WHITELIST
MASKED_VALUE
MASK_SYMBOL
PRUNE_ACTION_NAMES

Public Class Methods

new(config = {}) click to toggle source
# File lib/lenjador/preprocessors/whitelist.rb, line 19
def initialize(config = {})
  trie = build_trie(config)

  @strategy = if PRUNE_ACTION_NAMES.include?(config[:action].to_s)
                Strategies::Prune.new(trie)
              else
                Strategies::Mask.new(trie)
              end
end

Public Instance Methods

process(data) click to toggle source
# File lib/lenjador/preprocessors/whitelist.rb, line 29
def process(data)
  @strategy.process(data)
end

Private Instance Methods

build_trie(config) click to toggle source
# File lib/lenjador/preprocessors/whitelist.rb, line 45
def build_trie(config)
  pointers = (config[:pointers] || []) + DEFAULT_WHITELIST

  pointers.reduce(JSONPointerTrie.new(**config)) do |trie, pointer|
    validate_pointer(pointer)

    trie.insert(decode(pointer))
  end
end
decode(pointer) click to toggle source
# File lib/lenjador/preprocessors/whitelist.rb, line 39
def decode(pointer)
  pointer
    .gsub('~1', '/')
    .gsub('~0', '~')
end
validate_pointer(pointer) click to toggle source
# File lib/lenjador/preprocessors/whitelist.rb, line 35
def validate_pointer(pointer)
  raise InvalidPointerFormatException, 'Pointer should not contain trailing slash' if pointer.slice(-1) == '/'
end