Class: TensorBuzz::Api::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/tensorbuzz/api/sanitizer.rb

Constant Summary collapse

MAX_ARRAY_LENGTH =
100
MAX_DEPTH =
8
MAX_HASH_KEYS =
100
MAX_STRING_LENGTH =
4_000
SENSITIVE_KEY =
/(authorization|cookie|password|secret|session|token)/i

Class Method Summary collapse

Class Method Details

.call(value, depth: 0, key: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tensorbuzz/api/sanitizer.rb', line 12

def self.call(value, depth: 0, key: nil)
  return '[redacted]' if key.to_s.match?(SENSITIVE_KEY)
  return '[maximum depth reached]' if depth >= MAX_DEPTH

  case value
  when Array
    value.first(MAX_ARRAY_LENGTH).map { |item| call(item, depth: depth + 1) }
  when Hash
    value.first(MAX_HASH_KEYS).to_h do |child_key, child_value|
      [child_key.to_s, call(child_value, depth: depth + 1, key: child_key)]
    end
  when String
    truncate(value)
  when Numeric, TrueClass, FalseClass, NilClass
    value
  else
    truncate(value.to_s)
  end
end