Sha256: 3231302be8b53da3255a7f749eca2ceb0f450df44d819e6a5b27aad805f59b8b

Contents?: true

Size: 945 Bytes

Versions: 8

Compression:

Stored size: 945 Bytes

Contents

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

8 entries across 8 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.7.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.8 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.7 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.6 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.5 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.4 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.3 lib/routemaster/middleware/siphon.rb