Module: Appsignal::Utils

Defined in:
lib/appsignal/utils.rb

Class Method Summary collapse

Class Method Details

.encode_utf8(value) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/appsignal/utils.rb', line 53

def self.encode_utf8(value)
  value.encode(
    'utf-8'.freeze,
    :invalid => :replace,
    :undef   => :replace
  )
end

.json_generate(body) ⇒ Object


34
35
36
# File 'lib/appsignal/utils.rb', line 34

def self.json_generate(body)
  JSON.generate(jsonify(body))
end

.jsonify(value) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/appsignal/utils.rb', line 38

def self.jsonify(value)
  case value
  when String
    encode_utf8(value)
  when Numeric, NilClass, TrueClass, FalseClass
    value
  when Hash
    Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
  when Array
    value.map { |v| jsonify(v) }
  else
    jsonify(value.to_s)
  end
end

.sanitize(params, only_top_level = false, key_sanitizer = nil) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/appsignal/utils.rb', line 3

def self.sanitize(params, only_top_level=false, key_sanitizer=nil)
  if params.is_a?(Hash)
    {}.tap do |hsh|
      params.each do |key, val|
        hsh[self.sanitize_key(key, key_sanitizer)] = if only_top_level
          '?'
        else
          sanitize(val, only_top_level, key_sanitizer=nil)
        end
      end
    end
  elsif params.is_a?(Array)
    if only_top_level
      sanitize(params[0], only_top_level, key_sanitizer=nil)
    else
      params.map do |item|
        sanitize(item, only_top_level, key_sanitizer=nil)
      end.uniq
    end
  else
    '?'
  end
end

.sanitize_key(key, sanitizer) ⇒ Object


27
28
29
30
31
32
# File 'lib/appsignal/utils.rb', line 27

def self.sanitize_key(key, sanitizer)
  case sanitizer
  when :mongodb then key.to_s.gsub(/(\..+)/, '.?')
  else key
  end
end