Sha256: b98f88a5b87c7d9b539248aae74f6c6e2379b40f756fb4fa7258f051f11b7338

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'triviacrack/category_statistics'

# Internal: This module is used to parse data returned from the Trivia Crack API
# into a ruby object that represents category statistics for a Trivia Crack
# game.
module TriviaCrack
  module Parsers
    module CategoryStatisticsParser
      # Internal: Parses data returned from the Trivia Crack API to create
      # TriviaCrack::CategoryStatistics objects.
      #
      # raw_data - A hash of the raw data returned by the Trivia Crack API.
      #
      # Examples
      #
      #   raw_data = get_raw_data_from_API
      #   ...
      #   stats = TriviaCrack::Parsers::CategoryStatisticsParser.parse raw_data
      #
      # Returns a hash of TriviaCrack::CategoryStatistics objects, keyed by
      # category.
      def self.parse(raw_data) # rubocop:disable Metrics/MethodLength
        categories = {}

        raw_data&.each do |category|
          category_name = category['category'].downcase.to_sym

          categories[category_name] =
            TriviaCrack::CategoryStatistics.new(
              category: category_name,
              correct: category['correct'],
              incorrect: category['incorrect'],
              worst: category['worst']
            )
        end

        categories
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
triviacrack-0.8.0 lib/triviacrack/parsers/category_statistics_parser.rb
triviacrack-0.7.0 lib/triviacrack/parsers/category_statistics_parser.rb