<commit_before>require 'net/http'
require 'json'
require 'date'

SERVER_CONFIG       = 'server'
SECURE_TOKEN_CONFIG = 'token'

def weechat_init
  Weechat.register('gitterlog', '<NAME> <<EMAIL>>', '1.0', 'MIT', 'Loads the history for all your gitter chats', '', '')
  Weechat.hook_command('gitterlog', 'Loads Gitter history', '', 'No arguments', '', 'on_gitterlog', '')
  Weechat.hook_signal('*,irc_in2_join', 'on_join', '')

  Weechat.config_set_plugin(SERVER_CONFIG, 'gitter') unless Weechat.config_is_set_plugin(SERVER_CONFIG)
  Weechat.config_set_plugin(SECURE_TOKEN_CONFIG, '') unless Weechat.config_is_set_plugin(SECURE_TOKEN_CONFIG)

  Weechat::WEECHAT_RC_OK
end

def on_gitterlog(*)
  fetch_logs
end

def on_join(_, signal, data)
  joined_server, _ = signal.split(',')
  return Weechat::WEECHAT_RC_OK if joined_server != Weechat.config_get_plugin(SERVER_CONFIG)

  fetch_logs data.sub(/.*JOIN (.*)$/, '\1')
end

def fetch_logs(filter_channel=nil)
  server = Weechat.config_get_plugin(SERVER_CONFIG)
  secure_token = Weechat.config_get_plugin(SECURE_TOKEN_CONFIG)

  if server.empty? or secure_token.empty?
    Weechat.print '', "Missing #{SERVER_CONFIG} or #{SECURE_TOKEN_CONFIG} properties. Check gitterlog configurations."
    return Weechat::WEECHAT_RC_ERROR
  end

  GitterLog.new(server, filter_channel, secure_token).fetch
  Weechat::WEECHAT_RC_OK
end

module UrlFetcher
  private

  def fetch_url(address, secure_token)
    uri = URI.parse(address)
    response = perform_request(uri, http_get(uri, secure_token))

    yield JSON.parse(response.body) if response.is_a? Net::HTTPSuccess
  end

  def perform_request(uri, http_request)
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request http_request
    end
  end

  def http_get(uri, secure_token)
    Net::HTTP::Get.new(uri).tap do |request|
      request['Accept'] = 'application/json'
      request['Authorization'] = "Bearer #{secure_token}"
    end
  end
end

class GitterLog
  include UrlFetcher

  def initialize(server, filter_channel, secure_token)
    @server = server
    @filter_channel = filter_channel
    @secure_token = secure_token
  end

  def fetch
    fetch_url(Room.rooms_url, @secure_token) { |rooms_data| fetch_rooms rooms_data }
  end

  private

  def fetch_rooms(rooms_data)
    threads = rooms_data.map do |room_data|
      Thread.new do
        fetch_logs_for_room Room.parse(@server, room_data)
      end
    end

    threads.each(&:join)
  end

  def fetch_logs_for_room(room)
    return unless room.should_fetch? @filter_channel

    fetch_url(room.messages_url, @secure_token) do |messages_data|
      print room, Message.parse(messages_data)
    end
  end

  def print(room, messages)
    messages_to_show = messages.select(&:show?)
    return if messages_to_show.empty? or room.buffer.nil?

    messages_to_show.each do |message|
      Weechat.print room.buffer, message.text
    end
  end
end<commit_msg>Converting UrlFetcher to a class<commit_after>require 'net/http'
require 'json'
require 'date'

SERVER_CONFIG       = 'server'
SECURE_TOKEN_CONFIG = 'token'

def weechat_init
  Weechat.register('gitterlog', '<NAME> <<EMAIL>>', '1.0', 'MIT', 'Loads the history for all your gitter chats', '', '')
  Weechat.hook_command('gitterlog', 'Loads Gitter history', '', 'No arguments', '', 'on_gitterlog', '')
  Weechat.hook_signal('*,irc_in2_join', 'on_join', '')

  Weechat.config_set_plugin(SERVER_CONFIG, 'gitter') unless Weechat.config_is_set_plugin(SERVER_CONFIG)
  Weechat.config_set_plugin(SECURE_TOKEN_CONFIG, '') unless Weechat.config_is_set_plugin(SECURE_TOKEN_CONFIG)

  Weechat::WEECHAT_RC_OK
end

def on_gitterlog(*)
  fetch_logs
end

def on_join(_, signal, data)
  joined_server, _ = signal.split(',')
  return Weechat::WEECHAT_RC_OK if joined_server != Weechat.config_get_plugin(SERVER_CONFIG)

  fetch_logs data.sub(/.*JOIN (.*)$/, '\1')
end

def fetch_logs(filter_channel=nil)
  server = Weechat.config_get_plugin(SERVER_CONFIG)
  secure_token = Weechat.config_get_plugin(SECURE_TOKEN_CONFIG)

  if server.empty? or secure_token.empty?
    Weechat.print '', "Missing #{SERVER_CONFIG} or #{SECURE_TOKEN_CONFIG} properties. Check gitterlog configurations."
    return Weechat::WEECHAT_RC_ERROR
  end

  GitterLog.new(server, filter_channel, secure_token).fetch
  Weechat::WEECHAT_RC_OK
end

class GitterLog

  def initialize(server, filter_channel, secure_token)
    @server = server
    @filter_channel = filter_channel
    @fetcher = UrlFetcher.new(secure_token)
  end

  def fetch
    @fetcher.fetch_url(Room.rooms_url) { |rooms_data| fetch_rooms rooms_data }
  end

  private

  def fetch_rooms(rooms_data)
    threads = rooms_data.map do |room_data|
      Thread.new do
        fetch_logs_for_room Room.parse(@server, room_data)
      end
    end

    threads.each(&:join)
  end

  def fetch_logs_for_room(room)
    return unless room.should_fetch? @filter_channel

    @fetcher.fetch_url(room.messages_url) do |messages_data|
      print room, Message.parse(messages_data)
    end
  end

  def print(room, messages)
    messages_to_show = messages.select(&:show?)
    return if messages_to_show.empty? or room.buffer.nil?

    messages_to_show.each do |message|
      Weechat.print room.buffer, message.text
    end
  end
end

class UrlFetcher

  def initialize(secure_token)
    @secure_token = secure_token
  end

  def fetch_url(address)
    uri = URI.parse(address)
    response = perform_request(uri, http_get(uri))

    yield JSON.parse(response.body) if response.is_a? Net::HTTPSuccess
  end

  private

  def perform_request(uri, http_request)
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request http_request
    end
  end

  def http_get(uri)
    Net::HTTP::Get.new(uri).tap do |request|
      request['Accept'] = 'application/json'
      request['Authorization'] = "Bearer #{@secure_token}"
    end
  end
end
{'fraction_non_alphanumeric': 0.10990685859441152, 'fraction_numerical': 0.001354784081287045, 'mean_word_length': 4.504193849021435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '451070', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2058, 'n_words': 415}