Class: WCC::Filters

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/filter.rb

Constant Summary collapse

@@filters =
{}

Class Method Summary collapse

Class Method Details

.accept(data, filters) ⇒ Boolean

Called by wcc check routine to evaluate all filters and produce and’ed result of their boolean returns.

Parameters:

  • data (Object)

    arbitrary data the filters might use

  • filters (Array)

    list of FilterRefs with the IDs of the filters to be executed

Returns:

  • (Boolean)

    true if all filters returned true, false otherwise



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wcc/filter.rb', line 60

def self.accept(data, filters)
	return true if filters.nil?
	
	WCC.logger.info "Testing with filters: #{filters.join(', ')}"
	filters.each do |fref|
		block = @@filters[fref.id]
		if block.nil?
			WCC.logger.error "Requested filter '#{fref.id}' not found, skipping it."
			next
		end
		if not block.call(data, fref.arguments)
			WCC.logger.info "Filter '#{fref.id}' failed!"
			return false
		end
	end
	true
end

.add(id, &block) ⇒ Object

API method - register a filters code block under given ID. Should be called by filters the following way:

WCC::Filters.add 'filter-name' { block }

Parameters:

  • id (String)

    the “name” of the filter

  • block (Proc)

    a block of code returning true (Accept) or false (Decline) as the filters result



25
26
27
28
# File 'lib/wcc/filter.rb', line 25

def self.add(id, &block)
	WCC.logger.info "Adding filter '#{id}'"
	@@filters[id] = block
end

.call(data, id, args = {}) ⇒ Boolean

API method - invoke the specfied filter and give it’s result.

Parameters:

  • data (Object)

    arbitrary data the filter might use

  • id (String)

    the “name” of the filter

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

    the arguments of the filter

Returns:

  • (Boolean)

    true if filter returned true, false otherwise



36
37
38
39
40
41
42
# File 'lib/wcc/filter.rb', line 36

def self.call(data, id, args = {})
	block = @@filters[id]
	if block.nil?
		raise "Call to requested filter '#{id}' failed - filter not found!"
	end
	block.call(data, args)
end

.debug(msg) ⇒ Object

API method - log msg as debug



51
# File 'lib/wcc/filter.rb', line 51

def self.debug(msg); WCC.logger.debug "filter: #{msg}" end

.error(msg) ⇒ Object

API method - log msg as error



45
# File 'lib/wcc/filter.rb', line 45

def self.error(msg); WCC.logger.error "filter: #{msg}" end

.info(msg) ⇒ Object

API method - log msg as info



49
# File 'lib/wcc/filter.rb', line 49

def self.info(msg); WCC.logger.info "filter: #{msg}" end

.warn(msg) ⇒ Object

API method - log msg as warn



47
# File 'lib/wcc/filter.rb', line 47

def self.warn(msg); WCC.logger.warn "filter: #{msg}" end