Sha256: 030815599e178f6bb5a1c49073bc1974c696b4a3e480a5e40c2429b5e435bb25

Contents?: true

Size: 934 Bytes

Versions: 15

Compression:

Stored size: 934 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, 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

15 entries across 15 versions & 1 rubygems

Version Path
routemaster-drain-3.6.2 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.1 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.6.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.5.1 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.5.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.4.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.3.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.2.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.1.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.0.3 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.0.2 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.0.1 lib/routemaster/middleware/siphon.rb
routemaster-drain-3.0.0 lib/routemaster/middleware/siphon.rb
routemaster-drain-2.5.4 lib/routemaster/middleware/siphon.rb
routemaster-drain-2.5.3 lib/routemaster/middleware/siphon.rb