<commit_before>require 'bnext_robot'
require 'json'

class RankList
  def []=(title, url)
    @rank ||= {}
    @rank[title] = url
  end

  def to_json
    @rank.map do |title, url|
      { 'title' => title, 'url' => url }
    end.to_json
  end
end

class RankFeeds
  attr_reader :type, :rank

  def initialize(type)
    @type = type
    _load_ranks
  end

  private

  def _load_ranks
    rank = RankList.new
    bnext_robot = BNextRobot.new
    case type
    when 'weekrank'
      bnext_robot.week_rank_feeds.each do |feed|
        rank[feed.title] = feed.link
      end
    when 'dayrank'
      bnext_robot.day_rank_feeds.each do |feed|
        rank[feed.title] = feed.link
      end
    when 'feed'
      puts 'Category list:'
      bnext_robot.cats.each do |title, link|
        puts "[#{title}]: #{link.split("/")[-1]}"
      end
      print 'Category: '
      cat = $stdin.readline.chomp
      print 'Page number: '
      page_no = $stdin.readline.chomp
      feed_found = bnext_robot.get_feeds(cat, page_no)
      if feed_found.length == 0
        puts 'Error: No result found. Check the input or internet connection.'
      else
        puts "#{feed_found}"
      end
    end
  end
end
<commit_msg>Make the attr @rank in RankList is the list of all feeds information in json format<commit_after>require 'bnext_robot'
require 'json'

class RankList

  def initialize(rank)
    @rank = rank
  end

  def [](idx)
    @rank[idx]
  end

  def to_json
    @rank.each do |feed_hash|
      keys = feed_hash.keys.map { |k| k.dup.force_encoding(Encoding::UTF_8) }
      values = feed_hash.values.map { |v| if v.class == String then v.force_encoding(Encoding::UTF_8) else v end }
      Hash[keys.zip(values)]
    end.to_json
  end
end

class RankFeeds
  attr_reader :type, :rank

  def initialize(type)
    @type = type
    _load_ranks
  end

  def self.fetch(type)
    RankFeeds.new(type).rank
  end

  private

  def _load_ranks
    bnext_robot = BNextRobot.new
    case type
    when 'weekrank'
      @rank = RankList.new(bnext_robot.week_rank_feeds.map(&:to_hash))
    when 'dayrank'
      @rank = RankList.new(bnext_robot.day_rank_feeds.map(&:to_hash))
    when 'feed'
      puts 'Category list:'
      bnext_robot.cats.each do |title, link|
        puts "[#{title}]: #{link.split("/")[-1]}"
      end
      print 'Category: '
      cat = $stdin.readline.chomp
      print 'Page number: '
      page_no = $stdin.readline.chomp
      feed_found = bnext_robot.get_feeds(cat, page_no)
      if feed_found.length == 0
        puts 'Error: No result found. Check the input or internet connection.'
      else
        @rank = RankList.new(feed_found.map(&:to_hash))
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.1118543292456336, 'fraction_numerical': 0.002229654403567447, 'mean_word_length': 2.537450722733246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3786306', 'n_tokens_mistral': 974, 'n_tokens_neox': 966, 'n_words': 238}