<commit_before>require "dexby/connection"
require "dexby/parse"

class Dexby::Reader
  def initialize(username, password, connection_class=Dexby::Connection, parser_class=Dexby::Parse)
    @connection_class = connection_class
    @parser_class = parser_class
    @username = username
    @password = password
    @session_id = nil
  end

  def connection
    @connection_class
  end

  def parser
    @parser_class
  end

  def read(minutes=1440, count=1)
    ensure_session_id
    result = session_connection_read(minutes, count)
    if result[1] != 200
      raise ::StandardError
    end
    parser.parse_all(result[0])
  end

  def session_connection_read(minutes, count)
    result = read_connection(minutes, count)
    if result[1] == 401 # expired session_id
      result = get_session_reread(minutes, count)
    end
    return result
  end

  def get_session_reread(minutes, count)
    get_session_id
    read_connection(minutes, count)
  end

  def read_connection(minutes, count)<commit_msg>Also retry if result is 500; which now happens on invalid session<commit_after>require "dexby/connection"
require "dexby/parse"

class Dexby::Reader
  def initialize(username, password, connection_class=Dexby::Connection, parser_class=Dexby::Parse)
    @connection_class = connection_class
    @parser_class = parser_class
    @username = username
    @password = password
    @session_id = nil
  end

  def connection
    @connection_class
  end

  def parser
    @parser_class
  end

  def read(minutes=1440, count=1)
    ensure_session_id
    result = session_connection_read(minutes, count)
    if result[1] != 200
      raise ::StandardError, result[1]
    end
    parser.parse_all(result[0])
  end

  def session_connection_read(minutes, count)
    result = read_connection(minutes, count)
    if result[1] == 401 || result[1] == 500 # expired session_id
      result = get_session_reread(minutes, count)
    end
    return result
  end

  def get_session_reread(minutes, count)
    get_session_id
    read_connection(minutes, count)
  end

  def read_connection(minutes, count)
{'fraction_non_alphanumeric': 0.08057553956834532, 'fraction_numerical': 0.017266187050359712, 'mean_word_length': 3.9314420803782504, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7418620', 'n_tokens_mistral': 694, 'n_tokens_neox': 633, 'n_words': 159}