Class: Pechkin::MessageMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkin/message_matcher.rb

Overview

Allows to match message configuration against received data.

Data is checked againts either allow or forbid rules. But not both at the same time. Each field can contain list of rules to check. ‘allow’ list means we need at least one matching rule to allow data processing. And ‘forbid’ list respectievly means we need at least one matching rule to decline data processing.

Constant Summary collapse

KEY_ALLOW =
'allow'.freeze
KEY_FORBID =
'forbid'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ MessageMatcher

Returns a new instance of MessageMatcher.



17
18
19
# File 'lib/pechkin/message_matcher.rb', line 17

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/pechkin/message_matcher.rb', line 15

def logger
  @logger
end

Instance Method Details

#matches?(message_config, data) ⇒ Boolean

Checks data object against allow / forbid rule sets in message configuration. If data object matches rules it means we can process this data and send message.

Parameters:

  • message_config (Hash)

    message description.

  • data (Hash)

    request object that need to be inspected whether we should process this data or not

Returns:

  • (Boolean)

    is data object matches message_config rules or not



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pechkin/message_matcher.rb', line 29

def matches?(message_config, data)
  check(message_config)

  if message_config.key?(KEY_ALLOW)
    rules = message_config[KEY_ALLOW]
    rules.any? { |r| check_rule(r, r, data) }
  elsif message_config.key?(KEY_FORBID)
    rules = message_config[KEY_FORBID]
    rules.all? { |r| !check_rule(r, r, data) }
  else
    true
  end
end