Sha256: 0693eacd16e7faa801573ac04a035d2c7b9d2d9f60fa890da8056efaa71700ee

Contents?: true

Size: 1.98 KB

Versions: 8

Compression:

Stored size: 1.98 KB

Contents

require "triviacrack/profile"
require "triviacrack/parsers/category_statistics_parser"
require "triviacrack/parsers/time_parser"

# Internal: This module is used to parse data returned from the Trivia Crack API
# into a ruby object that represents a Trivia Crack user profile.
module TriviaCrack
  module Parsers
    module ProfileParser

      # Internal: Parses data returned from the Trivia Crack API to create a
      # TriviaCrack::Profile object.
      #
      # raw_data - A hash of the raw data returned by the Trivia Crack API.
      #
      # Examples
      #
      #   raw_data = get_raw_data_from_API
      #   ...
      #   profile = TriviaCrack::Parsers::ProfileParser.parse raw_data
      #
      # Returns a TriviaCrack::Profile.
      def self.parse(raw_data)
        stats = raw_data["statistics"]
        versus = raw_data["versus"]

        categories = CategoryStatisticsParser.parse stats["category_questions"]

        TriviaCrack::Profile.new(
          id: raw_data["id"],
          facebook_name: raw_data["facebook_name"],
          is_friend: raw_data["is_friend"],
          is_blocked: raw_data["is_blocked"],
          username: raw_data["username"],
          country: raw_data["country"].downcase.to_sym,
          email: raw_data["email"],
          last_play: TimeParser.parse(raw_data["last_play_date"]),
          last_login: TimeParser.parse(raw_data["last_log"]),
          games_won: stats["games_won"],
          games_lost: stats["games_lost"],
          games_resigned: stats["games_resigned"],
          consecutive_games_won: stats["consecutive_games_won"],
          consecutive_answers_correct: stats["consecutive_answers_correct"],
          level: raw_data["level_data"]["level"],
          challenges_won: stats["challenges"]["won"],
          challenges_lost: stats["challenges"]["lost"],
          categories: categories,
          my_wins_vs_user: versus ? versus["won"] : nil,
          my_losses_vs_user: versus ? versus["lost"] : nil
        )
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
triviacrack-0.6.0 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.5.1 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.5.0 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.4.0 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.3.2 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.3.0 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.2.0 lib/triviacrack/parsers/profile_parser.rb
triviacrack-0.1.0 lib/triviacrack/parsers/profile_parser.rb