Sha256: 487f99937fc6699cdb0ba39a9e0858031937b7371057d81554aeab35613965cd

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
module BunnyMock
  module Exchanges
    class Topic < BunnyMock::Exchange
      # @private
      # @return [String] Multiple subdomain wildcard
      MULTI_WILDCARD = '#'

      # @private
      # @return [String] Single subdomain wildcard
      SINGLE_WILDCARD = '*'

      #
      # API
      #

      ##
      # Deliver a message to route with keys matching wildcards
      #
      # @param [Object] payload Message content
      # @param [Hash] opts Message properties
      # @param [String] key Routing key
      #
      # @api public
      #
      def deliver(payload, opts, key)
        # escape periods with backslash for regex
        key = key.gsub('.', '\.')

        # replace single wildcards with regex for a single domain
        key = key.gsub(SINGLE_WILDCARD, '(?:\w+)')

        # replace multi wildcards with regex for many domains separated by '.'
        key = key.gsub(MULTI_WILDCARD, '\w+\.?')

        # turn key into regex
        key = Regexp.new(key)

        @routes.each do |route, destination|
          destination.publish(payload, opts) if route =~ key
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunny-mock-1.3.0 lib/bunny_mock/exchanges/topic.rb