<commit_before>require 'em-http'
require 'json'
require 'net/http'

##
# Lita module.
#
module Lita
  ##
  # Lita adapters module.
  #
  module Adapters
    ##
    # Gitter adapter for the Lita chat bot.
    #
    class Gitter < Adapter
      config :token,   type: String, required: true
      config :room_id, type: String, required: true

      # The main loop. Listens for incoming messages,
      # creates {Lita::Message} objects from them,
      # and dispatches them to the robot.
      #
      def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
        stream_url =
          "https://stream.gitter.im/v1/rooms/#{config.room_id}/chatMessages"

        http = EventMachine::HttpRequest.new(
          stream_url,
          keepalive: true,
          connect_timeout: 0,
          inactivity_timeout: 0,
        )

        EventMachine.run do
          request = http.get(
            head: {
              'Accept' => 'application/json',
              'Authorization' => "Bearer #{config.token}",
            }
          )

          buffer = ''

          request.stream do |chunk|
            body = buffer + chunk

            unless body.strip.empty?
              begin
                response = JSON.parse(body)
                body = ''

                text = response['text']
                from_id = response['fromUser']['id']
                room_id = config.room_id

                get_message(text, from_id, room_id)

              rescue JSON::ParserError
                buffer = body
              end<commit_msg>Improved parser. Wait for proper chunk separator.<commit_after>require 'em-http'
require 'net/http'

##
# Lita module.
#
module Lita
  ##
  # Lita adapters module.
  #
  module Adapters
    ##
    # Gitter adapter for the Lita chat bot.
    #
    class Gitter < Adapter
      config :token,   type: String, required: true
      config :room_id, type: String, required: true

      # The main loop. Listens for incoming messages,
      # creates {Lita::Message} objects from them,
      # and dispatches them to the robot.
      #
      def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
        stream_url = "https://stream.gitter.im/v1/rooms/#{config.room_id}/chatMessages"

        http = EventMachine::HttpRequest.new(
          stream_url,
          keepalive: true,
          connect_timeout: 0,
          inactivity_timeout: 0,
        )

        EventMachine.run do
          log.debug("Connecting to Gitter Stream API (room: #{config.room_id}).")
          request = http.get(
            head: {
              'Accept' => 'application/json',
              'Authorization' => "Bearer #{config.token}",
            }
          )

          buffer = ''

          request.stream do |chunk|
            body = buffer + chunk

            if body.strip.empty?
              # Keep alive packet, ignore!
              next
            end

            if body.end_with?("}\n")
              # End of chunk, let's process it!
              begin
                response = MultiJson.load(body)
                buffer = ''
                body = ''

                text = response['text']
                from_id = response['fromUser']['id']
                room_id = config.room_id

                get_message(text, from_id, room_id)

              rescue MultiJson::ParseError => e
                log.error "Failed to decode JSON: #{e}"
              end
            else
              # Chunk too big, buffering!
              buffer = body
{'fraction_non_alphanumeric': 0.0982066609735269, 'fraction_numerical': 0.0017079419299743809, 'mean_word_length': 1.5537790697674418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '745617', 'n_tokens_mistral': 994, 'n_tokens_neox': 948, 'n_words': 253}