Class: Datadog::AppSec::Reactive::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/reactive/engine.rb

Overview

Reactive Engine

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



11
12
13
14
# File 'lib/datadog/appsec/reactive/engine.rb', line 11

def initialize
  @data = {}
  @subscribers = AddressHash.new
end

Instance Method Details

#publish(address, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datadog/appsec/reactive/engine.rb', line 20

def publish(address, value)
  # check if someone has address subscribed
  if @subscribers.addresses.include?(address)

    # someone will be interested, set value
    @data[address] = value

    # find candidates i.e address groups that contain the just posted address
    @subscribers.with(address).each do |addresses|
      # find targets to the address group containing the posted address
      subscribers = @subscribers[addresses]

      # is all data for the targets available?
      if (addresses - @data.keys).empty?
        hash = addresses.each_with_object({}) { |a, h| h[a] = @data[a] }
        subscribers.each { |s| s.call(*hash.values) }
      end
    end
  end
end

#subscribe(*addresses, &block) ⇒ Object



16
17
18
# File 'lib/datadog/appsec/reactive/engine.rb', line 16

def subscribe(*addresses, &block)
  @subscribers[addresses.freeze] << Subscriber.new(&block)
end