Class: Conrad::Processors::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/conrad/processors/envelope.rb

Overview

Processor class used to take a given event, extract a set of attributes from it, then wrap the remaining attributes into a payload attribute. This allows downstream consumers to know about the outermost structure (the envelope) without needing to account for all possible permutations of the event. This also allows routing through various systems on a small number of values. If a given envelope key does not exist in the original event, then it will be set to nil in the resulting event.

When using this processor, it is highly recommended that this be the last processor unless you need to act on the wrapped event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(envelope_keys, payload_key: :payload) ⇒ Envelope

Returns a new instance of Envelope.

Parameters:

  • envelope_keys (Array<Symbol>)

    the keys to extract from the event. NOTE: These must be exact matches of both value and type (i.e. Strings and Symbols should not be considered interchangeable, and the event must be created with the attribute keys matching the types given here)

  • payload_key (Symbol) (defaults to: :payload)

    key to wrap the remainder of the event inside

Raises:

  • (TypeError)

    if the envelope_keys argument is not an Array



26
27
28
29
30
31
# File 'lib/conrad/processors/envelope.rb', line 26

def initialize(envelope_keys, payload_key: :payload)
  raise TypeError, 'envelope_keys must be an Array' unless envelope_keys.is_a? Array

  @envelope_keys = envelope_keys
  @payload_key = payload_key
end

Instance Attribute Details

#envelope_keysObject (readonly)

Processor class used to take a given event, extract a set of attributes from it, then wrap the remaining attributes into a payload attribute. This allows downstream consumers to know about the outermost structure (the envelope) without needing to account for all possible permutations of the event. This also allows routing through various systems on a small number of values. If a given envelope key does not exist in the original event, then it will be set to nil in the resulting event.

When using this processor, it is highly recommended that this be the last processor unless you need to act on the wrapped event.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/conrad/processors/envelope.rb', line 16

class Envelope
  attr_reader :envelope_keys, :payload_key

  # @param envelope_keys [Array<Symbol>] the keys to extract from the event.
  #   NOTE: These must be exact matches of both value and type (i.e. Strings
  #   and Symbols should not be considered interchangeable, and the event
  #   must be created with the attribute keys matching the types given here)
  # @param payload_key [Symbol] key to wrap the remainder of the event inside
  #
  # @raise [TypeError] if the envelope_keys argument is not an Array
  def initialize(envelope_keys, payload_key: :payload)
    raise TypeError, 'envelope_keys must be an Array' unless envelope_keys.is_a? Array

    @envelope_keys = envelope_keys
    @payload_key = payload_key
  end

  # @param event [Hash] event to be wrapped in the configured envelope
  #
  # @return [Hash] the wrapped event
  def call(event)
    envelope = envelope_keys.each_with_object({}) do |key, obj|
      obj[key] = event.delete(key)
    end

    envelope.merge(payload_key => event)
  end
end

#payload_keyObject (readonly)

Processor class used to take a given event, extract a set of attributes from it, then wrap the remaining attributes into a payload attribute. This allows downstream consumers to know about the outermost structure (the envelope) without needing to account for all possible permutations of the event. This also allows routing through various systems on a small number of values. If a given envelope key does not exist in the original event, then it will be set to nil in the resulting event.

When using this processor, it is highly recommended that this be the last processor unless you need to act on the wrapped event.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/conrad/processors/envelope.rb', line 16

class Envelope
  attr_reader :envelope_keys, :payload_key

  # @param envelope_keys [Array<Symbol>] the keys to extract from the event.
  #   NOTE: These must be exact matches of both value and type (i.e. Strings
  #   and Symbols should not be considered interchangeable, and the event
  #   must be created with the attribute keys matching the types given here)
  # @param payload_key [Symbol] key to wrap the remainder of the event inside
  #
  # @raise [TypeError] if the envelope_keys argument is not an Array
  def initialize(envelope_keys, payload_key: :payload)
    raise TypeError, 'envelope_keys must be an Array' unless envelope_keys.is_a? Array

    @envelope_keys = envelope_keys
    @payload_key = payload_key
  end

  # @param event [Hash] event to be wrapped in the configured envelope
  #
  # @return [Hash] the wrapped event
  def call(event)
    envelope = envelope_keys.each_with_object({}) do |key, obj|
      obj[key] = event.delete(key)
    end

    envelope.merge(payload_key => event)
  end
end

Instance Method Details

#call(event) ⇒ Hash

Returns the wrapped event.

Parameters:

  • event (Hash)

    event to be wrapped in the configured envelope

Returns:

  • (Hash)

    the wrapped event



36
37
38
39
40
41
42
# File 'lib/conrad/processors/envelope.rb', line 36

def call(event)
  envelope = envelope_keys.each_with_object({}) do |key, obj|
    obj[key] = event.delete(key)
  end

  envelope.merge(payload_key => event)
end