<commit_before># frozen_string_literal: true

class FastlyNsq::Listener
  DEFAULT_PRIORITY = 0
  DEFAULT_CONNECTION_TIMEOUT = 5 # seconds

  attr_reader :preprocessor, :topic, :processor, :priority, :channel, :logger, :consumer

  def initialize(topic:, processor:, preprocessor: FastlyNsq.preprocessor, channel: FastlyNsq.channel, consumer: nil,
                 logger: FastlyNsq.logger, priority: DEFAULT_PRIORITY, connect_timeout: DEFAULT_CONNECTION_TIMEOUT)

    raise ArgumentError, "processor #{processor.inspect} does not respond to #call" unless processor.respond_to?(:call)
    raise ArgumentError, "priority #{priority.inspect} must be a Integer" unless priority.is_a?(Integer)

    @channel      = channel
    @logger       = logger
    @preprocessor = preprocessor
    @processor    = processor
    @topic        = topic
    @priority     = priority

    @consumer = consumer || FastlyNsq::Consumer.new(topic: topic,
                                                    connect_timeout: connect_timeout,
                                                    channel: channel,
                                                    queue: FastlyNsq::Feeder.new(self, priority))

    FastlyNsq.manager.add_listener(self)
  end

  def call(nsq_message)
    message = FastlyNsq::Message.new(nsq_message)
    log message
    preprocessor&.call(message)
    result = processor.call(message)
    nsq_message.finish if result
  end

  def connected?
    consumer.connected?
  end

  def terminate
    return unless connected?
    consumer.terminate
    logger.info "< Consumer terminated: topic [#{topic}]"
  end

  private

  def log(message)
    logger&.info "[NSQ] Message received on topic [#{topic}]: #{message}"
  end
end<commit_msg>refactor: delegate Listener#connected? to consumer<commit_after># frozen_string_literal: true

class FastlyNsq::Listener
  extend Forwardable

  DEFAULT_PRIORITY = 0
  DEFAULT_CONNECTION_TIMEOUT = 5 # seconds

  def_delegators :consumer, :connected?

  attr_reader :preprocessor, :topic, :processor, :priority, :channel, :logger, :consumer

  def initialize(topic:, processor:, preprocessor: FastlyNsq.preprocessor, channel: FastlyNsq.channel, consumer: nil,
                 logger: FastlyNsq.logger, priority: DEFAULT_PRIORITY, connect_timeout: DEFAULT_CONNECTION_TIMEOUT)

    raise ArgumentError, "processor #{processor.inspect} does not respond to #call" unless processor.respond_to?(:call)
    raise ArgumentError, "priority #{priority.inspect} must be a Integer" unless priority.is_a?(Integer)

    @channel      = channel
    @logger       = logger
    @preprocessor = preprocessor
    @processor    = processor
    @topic        = topic
    @priority     = priority

    @consumer = consumer || FastlyNsq::Consumer.new(topic: topic,
                                                    connect_timeout: connect_timeout,
                                                    channel: channel,
                                                    queue: FastlyNsq::Feeder.new(self, priority))

    FastlyNsq.manager.add_listener(self)
  end

  def call(nsq_message)
    message = FastlyNsq::Message.new(nsq_message)
    log message
    preprocessor&.call(message)
    result = processor.call(message)
    nsq_message.finish if result
  end

  def terminate
    return unless connected?
    consumer.terminate
    logger.info "< Consumer terminated: topic [#{topic}]"
  end

  private

  def log(message)
    logger&.info "[NSQ] Message received on topic [#{topic}]: #{message}"
  end
end
{'fraction_non_alphanumeric': 0.08843537414965986, 'fraction_numerical': 0.0011337868480725624, 'mean_word_length': 2.7383474576271185, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1044865', 'n_tokens_mistral': 969, 'n_tokens_neox': 936, 'n_words': 246}