Class: Airbrake::Truncator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake-ruby/truncator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is responsible for truncation of too big objects. Mainly, you should use it for simple objects such as strings, hashes, & arrays.

Since:

  • v1.0.0

Constant Summary collapse

ENCODING_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns the options for String#encode.

Returns:

  • (Hash)

    the options for String#encode

Since:

  • v1.0.0

{ invalid: :replace, undef: :replace }.freeze
TEMP_ENCODING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns the temporary encoding to be used when fixing invalid strings with ENCODING_OPTIONS.

Returns:

  • (String)

    the temporary encoding to be used when fixing invalid strings with ENCODING_OPTIONS

Since:

  • v1.0.0

'utf-16'.freeze
SUPPORTED_ENCODINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns encodings that are eligible for fixing invalid characters.

Returns:

  • (Array<Encoding>)

    encodings that are eligible for fixing invalid characters

Since:

  • v1.0.0

[Encoding::UTF_8, Encoding::ASCII].freeze
CIRCULAR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns what to append when something is a circular reference.

Returns:

  • (String)

    what to append when something is a circular reference

Since:

  • v1.0.0

'[Circular]'.freeze
TRUNCATED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns what to append when something is truncated.

Returns:

  • (String)

    what to append when something is truncated

Since:

  • v1.0.0

'[Truncated]'.freeze
CIRCULAR_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns The types that can contain references to itself.

Returns:

  • (Array<Class>)

    The types that can contain references to itself

Since:

  • v1.0.0

[Array, Hash, Set].freeze

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Truncator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Truncator.

Parameters:

  • max_size (Integer)

    maximum size of hashes, arrays and strings

Since:

  • v1.0.0



29
30
31
# File 'lib/airbrake-ruby/truncator.rb', line 29

def initialize(max_size)
  @max_size = max_size
end

Instance Method Details

#reduce_max_sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reduces maximum allowed size of hashes, arrays, sets & strings by half.

Returns:

  • (Integer)

    current max_size value

Since:

  • v1.0.0



50
51
52
# File 'lib/airbrake-ruby/truncator.rb', line 50

def reduce_max_size
  @max_size /= 2
end

#truncate(object, seen = Set.new) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs deep truncation of arrays, hashes, sets & strings. Uses a placeholder for recursive objects (‘[Circular]`).

Parameters:

  • object (Object)

    The object to truncate

  • seen (Set) (defaults to: Set.new)

    The cache that helps to detect recursion

Returns:

  • (Object)

    truncated object

Since:

  • v1.0.0



39
40
41
42
43
44
45
46
# File 'lib/airbrake-ruby/truncator.rb', line 39

def truncate(object, seen = Set.new)
  if seen.include?(object.object_id)
    return CIRCULAR if CIRCULAR_TYPES.any? { |t| object.is_a?(t) }

    return object
  end
  truncate_object(object, seen << object.object_id)
end