Class: Warren::MessageFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/warren/message_filter.rb,
lib/warren/filters/yaml.rb,
lib/warren/filters/shared_secret.rb

Overview

Handles filtering messages going onto/coming off the queue

Direct Known Subclasses

Yaml

Defined Under Namespace

Classes: SharedSecret, Yaml

Constant Summary collapse

@@filters =

Array of filters to be run on the message before its pushed to rabbit.

NB: These get called in reverse order from the array - the last filter to be added gets called first.

[]

Class Method Summary collapse

Class Method Details

.<<(filter) ⇒ Object Also known as: add_filter

Adds a filter to the list

A valid filter is just a class that defines self.pack and self.unpack methods, which both accept a single argument, act upon it, and return the output.

Example filter class (See also message_filters/*.rb)

class Foo
  def self.pack msg
    msg.reverse # Assumes msg responds to reverse
  end

  def self.unpack msg
    msg.reverse # Does the opposite of Foo#pack
  end
end


31
32
33
# File 'lib/warren/message_filter.rb', line 31

def << filter
  @@filters << filter
end

.filtersObject

Returns current array of filters



43
44
45
# File 'lib/warren/message_filter.rb', line 43

def self.filters
  @@filters
end

.inherited(klass) ⇒ Object

Called when a subclass is created, adds the subclass to the queue



38
39
40
# File 'lib/warren/message_filter.rb', line 38

def self.inherited klass
  add_filter klass
end

.pack(msg) ⇒ Object

Runs the raw message through all the filters and returns the filtered version



54
55
56
57
58
59
60
# File 'lib/warren/message_filter.rb', line 54

def self.pack msg
  @@filters.reverse.each do |f|
    # puts "Packing with #{f}"
    msg = f.send(:pack, msg)
  end
  msg
end

.reset_filtersObject

Resets the filters to default



48
49
50
# File 'lib/warren/message_filter.rb', line 48

def self.reset_filters
  @@filters = [Warren::MessageFilter::Yaml]
end

.unpack(msg) ⇒ Object

Runs the filtered message through all the filters and returns the raw version



64
65
66
67
68
69
70
# File 'lib/warren/message_filter.rb', line 64

def self.unpack msg
  @@filters.each do |f|
    # puts "Unpacking with #{f}"
    msg = f.unpack(msg)
  end
  msg
end