<commit_before>def rescue_rate_limit
  yield
rescue Twitter::Error::TooManyRequests => error
  tries ||= 0
  tries += 1
  if tries < 6
    puts "[WARNING] Hit Twitter rate limit in lib/meta_twitter.rb, current retries: #{tries}/5"
    sleep error.rate_limit.reset_in + tries + 1
    retry
  else
    puts "[ERROR] Exhausted all rate limit retries in lib/meta_twitter.rb"
    raise
  end
end

def rescue_not_found
  yield
rescue Twitter::Error::NotFound
  nil
end


############################################


module MetaTwitter

  def MetaTwitter.strip_if_leading_character(string, character)
  # string -> string ('@cats' or '#cats' or 'cats')
  # character -> string ('#' or '@')
    string[0] == character ? string[1..string.length] : string
  end

  def MetaTwitter.get_account_id(user)
  # user => MetaTwitter::Auth.config
    user.access_token.split('-')[0]
  end

  def MetaTwitter.read_user_from_auth
    MetaTwitter.read_user_from_twitter_id(
      MetaTwitter.get_account_id(Twitter::REST::Client)
    )
  end

  def MetaTwitter.read_user_from_twitter_id(id)
  # id => int (1111111111111111)
    Rails.cache.fetch("/read_from_id/#{id}", expires_in: 1.hours) do
      rescue_not_found {
      rescue_rate_limit {
        TwitterClient.REST.user(id).to_h
      }}
    end
  end

  def MetaTwitter.read_user_from_twitter_name(name)
  # name => string ('@cyrin')
    name = MetaTwitter.strip_if_leading_character(name, '@')
    Rails.cache.fetch("/read_from_name/#{name}", expires_in: 1.hours) do
      rescue_not_found {
      rescue_rate_limit {
        TwitterClient.REST.user(name).to_h
      }}
    end
  end
<commit_msg>fix Twitter gem function issues<commit_after>def rescue_rate_limit
  yield
rescue Twitter::Error::TooManyRequests => error
  tries ||= 0
  tries += 1
  if tries < 6
    puts "[WARNING] Hit Twitter rate limit in lib/meta_twitter.rb, current retries: #{tries}/5"
    sleep error.rate_limit.reset_in + tries + 1
    retry
  else
    puts "[ERROR] Exhausted all rate limit retries in lib/meta_twitter.rb"
    raise
  end
end

def rescue_not_found
  yield
rescue Twitter::Error::NotFound
  nil
end


############################################


module MetaTwitter

  def MetaTwitter.strip_if_leading_character(string, character)
  # string -> string ('@cats' or '#cats' or 'cats')
  # character -> string ('#' or '@')
    string[0] == character ? string[1..string.length] : string
  end

  def MetaTwitter.get_account_id(user)
  # user => MetaTwitter::Auth.config
    user.access_token.split('-')[0]
  end

  def MetaTwitter.read_user_from_auth
    MetaTwitter.read_user_from_twitter_id(
      MetaTwitter.get_account_id(TwitterClient)
    )
  end

  def MetaTwitter.read_user_from_twitter_id(id)
  # id => int (1111111111111111)
    Rails.cache.fetch("/read_from_id/#{id}", expires_in: 1.hours) do
      rescue_not_found {
      rescue_rate_limit {
        TwitterClient.user(id.to_i).to_h
      }}
    end
  end

  def MetaTwitter.read_user_from_twitter_name(name)
  # name => string ('@cyrin')
    name = MetaTwitter.strip_if_leading_character(name, '@')
    Rails.cache.fetch("/read_from_name/#{name}", expires_in: 1.hours) do
      rescue_not_found {
      rescue_rate_limit {
        TwitterClient.user(name).to_h
      }}
    end
  end

{'fraction_non_alphanumeric': 0.1364885496183206, 'fraction_numerical': 0.01587786259541985, 'mean_word_length': 3.640226628895184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3470711', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1089, 'n_words': 224}