lib/bunny_mock/exchanges/topic.rb in bunny-mock-1.3.0 vs lib/bunny_mock/exchanges/topic.rb in bunny-mock-1.4.0
- old
+ new
@@ -22,24 +22,22 @@
# @param [String] key Routing key
#
# @api public
#
def deliver(payload, opts, key)
- # escape periods with backslash for regex
- key = key.gsub('.', '\.')
+ delivery_routes = @routes.dup.keep_if { |route, _| key =~ route_to_regex(route) }
+ delivery_routes.values.each { |dest| dest.publish(payload, opts) }
+ end
- # replace single wildcards with regex for a single domain
- key = key.gsub(SINGLE_WILDCARD, '(?:\w+)')
+ private
- # replace multi wildcards with regex for many domains separated by '.'
+ # @private
+ def route_to_regex(key)
+ key = key.gsub('.', '\.')
+ key = key.gsub(SINGLE_WILDCARD, '(?:\w+)')
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
+ Regexp.new(key)
end
end
end
end