Sha256: 4f900d2c791fac254588153afc8b81b7bb066f31e4bb3140979f2f341ba0364b

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'triviacrack/question'

# Internal: This module is used to parse data returned from the Trivia Crack API
# into a ruby object that represents a Trivia Crack question.
module TriviaCrack
  module Parsers
    module QuestionParser
      # Internal: Parses data returned from the Trivia Crack API to create a
      # TriviaCrack::Question object.
      #
      # raw_data  - A hash of the raw data returned by the Trivia Crack API.
      #
      # Examples
      #
      #   raw_data = get_raw_data_from_API
      #   ...
      #   question = TriviaCrack::Parsers::QuestionParser.parse raw_data
      #
      # Returns a TriviaCrack::Question.
      def self.parse(raw_data)
        TriviaCrack::Question.new(
          id: raw_data['id'],
          type: raw_data['type'].downcase.to_sym,
          category: raw_data['category'].downcase.to_sym,
          text: raw_data['text'],
          answers: raw_data['answers'],
          correct_answer: raw_data['correct_answer'],
          media_type: raw_data['media_type'].downcase.to_sym,
          image_url: raw_data['base_url']
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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