triviacrack 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +19 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +7 -0
- data/lib/triviacrack.rb +12 -0
- data/lib/triviacrack/api/client.rb +31 -0
- data/lib/triviacrack/api/common.rb +76 -0
- data/lib/triviacrack/api/game.rb +72 -0
- data/lib/triviacrack/api/login.rb +35 -0
- data/lib/triviacrack/api/profile.rb +44 -0
- data/lib/triviacrack/api/question.rb +43 -0
- data/lib/triviacrack/api/user.rb +56 -0
- data/lib/triviacrack/category_statistics.rb +27 -0
- data/lib/triviacrack/errors/parse_error.rb +7 -0
- data/lib/triviacrack/errors/request_error.rb +18 -0
- data/lib/triviacrack/game.rb +100 -0
- data/lib/triviacrack/game_statistics.rb +35 -0
- data/lib/triviacrack/parsers/category_statistics_parser.rb +45 -0
- data/lib/triviacrack/parsers/game_parser.rb +74 -0
- data/lib/triviacrack/parsers/game_statistics_parser.rb +44 -0
- data/lib/triviacrack/parsers/profile_parser.rb +55 -0
- data/lib/triviacrack/parsers/question_parser.rb +36 -0
- data/lib/triviacrack/parsers/session_parser.rb +36 -0
- data/lib/triviacrack/parsers/time_parser.rb +26 -0
- data/lib/triviacrack/parsers/user_parser.rb +47 -0
- data/lib/triviacrack/profile.rb +96 -0
- data/lib/triviacrack/question.rb +43 -0
- data/lib/triviacrack/session.rb +30 -0
- data/lib/triviacrack/user.rb +91 -0
- data/lib/triviacrack/version.rb +4 -0
- data/spec/api/game_spec.rb +72 -0
- data/spec/api/login_spec.rb +34 -0
- data/spec/api/profile_spec.rb +51 -0
- data/spec/api/question_spec.rb +45 -0
- data/spec/api/user_spec.rb +50 -0
- data/spec/data/answer.json +161 -0
- data/spec/data/dashboard.json +949 -0
- data/spec/data/game.json +147 -0
- data/spec/data/login.json +25 -0
- data/spec/data/my_profile.json +556 -0
- data/spec/data/new_game.json +363 -0
- data/spec/data/profile.json +553 -0
- data/spec/data/question.json +14 -0
- data/spec/data/question_image.json +15 -0
- data/spec/data/search.json +123 -0
- data/spec/data/user.json +25 -0
- data/spec/game_spec.rb +60 -0
- data/spec/parsers/category_statistics_parser_spec.rb +41 -0
- data/spec/parsers/game_parser_spec.rb +114 -0
- data/spec/parsers/game_statistics_parser_spec.rb +56 -0
- data/spec/parsers/profile_parser_spec.rb +59 -0
- data/spec/parsers/question_parser_spec.rb +34 -0
- data/spec/parsers/session_parser_spec.rb +20 -0
- data/spec/parsers/time_parser_spec.rb +19 -0
- data/spec/parsers/user_parser_spec.rb +51 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/user_spec.rb +33 -0
- data/triviacrack.gemspec +31 -0
- metadata +258 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
require "triviacrack/profile"
|
2
|
+
require "triviacrack/parsers/category_statistics_parser"
|
3
|
+
require "triviacrack/parsers/time_parser"
|
4
|
+
|
5
|
+
# Internal: This module is used to parse data returned from the Trivia Crack API
|
6
|
+
# into a ruby object that represents a Trivia Crack user profile.
|
7
|
+
module TriviaCrack
|
8
|
+
module Parsers
|
9
|
+
module ProfileParser
|
10
|
+
|
11
|
+
# Internal: Parses data returned from the Trivia Crack API to create a
|
12
|
+
# TriviaCrack::Profile object.
|
13
|
+
#
|
14
|
+
# raw_data - A hash of the raw data returned by the Trivia Crack API.
|
15
|
+
#
|
16
|
+
# Examples
|
17
|
+
#
|
18
|
+
# raw_data = get_raw_data_from_API
|
19
|
+
# ...
|
20
|
+
# profile = TriviaCrack::Parsers::ProfileParser.parse raw_data
|
21
|
+
#
|
22
|
+
# Returns a TriviaCrack::Profile.
|
23
|
+
def self.parse(raw_data)
|
24
|
+
stats = raw_data["statistics"]
|
25
|
+
versus = raw_data["versus"]
|
26
|
+
|
27
|
+
categories = CategoryStatisticsParser.parse stats["category_questions"]
|
28
|
+
|
29
|
+
TriviaCrack::Profile.new(
|
30
|
+
id: raw_data["id"],
|
31
|
+
facebook_name: raw_data["facebook_name"],
|
32
|
+
is_friend: raw_data["is_friend"],
|
33
|
+
is_blocked: raw_data["is_blocked"],
|
34
|
+
username: raw_data["username"],
|
35
|
+
country: raw_data["country"].downcase.to_sym,
|
36
|
+
email: raw_data["email"],
|
37
|
+
last_play: TimeParser.parse(raw_data["last_play_date"]),
|
38
|
+
last_login: TimeParser.parse(raw_data["last_log"]),
|
39
|
+
games_won: stats["games_won"],
|
40
|
+
games_lost: stats["games_lost"],
|
41
|
+
games_resigned: stats["games_resigned"],
|
42
|
+
consecutive_games_won: stats["consecutive_games_won"],
|
43
|
+
consecutive_answers_correct: stats["consecutive_answers_correct"],
|
44
|
+
level: raw_data["level_data"]["level"],
|
45
|
+
challenges_won: stats["challenges"]["won"],
|
46
|
+
challenges_lost: stats["challenges"]["lost"],
|
47
|
+
categories: categories,
|
48
|
+
my_wins_vs_user: versus ? versus["won"] : nil,
|
49
|
+
my_losses_vs_user: versus ? versus["lost"] : nil
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "triviacrack/question"
|
2
|
+
|
3
|
+
# Internal: This module is used to parse data returned from the Trivia Crack API
|
4
|
+
# into a ruby object that represents a Trivia Crack question.
|
5
|
+
module TriviaCrack
|
6
|
+
module Parsers
|
7
|
+
module QuestionParser
|
8
|
+
|
9
|
+
# Internal: Parses data returned from the Trivia Crack API to create a
|
10
|
+
# TriviaCrack::Question object.
|
11
|
+
#
|
12
|
+
# raw_data - A hash of the raw data returned by the Trivia Crack API.
|
13
|
+
#
|
14
|
+
# Examples
|
15
|
+
#
|
16
|
+
# raw_data = get_raw_data_from_API
|
17
|
+
# ...
|
18
|
+
# question = TriviaCrack::Parsers::QuestionParser.parse raw_data
|
19
|
+
#
|
20
|
+
# Returns a TriviaCrack::Question.
|
21
|
+
def self.parse(raw_data)
|
22
|
+
TriviaCrack::Question.new(
|
23
|
+
id: raw_data["id"],
|
24
|
+
type: raw_data["type"].downcase.to_sym,
|
25
|
+
category: raw_data["category"].downcase.to_sym,
|
26
|
+
text: raw_data["text"],
|
27
|
+
answers: raw_data["answers"],
|
28
|
+
correct_answer: raw_data["correct_answer"],
|
29
|
+
media_type: raw_data["media_type"].downcase.to_sym,
|
30
|
+
image_url: raw_data["base_url"]
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "triviacrack/session"
|
2
|
+
require "triviacrack/parsers/time_parser"
|
3
|
+
|
4
|
+
# Internal: This module is used to parse data returned from the Trivia Crack API
|
5
|
+
# into a ruby object that represents a Trivia Crack session.
|
6
|
+
module TriviaCrack
|
7
|
+
module Parsers
|
8
|
+
module SessionParser
|
9
|
+
|
10
|
+
# Internal: Parses data returned from the Trivia Crack API to create a
|
11
|
+
# TriviaCrack::Session object.
|
12
|
+
#
|
13
|
+
# raw_data - A hash of the raw data returned by the Trivia Crack API.
|
14
|
+
#
|
15
|
+
# Examples
|
16
|
+
#
|
17
|
+
# raw_data = get_raw_data_from_API
|
18
|
+
# ...
|
19
|
+
# session = TriviaCrack::Parsers::SessionParser.parse raw_data
|
20
|
+
#
|
21
|
+
# Returns a TriviaCrack::Session.
|
22
|
+
def self.parse(raw_data)
|
23
|
+
expiration = TimeParser.parse raw_data["session"]["expirationTime"]
|
24
|
+
|
25
|
+
TriviaCrack::Session.new(
|
26
|
+
session_id: raw_data["session"]["session"],
|
27
|
+
user_id: raw_data["id"],
|
28
|
+
username: raw_data["username"],
|
29
|
+
device_key: raw_data["session"]["deviceKey"],
|
30
|
+
expiration: expiration
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "time"
|
2
|
+
|
3
|
+
# Internal: This module is used to parse date_time strings returned by the
|
4
|
+
# Trivia Crack API.
|
5
|
+
module TriviaCrack
|
6
|
+
module Parsers
|
7
|
+
module TimeParser
|
8
|
+
|
9
|
+
TIME_FORMAT = "%m/%d/%Y %H:%M:%S"
|
10
|
+
|
11
|
+
# Internal: Parses a date_time string returned by the Trivia Crack API.
|
12
|
+
#
|
13
|
+
# raw_data - The date_time string.
|
14
|
+
#
|
15
|
+
# Examples
|
16
|
+
#
|
17
|
+
# TriviaCrack::Parsers::TimeParser.parse "02/17/2015 21:47:19 EST"
|
18
|
+
#
|
19
|
+
# Returns a Time object representation of the given string.
|
20
|
+
def self.parse(raw_data)
|
21
|
+
Time.strptime(raw_data, TIME_FORMAT)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "triviacrack/user"
|
2
|
+
|
3
|
+
# Internal: This module is used to parse data returned from the Trivia Crack API
|
4
|
+
# into a ruby object that represents a Trivia Crack user.
|
5
|
+
module TriviaCrack
|
6
|
+
module Parsers
|
7
|
+
module UserParser
|
8
|
+
|
9
|
+
# Internal: Parses data returned from the Trivia Crack API to create a
|
10
|
+
# TriviaCrack::User object.
|
11
|
+
#
|
12
|
+
# raw_data - A hash of the raw data returned by the Trivia Crack API.
|
13
|
+
#
|
14
|
+
# Examples
|
15
|
+
#
|
16
|
+
# raw_data = get_raw_data_from_API
|
17
|
+
# ...
|
18
|
+
# user = TriviaCrack::Parsers::UserParser.parse raw_data
|
19
|
+
#
|
20
|
+
# Returns a TriviaCrack::User.
|
21
|
+
def self.parse(raw_data)
|
22
|
+
lives = raw_data["lives"]
|
23
|
+
level_data = raw_data["level_data"]
|
24
|
+
country = raw_data["country"]
|
25
|
+
|
26
|
+
TriviaCrack::User.new(
|
27
|
+
id: raw_data["id"],
|
28
|
+
username: raw_data["username"],
|
29
|
+
facebook_id: raw_data["facebook_id"],
|
30
|
+
facebook_name: raw_data["facebook_name"],
|
31
|
+
coins: raw_data["coins"],
|
32
|
+
lives: lives ? lives["quantity"] : nil,
|
33
|
+
max_lives: lives ? lives["max"] : nil,
|
34
|
+
unlimited_lives: lives ? lives["unlimited"] : nil,
|
35
|
+
country: country ? country.downcase.to_sym : nil,
|
36
|
+
extra_shots: raw_data["extra_shots"],
|
37
|
+
level: level_data ? level_data["level"] : nil,
|
38
|
+
level_points: level_data ? level_data["points"] : nil,
|
39
|
+
level_progress: level_data ? level_data["progress"] : nil,
|
40
|
+
goal_points: level_data ? level_data["goal_points"] : nil,
|
41
|
+
level_up: level_data ? level_data["level_up"] : nil
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# Public: An object containing the Trivia Crack profile information of a user.
|
2
|
+
module TriviaCrack
|
3
|
+
class Profile
|
4
|
+
|
5
|
+
# Public: The unique identifier of the user.
|
6
|
+
attr_reader :id
|
7
|
+
|
8
|
+
# Public: The user's Facebook name.
|
9
|
+
attr_reader :facebook_name
|
10
|
+
|
11
|
+
# Public: Boolean indicating whether the user is your friend.
|
12
|
+
attr_reader :is_friend
|
13
|
+
|
14
|
+
# Public: Boolean indicating whether you have blocked the user.
|
15
|
+
attr_reader :is_blocked
|
16
|
+
|
17
|
+
# Public: The user's username.
|
18
|
+
attr_reader :username
|
19
|
+
|
20
|
+
# Public: The user's country (e.g. :ca, :us).
|
21
|
+
attr_reader :country
|
22
|
+
|
23
|
+
# Public: The user's email address.
|
24
|
+
attr_reader :email
|
25
|
+
|
26
|
+
# Public: The datetime of the user's last play.
|
27
|
+
attr_reader :last_play
|
28
|
+
|
29
|
+
# Public: The datetime of the user's last login.
|
30
|
+
attr_reader :last_login
|
31
|
+
|
32
|
+
# Public: Number of games won by the user.
|
33
|
+
attr_reader :games_won
|
34
|
+
|
35
|
+
# Public: Number of games lost by the user.
|
36
|
+
attr_reader :games_lost
|
37
|
+
|
38
|
+
# Public: Number of games resigned by the user.
|
39
|
+
attr_reader :games_resigned
|
40
|
+
|
41
|
+
# Public: Number of consecutive games won by the user.
|
42
|
+
attr_reader :consecutive_games_won
|
43
|
+
|
44
|
+
# Public: Number of consecutive correctly answered questions by the user.
|
45
|
+
attr_reader :consecutive_answers_correct
|
46
|
+
|
47
|
+
# Public: The user's level.
|
48
|
+
attr_reader :level
|
49
|
+
|
50
|
+
# Public: Number of challenges won by the user.
|
51
|
+
attr_reader :challenges_won
|
52
|
+
|
53
|
+
# Public: Number of challenges lost by the user.
|
54
|
+
attr_reader :challenges_lost
|
55
|
+
|
56
|
+
# Public: A hash of TriviaCrack::CategoryStatistics, containing the user's
|
57
|
+
# statistics for individual categories.
|
58
|
+
attr_reader :categories
|
59
|
+
|
60
|
+
# Public: Number of games won against the requesting user.
|
61
|
+
attr_reader :my_wins_vs_user
|
62
|
+
|
63
|
+
# Public: Number of games lost against the requesting user.
|
64
|
+
attr_reader :my_losses_vs_user
|
65
|
+
|
66
|
+
def initialize(id: nil, facebook_name: nil, is_friend: nil, is_blocked: nil,
|
67
|
+
username: nil, country: nil, email: nil, last_play: nil,
|
68
|
+
last_login: nil, games_won: nil, games_lost: nil,
|
69
|
+
games_resigned: nil, consecutive_games_won: nil,
|
70
|
+
consecutive_answers_correct: nil, level: nil,
|
71
|
+
challenges_won: nil, challenges_lost: nil, categories: nil,
|
72
|
+
my_wins_vs_user: nil, my_losses_vs_user: nil)
|
73
|
+
@id = id
|
74
|
+
@facebook_name = facebook_name
|
75
|
+
@is_friend = is_friend
|
76
|
+
@is_blocked = is_blocked
|
77
|
+
@username = username
|
78
|
+
@country = country
|
79
|
+
@email = email
|
80
|
+
@last_play = last_play
|
81
|
+
@last_login = last_login
|
82
|
+
@games_won = games_won
|
83
|
+
@games_lost = games_lost
|
84
|
+
@games_resigned = games_resigned
|
85
|
+
@consecutive_games_won = consecutive_games_won
|
86
|
+
@consecutive_answers_correct = consecutive_answers_correct
|
87
|
+
@level = level
|
88
|
+
@challenges_won = challenges_won
|
89
|
+
@challenges_lost = challenges_lost
|
90
|
+
@categories = categories
|
91
|
+
@my_wins_vs_user = my_wins_vs_user
|
92
|
+
@my_losses_vs_user = my_losses_vs_user
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Public: An object representing a Trivia Crack Question, including the list of
|
2
|
+
# available answers as well as the correct answer.
|
3
|
+
module TriviaCrack
|
4
|
+
class Question
|
5
|
+
|
6
|
+
# Public: The unique identifier of the question.
|
7
|
+
attr_reader :id
|
8
|
+
|
9
|
+
# Public: The type of the question (e.g. :normal, :crown, :duel).
|
10
|
+
attr_reader :type
|
11
|
+
|
12
|
+
# Public: The category of the question (e.g. :arts, :geography, etc).
|
13
|
+
attr_reader :category
|
14
|
+
|
15
|
+
# Public: The question text.
|
16
|
+
attr_reader :text
|
17
|
+
|
18
|
+
# Public: The list of possible answers to the question.
|
19
|
+
attr_reader :answers
|
20
|
+
|
21
|
+
# Public: The index of the correct answer (based on the :answers property).
|
22
|
+
attr_reader :correct_answer
|
23
|
+
|
24
|
+
# Public: The type of media used by the question (e.g. :normal, :image).
|
25
|
+
attr_reader :media_type
|
26
|
+
|
27
|
+
# Public: The URL for the image.
|
28
|
+
attr_reader :image_url
|
29
|
+
|
30
|
+
def initialize(id:, type: nil, category: nil, text: nil, answers: nil,
|
31
|
+
correct_answer: nil, media_type: nil, image_url: nil)
|
32
|
+
@id = id
|
33
|
+
@type = type
|
34
|
+
@category = category
|
35
|
+
@text = text
|
36
|
+
@answers = answers
|
37
|
+
@correct_answer = correct_answer
|
38
|
+
@media_type = media_type
|
39
|
+
@image_url = image_url
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Public: An object representing a Trivia Crack Session.
|
2
|
+
module TriviaCrack
|
3
|
+
class Session
|
4
|
+
|
5
|
+
# Public: The session id.
|
6
|
+
attr_reader :session_id
|
7
|
+
|
8
|
+
# Public: The id of the User associated with this session.
|
9
|
+
attr_reader :user_id
|
10
|
+
|
11
|
+
# Public: The username of the User associated with this session.
|
12
|
+
attr_reader :username
|
13
|
+
|
14
|
+
# Public: The device key.
|
15
|
+
attr_reader :device_key
|
16
|
+
|
17
|
+
# Public: The time at which the session will expire.
|
18
|
+
attr_reader :expiration
|
19
|
+
|
20
|
+
def initialize(session_id:, user_id: nil, username: nil, device_key: nil,
|
21
|
+
expiration: nil)
|
22
|
+
@session_id = session_id
|
23
|
+
@user_id = user_id
|
24
|
+
@username = username
|
25
|
+
@device_key = device_key
|
26
|
+
@expiration = expiration
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# Public: An object representing a Trivia Crack User, including information
|
2
|
+
# about the user's level, lives and coins.
|
3
|
+
module TriviaCrack
|
4
|
+
class User
|
5
|
+
|
6
|
+
# Public: The unique identifier of the user.
|
7
|
+
attr_reader :id
|
8
|
+
|
9
|
+
# Public: The unique username of the user.
|
10
|
+
attr_reader :username
|
11
|
+
|
12
|
+
# Public: The Facebook ID of the user (nil if the user has not linked their
|
13
|
+
# Facebook account).
|
14
|
+
attr_reader :facebook_id
|
15
|
+
|
16
|
+
# Public: The Facebook name of the user (nil if the user has not linked
|
17
|
+
# their Facebook account).
|
18
|
+
attr_reader :facebook_name
|
19
|
+
|
20
|
+
# Public: The amount of coins that the user has accumulated.
|
21
|
+
attr_reader :coins
|
22
|
+
|
23
|
+
# Public: The amount of lives available to the user.
|
24
|
+
attr_reader :lives
|
25
|
+
|
26
|
+
# Public: The maximum number of lives that the user can have.
|
27
|
+
attr_reader :max_lives
|
28
|
+
|
29
|
+
# Public: Boolean indicating whether the user has unlimited lives.
|
30
|
+
attr_reader :unlimited_lives
|
31
|
+
|
32
|
+
# Public: The country code of the user's country.
|
33
|
+
attr_reader :country
|
34
|
+
|
35
|
+
# Public: The number of extra shots available to the user.
|
36
|
+
attr_reader :extra_shots
|
37
|
+
|
38
|
+
# Public: The user's level.
|
39
|
+
attr_reader :level
|
40
|
+
|
41
|
+
# Public: The experience points that the user has accumulated towards their
|
42
|
+
# next level.
|
43
|
+
attr_reader :level_points
|
44
|
+
|
45
|
+
# Public: The percentage progress towards the next level.
|
46
|
+
attr_reader :level_progress
|
47
|
+
|
48
|
+
# Public: The user's goal points.
|
49
|
+
attr_reader :goal_points
|
50
|
+
|
51
|
+
# Public: Boolean indicating whether the user has leveled up.
|
52
|
+
attr_reader :level_up
|
53
|
+
|
54
|
+
def initialize(id:, username: nil, facebook_id: nil, facebook_name: nil,
|
55
|
+
coins: nil, lives: nil, max_lives: nil, unlimited_lives: nil,
|
56
|
+
country: nil, extra_shots: nil, level: nil,
|
57
|
+
level_points: nil, level_progress: nil, goal_points: nil,
|
58
|
+
level_up: nil)
|
59
|
+
@id = id
|
60
|
+
@username = username
|
61
|
+
@facebook_id = facebook_id
|
62
|
+
@facebook_name = facebook_name
|
63
|
+
@coins = coins
|
64
|
+
@lives = lives
|
65
|
+
@max_lives = max_lives
|
66
|
+
@unlimited_lives = unlimited_lives
|
67
|
+
@country = country
|
68
|
+
@extra_shots = extra_shots
|
69
|
+
@level = level
|
70
|
+
@level_points = level_points
|
71
|
+
@level_progress = level_progress
|
72
|
+
@goal_points = goal_points
|
73
|
+
@level_up = level_up
|
74
|
+
end
|
75
|
+
|
76
|
+
# Public: Returns true if the user has one or more lives, or has unlimited
|
77
|
+
# lives.
|
78
|
+
#
|
79
|
+
# Examples
|
80
|
+
#
|
81
|
+
# if user.start_new_game?
|
82
|
+
# ... start a new game ...
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# Returns true if the user can start a new game, false otherwise.
|
86
|
+
def start_new_game?
|
87
|
+
@lives >= 1 || @unlimited_lives
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|