<commit_before>module Ruboty
  module Handlers
    class TwitterTrack < Base
      on(/twitter track by (?<term>.+)\z/,
        name: 'track',
        description: 'Track the twitter stream by the certain term.')

      on(/twitter untrack by (?<term>.+)\z/,
        name: 'untrack',
        description: 'Untrack the twitter stream by the certain term.')

      on(/twitter tracking\z/,
        name: 'tracking',
        description: 'List tracking terms.')

      def initialize(*args)
        super

        @stream = Ruboty::TwitterTrack::Stream.new(robot)
        @stream.start(cache[:message], cache[:terms])
      end

      def track(message)
        cache[:message] = message.original.except(:robot)

        message[:term].split(',').each do |term|
          words = term.strip.split(/\s+/).sort
          cache[:terms].push(words).uniq!
        end

        begin
          @stream.restart(cache[:message], cache[:terms])
          message.reply("Done.")
        rescue Twitter::Error::Forbidden
          message.reply("Unable to verify your credentials.")
        end
      end

      def untrack(message)
        cache[:message] = message.original.except(:robot)

        message[:term].split(',').each do |term|
          words = term.strip.split(/\s+/).sort
          cache[:terms].delete(words)
        end

        begin
          @stream.restart(cache[:message], cache[:terms])
          message.reply("Done.")
        rescue Twitter::Error::Forbidden
          message.reply("Unable to verify your credentials.")
        end
      end

      def tracking(message)
        if cache[:terms].empty?
          message.reply("Tracking no terms.")
        else
          cache[:terms].each { |words| message.reply(words.join(' '), code:true) }
        end
      end

      def cache
        unless robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
          status = { message: nil, terms: [] }
          robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] = status
        end

        robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
      end
    end
  end
end<commit_msg>Fix to put the tracking response in order<commit_after>module Ruboty
  module Handlers
    class TwitterTrack < Base
      on(/twitter track by (?<term>.+)\z/,
        name: 'track',
        description: 'Track the twitter stream by the certain term.')

      on(/twitter untrack by (?<term>.+)\z/,
        name: 'untrack',
        description: 'Untrack the twitter stream by the certain term.')

      on(/twitter tracking\z/,
        name: 'tracking',
        description: 'List tracking terms.')

      def initialize(*args)
        super

        @stream = Ruboty::TwitterTrack::Stream.new(robot)
        @stream.start(cache[:message], cache[:terms])
      end

      def track(message)
        cache[:message] = message.original.except(:robot)

        message[:term].split(',').each do |term|
          words = term.strip.split(/\s+/).sort
          cache[:terms].push(words).uniq!
        end

        begin
          @stream.restart(cache[:message], cache[:terms])
          message.reply("Done.")
        rescue Twitter::Error::Forbidden
          message.reply("Unable to verify your credentials.")
        end
      end

      def untrack(message)
        cache[:message] = message.original.except(:robot)

        message[:term].split(',').each do |term|
          words = term.strip.split(/\s+/).sort
          cache[:terms].delete(words)
        end

        begin
          @stream.restart(cache[:message], cache[:terms])
          message.reply("Done.")
        rescue Twitter::Error::Forbidden
          message.reply("Unable to verify your credentials.")
        end
      end

      def tracking(message)
        if cache[:terms].empty?
          message.reply("Tracking no terms.")
        else
          response = cache[:terms].map { |words| message.reply(words.join(' ') }.join("\n")
          message.reply(response, code:true)
        end
      end

      def cache
        unless robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
          status = { message: nil, terms: [] }
          robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] = status
        end

        robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13410051667449507, 'fraction_numerical': 0.0, 'mean_word_length': 2.150147928994083, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7469755', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1179, 'n_words': 271}