Sha256: df560288c0f423af1ffd43b86b38907b4720f5d405a2a5a5e0eba2da76cf0df0

Contents?: true

Size: 953 Bytes

Versions: 2

Compression:

Stored size: 953 Bytes

Contents

module AmqpTopicBinding
  class Matcher
    def initialize(pattern)
      @pattern = pattern || ''
      @pattern_parts = @pattern.split('.')
    end

    def matches?(routing_key)
      if routing_key.nil? || routing_key == ''
        return false
      end

      if @pattern == routing_key
        return true
      end

      routing_key_parts = routing_key.split('.')

      if @pattern_parts.length > routing_key_parts.length
        return false
      end

      last_checked_index = 0

      @pattern_parts.each_with_index do |pattern_part, index|
        if pattern_part == '#'
          return true
        end

        routing_key_part = routing_key_parts[index]

        if pattern_part != '*' && pattern_part != routing_key_part
          return false
        end

        last_checked_index = index
      end

      if routing_key_parts.length > (last_checked_index + 1)
        return false
      end

      return true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amqp_topic_binding-0.2.0 lib/amqp_topic_binding/matcher.rb
amqp_topic_binding-0.1.0 lib/amqp_topic_binding/matcher.rb