Module: Libhoney::Cleaner

Included in:
LogTransmissionClient, TransmissionClient
Defined in:
lib/libhoney/cleaner.rb

Constant Summary collapse

ENCODING_OPTIONS =
{ invalid: :replace, undef: :replace }.freeze
RECURSION =
'[RECURSION]'.freeze
RAISED =
'[RAISED]'.freeze

Instance Method Summary collapse

Instance Method Details

#clean_data(data, seen = {}) ⇒ Object

Cleans an object for converting to JSON. Checks for recursion, exceptions generated, and non UTF8 encoded strings.

Parameters:

  • data

    the data to clean

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

    used to check for recursion

Returns:

  • the cleaned object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libhoney/cleaner.rb', line 12

def clean_data(data, seen = {})
  return nil if data.nil?

  # check for recursion here, by tracking all of the potentially nested
  # objects that we have seen before.
  protection =  case data
                when Hash, Array, Set
                  # bail here if we have already seen this object
                  return seen[data] if seen[data]

                  seen[data] = RECURSION
                end

  value = case data
          when Hash
            clean_hash = {}
            data.each do |key, val|
              clean_hash[key] = clean_data(val, seen)
            end
            clean_hash
          when Array, Set
            data.map do |element|
              clean_data(element, seen)
            end
          when Numeric, TrueClass, FalseClass
            data
          when String
            clean_string(data)
          else
            str = begin
                    data.to_s
                  rescue StandardError
                    RAISED
                  end
            clean_string(str)
          end

  seen[data] = value if protection
  value
end

#clean_string(str) ⇒ String

Converts a string to UTF8 encoding if required using the ENCODING_OPTIONS

Parameters:

  • str (String)

    the string to convert

Returns:

  • (String)

    the UTF8 encoded string


56
57
58
59
60
# File 'lib/libhoney/cleaner.rb', line 56

def clean_string(str)
  return str if str.encoding == Encoding::UTF_8 && str.valid_encoding?

  str.encode(Encoding::UTF_8, **ENCODING_OPTIONS)
end