Sha256: e251ff768548e32dd8d4750be5dce9c721d9b17713fa7887030a915922cb6ec5

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Msgr
  class Route
    attr_reader :consumer, :action, :opts

    MATCH_REGEXP = /\A(?<consumer>\w+)#(?<action>\w+)\z/.freeze
    def initialize(key, opts = {})
      @opts = opts
      raise ArgumentError.new 'Missing `to` options.' unless @opts[:to]

      add key

      result = MATCH_REGEXP.match(opts[:to].strip.to_s)

      unless result
        raise ArgumentError.new \
          "Invalid consumer format: #{opts[:to].strip.to_s.inspect}. " \
          'Must be `consumer_class#action`.'
      end

      @consumer = "#{result[:consumer].camelize}Consumer"
      @action   = result[:action].underscore
    end

    def keys
      @keys ||= []
    end
    alias routing_keys keys

    def prefetch
      @opts[:prefetch] || 1
    end

    def add(key)
      raise ArgumentError.new 'Routing key required.' unless key.present?

      keys << key
    end

    def accept?(_key, opts)
      self.opts == opts
    end

    def name
      "msgr.consumer.#{consumer}.#{action}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
msgr-1.3.2 lib/msgr/route.rb
msgr-1.3.1 lib/msgr/route.rb
msgr-1.3.0 lib/msgr/route.rb