<commit_before>class UsernameFetcher
  NoUserInSlack = Class.new(StandardError)

  def call(user_tag)
    user_tag = parse_user_tag(user_tag)
    slack_user_info(user_tag)
  rescue NoUserInSlack
    username_from_tag(user_tag)
  end

  private
  def parse_user_tag(user_tag)
    user_tag[2..-2]
  end

  def slack_user_info(user_tag)
    json = call_slack(user_tag)
    raise NoUserInSlack unless json["ok"]
    json["user"]["name"]
  end

  def call_slack(user_tag)
    uri = build_uri(user_tag)
    response = Net::HTTP.get(uri)
    JSON.parse(response)
  end

  def build_uri(user_tag)
    uri = URI('https://slack.com/api/users.info')
    params = {
        token: 'xoxp-2181345045-2321579527-3389973901-7ff25a',
        user: user_tag
    }
    uri.query = URI.encode_www_form(params)
    uri
  end

  def username_from_tag(user_tag)
    user_tag.slice(0).downcase
  end
end<commit_msg>I didn't took into consideration text that isn't encoded by slack as a nickname.<commit_after>class UsernameFetcher
  NoUserInSlack = Class.new(StandardError)

  def call(user_tag)
    if slack_username?(user_tag)
      fetch_slack_username(user_tag)
    else
      user_tag
    end
  rescue NoUserInSlack
    slack_bot_username(user_tag)
  end

  private
  def slack_username?(user_tag)
    user_tag.start_with?("<@")
  end

  def fetch_slack_username(user_tag)
    user_tag = strip_slack_user_tag(user_tag)
    json = call_slack(user_tag)
    raise NoUserInSlack unless json["ok"]
    json["user"]["name"]
  end

  def call_slack(user_tag)
    uri = build_uri(user_tag)
    response = Net::HTTP.get(uri)
    JSON.parse(response)
  end

  def build_uri(user_tag)
    uri = URI('https://slack.com/api/users.info')
    params = {
        token: '<PASSWORD>',
        user: user_tag
    }
    uri.query = URI.encode_www_form(params)
    uri
  end

  def strip_slack_user_tag(user_tag)
    user_tag[2..-2]
  end

  def slack_bot_username(user_tag)
    strip_slack_user_tag(user_tag).slice(0).downcase
  end
end
{'fraction_non_alphanumeric': 0.0995995995995996, 'fraction_numerical': 0.01951951951951952, 'mean_word_length': 3.4620535714285716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5261252', 'n_tokens_mistral': 785, 'n_tokens_neox': 755, 'n_words': 129}