Sha256: d66c45779bbefcde5d0238ee4fe4e6f378d005c09ce0a7574becd4dc154b52fa

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module BoomNats
  class RouteTopic
    attr_reader :topic, :executor, :queue, :serializer, :parser, :options

    def initialize(application)
      @application = application
    end

    def setup(topic_name, executor, serializer: nil, parser: nil, queue: nil, options: nil)
      @topic = topic_name
      @executor = executor
      @serializer = default_serializer(serializer)
      @parser = default_parser(parser)
      @queue = queue
      @options = default_options(options, queue)

      subscribe
    end

    protected

    def default_parser(parser)
      [
        parser,
        BoomNats::Serializer.default_parser
      ].each do |entry|
        return entry if entry.respond_to? :call
      end
      BoomNats::Serializer::JSONParser
    end

    def default_serializer(serializer)
      [
        serializer,
        BoomNats::Serializer.default_serializer
      ].each do |entry|
        return entry if entry.respond_to? :call
      end
      BoomNats::Serializer::JSONSerializer
    end

    def default_options(options = {}, queue = nil)
      {
        queue: queue,
        max: nil,
        **(options.is_a?(Hash) ? options : {})
      }
    end

    def subscribe
      @application.route_topics << self
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
boom_nats-0.1.5 lib/boom_nats/route_topic.rb
boom_nats-0.1.4 lib/boom_nats/route_topic.rb
boom_nats-0.1.3 lib/boom_nats/route_topic.rb
boom_nats-0.1.2 lib/boom_nats/route_topic.rb
boom_nats-0.1.1 lib/boom_nats/route_topic.rb
boom_nats-0.1.0 lib/boom_nats/route_topic.rb