Sha256: a99a5116d5c70e2d33dd3e66e15ccb9edc460249e72a64893969aeca82c6482a

Contents?: true

Size: 913 Bytes

Versions: 3

Compression:

Stored size: 913 Bytes

Contents

module Routemaster
  module Middleware
    # Filters out events based on their topic and passes them to a handling class
    #
    # `use Middleware::Siphon, 'some_topic' => SomeTopicHandler`
    #
    #  Topic handlers are initialized with the full event payload and must respond to `#call`
    class Siphon
      def initialize(app, siphon_events: nil)
        @app = app
        @processors = siphon_events || {}
      end

      def call(env)
        siphoned, non_siphoned = env.fetch('routemaster.payload', []).partition do |event|
          topics_to_siphon.include? event['topic']
        end
        siphoned.each do |event|
          @processors[event['topic']].new(event).call
        end
        env['routemaster.payload'] = non_siphoned
        @app.call(env)
      end

      private

      def topics_to_siphon
        @topics_to_siphon ||= @processors.keys.map(&:to_s)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
routemaster-drain-2.5.2 lib/routemaster/middleware/siphon.rb
routemaster-drain-2.5.1 lib/routemaster/middleware/siphon.rb
routemaster-drain-2.5.0 lib/routemaster/middleware/siphon.rb