Class: Airbrake::FilterChain Private

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake-ruby/filter_chain.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.

FilterChain represents an ordered array of filters.

A filter is an object that responds to #call (typically a Proc or a class that implements the call method). The #call method must accept exactly one argument: an object to be filtered.

When you add a new filter to the chain, it gets inserted according to its weight. Smaller weight means the filter will be somewhere in the beginning of the array. Larger - in the end. If a filter doesn’t implement weight, the chain assumes it’s equal to 0.

Examples:

class MyFilter
  attr_reader :weight

  def initialize
    @weight = 1
  end

  def call(obj)
    puts 'Filtering...'
    obj[:data] = '[Filtered]'
  end
end

filter_chain = FilterChain.new
filter_chain.add_filter(MyFilter)

filter_chain.refine(obj)
#=> Filtering...

See Also:

Since:

  • v1.0.0

Constant Summary collapse

DEFAULT_WEIGHT =

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:

  • (Integer)

Since:

  • v1.0.0

0

Instance Method Summary collapse

Constructor Details

#initializeFilterChain

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 FilterChain.

Since:

  • v1.0.0



40
41
42
# File 'lib/airbrake-ruby/filter_chain.rb', line 40

def initialize
  @filters = []
end

Instance Method Details

#add_filter(filter) ⇒ void

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.

This method returns an undefined value.

Adds a filter to the filter chain. Sorts filters by weight.

Parameters:

  • filter (#call)

    The filter object (proc, class, module, etc)

Since:

  • v1.0.0



48
49
50
51
52
# File 'lib/airbrake-ruby/filter_chain.rb', line 48

def add_filter(filter)
  @filters = (@filters << filter).sort_by do |f|
    f.respond_to?(:weight) ? f.weight : DEFAULT_WEIGHT
  end.reverse!
end

#delete_filter(filter_class) ⇒ void

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.

This method returns an undefined value.

Deletes a filter from the the filter chain.

Parameters:

  • filter_class (Class)

    The class of the filter you want to delete

Since:

  • v3.1.0



59
60
61
62
63
64
# File 'lib/airbrake-ruby/filter_chain.rb', line 59

def delete_filter(filter_class)
  # rubocop:disable Style/ClassEqualityComparison
  index = @filters.index { |f| f.class.name == filter_class.name }
  # rubocop:enable Style/ClassEqualityComparison
  @filters.delete_at(index) if index
end

#includes?(filter_class) ⇒ Boolean

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 true if the current chain has an instance of the given class, false otherwise.

Parameters:

  • filter_class (Class)

Returns:

  • (Boolean)

    true if the current chain has an instance of the given class, false otherwise

Since:

  • v4.14.0



102
103
104
# File 'lib/airbrake-ruby/filter_chain.rb', line 102

def includes?(filter_class)
  filter_classes.include?(filter_class)
end

#inspectString

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 customized inspect to lessen the amount of clutter.

Returns:

  • (String)

    customized inspect to lessen the amount of clutter

Since:

  • v1.0.0



81
82
83
# File 'lib/airbrake-ruby/filter_chain.rb', line 81

def inspect
  filter_classes.to_s
end

#pretty_print(q) ⇒ String

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 #inspect for PrettyPrint.

Returns:

Since:

  • v1.0.0



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/airbrake-ruby/filter_chain.rb', line 86

def pretty_print(q)
  q.text('[')

  # Make nesting of the first element consistent on JRuby and MRI.
  q.nest(2) { q.breakable } if @filters.any?

  q.nest(2) do
    q.seplist(@filters) { |f| q.pp(f.class) }
  end
  q.text(']')
end

#refine(notice) ⇒ void

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.

TODO:

Make it work with anything, not only notices

This method returns an undefined value.

Applies all the filters in the filter chain to the given notice. Does not filter ignored notices.

Parameters:

Since:

  • v1.0.0



72
73
74
75
76
77
78
# File 'lib/airbrake-ruby/filter_chain.rb', line 72

def refine(notice)
  @filters.each do |filter|
    break if notice.ignored?

    filter.call(notice)
  end
end