Class: SwarmSDK::LogCollector::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/log_collector.rb

Overview

Subscription object with filtering capabilities

Encapsulates a callback with optional filters. Filters can match against agent name, event type, swarm_id, or any event field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter: {}, &callback) ⇒ Subscription

Initialize a subscription

Parameters:

  • (defaults to: {})

    Filter criteria

  • Block to call when event matches



69
70
71
72
73
# File 'lib/swarm_sdk/log_collector.rb', line 69

def initialize(filter: {}, &callback)
  @id = SecureRandom.uuid
  @filter = normalize_filter(filter)
  @callback = callback
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



63
64
65
# File 'lib/swarm_sdk/log_collector.rb', line 63

def callback
  @callback
end

#filterObject (readonly)

Returns the value of attribute filter.



63
64
65
# File 'lib/swarm_sdk/log_collector.rb', line 63

def filter
  @filter
end

#idObject (readonly)

Returns the value of attribute id.



63
64
65
# File 'lib/swarm_sdk/log_collector.rb', line 63

def id
  @id
end

Instance Method Details

#matches?(event) ⇒ Boolean

Check if event matches filter criteria

Empty filter matches all events. Multiple filter keys are AND'd together.

Parameters:

  • Event entry with :type, :agent, etc.

Returns:

  • True if event matches filter



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/swarm_sdk/log_collector.rb', line 81

def matches?(event)
  return true if @filter.empty?

  @filter.all? do |key, matcher|
    value = event[key]
    case matcher
    when Array
      # Match if value is in array (handles symbols/strings)
      matcher.include?(value) || matcher.map(&:to_s).include?(value.to_s)
    when Regexp
      # Regex matching
      value.to_s.match?(matcher)
    when Proc
      # Custom matcher
      matcher.call(value)
    else
      # Exact match (handles symbols/strings)
      matcher == value || matcher.to_s == value.to_s
    end
  end
end