Module: TriviaCrack::Parsers::ProfileParser

Defined in:
lib/triviacrack/parsers/profile_parser.rb

Class Method Summary collapse

Class Method Details

.parse(raw_data) ⇒ Object

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/triviacrack/parsers/profile_parser.rb', line 23

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