<commit_before>require_relative '../badge'

class PopularQuestion < Badge
  series :popular_question

  def progress_title
    "You're getting close to..."
  end

  def progress_description
    questions = service.fetch('users', 'questions', {
      ids: user_id,
      sort: 'votes',
      order: 'desc',
      fetch_all_pages: true
    })

    if questions.length > 0
      highest_views_question = questions.first

      # Get badge with highest view_count
      if questions.length > 1
        highest_views_question = questions.reduce do |highest_views, question|
          question['view_count'] > highest_views['view_count'] ? question : highest_views
        end
      end

      title = highest_views_question['title']
      views = highest_views_question['view_count']
      remaining = required_views - views

      views_str = "#{views.with_commas} " + "view".pluralize(views, "views")
      remaining_str = "#{remaining.with_commas} " + "view".pluralize(remaining, "views")

      "Your question \"#{title.truncate(70)}\" has #{views_str}. #{remaining_str} to go!"
    else
      "Asked a question with #{required_views.with_commas} views. You have not asked any questions yet!"
    end
  end

  def required_views
    1000
  end

  def service
    @service ||= StackExchangeService.new('stackoverflow')
  end
end

class NotableQuestion < PopularQuestion
  def required_views
    2500
  end
end

class FamousQuestion < PopularQuestion
  def required_views
    10000
  end
end
<commit_msg>Refactor PopularQuestion's progress_description method<commit_after>require_relative '../badge'

class PopularQuestion < Badge
  series :popular_question

  def progress_title
    "You're getting close to..."
  end

  def progress_description
    questions = service.fetch('users', 'questions', {
      ids: user_id,
      sort: 'votes',
      order: 'desc',
      fetch_all_pages: true
    })

    # Get badge with highest view_count
    highest_views_question = questions.reduce do |highest_views, question|
      question['view_count'] > highest_views['view_count'] ? question : highest_views
    end

    if highest_views_question
      title = highest_views_question['title']
      views = highest_views_question['view_count']
      remaining = required_views - views

      views_str = "#{views.with_commas} " + "view".pluralize(views, "views")
      remaining_str = "#{remaining.with_commas} " + "view".pluralize(remaining, "views")

      "Your question \"#{title.truncate(70)}\" has #{views_str}. #{remaining_str} to go!"
    else
      "Asked a question with #{required_views.with_commas} views. You have not asked any questions yet!"
    end
  end

  def required_views
    1000
  end

  def service
    @service ||= StackExchangeService.new('stackoverflow')
  end
end

class NotableQuestion < PopularQuestion
  def required_views
    2500
  end
end

class FamousQuestion < PopularQuestion
  def required_views
    10000
  end
end

{'fraction_non_alphanumeric': 0.09683995922528033, 'fraction_numerical': 0.010873258579680599, 'mean_word_length': 3.3679525222551927, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '926786', 'n_tokens_mistral': 913, 'n_tokens_neox': 856, 'n_words': 228}