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.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +19 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +120 -0
  8. data/Rakefile +7 -0
  9. data/lib/triviacrack.rb +12 -0
  10. data/lib/triviacrack/api/client.rb +31 -0
  11. data/lib/triviacrack/api/common.rb +76 -0
  12. data/lib/triviacrack/api/game.rb +72 -0
  13. data/lib/triviacrack/api/login.rb +35 -0
  14. data/lib/triviacrack/api/profile.rb +44 -0
  15. data/lib/triviacrack/api/question.rb +43 -0
  16. data/lib/triviacrack/api/user.rb +56 -0
  17. data/lib/triviacrack/category_statistics.rb +27 -0
  18. data/lib/triviacrack/errors/parse_error.rb +7 -0
  19. data/lib/triviacrack/errors/request_error.rb +18 -0
  20. data/lib/triviacrack/game.rb +100 -0
  21. data/lib/triviacrack/game_statistics.rb +35 -0
  22. data/lib/triviacrack/parsers/category_statistics_parser.rb +45 -0
  23. data/lib/triviacrack/parsers/game_parser.rb +74 -0
  24. data/lib/triviacrack/parsers/game_statistics_parser.rb +44 -0
  25. data/lib/triviacrack/parsers/profile_parser.rb +55 -0
  26. data/lib/triviacrack/parsers/question_parser.rb +36 -0
  27. data/lib/triviacrack/parsers/session_parser.rb +36 -0
  28. data/lib/triviacrack/parsers/time_parser.rb +26 -0
  29. data/lib/triviacrack/parsers/user_parser.rb +47 -0
  30. data/lib/triviacrack/profile.rb +96 -0
  31. data/lib/triviacrack/question.rb +43 -0
  32. data/lib/triviacrack/session.rb +30 -0
  33. data/lib/triviacrack/user.rb +91 -0
  34. data/lib/triviacrack/version.rb +4 -0
  35. data/spec/api/game_spec.rb +72 -0
  36. data/spec/api/login_spec.rb +34 -0
  37. data/spec/api/profile_spec.rb +51 -0
  38. data/spec/api/question_spec.rb +45 -0
  39. data/spec/api/user_spec.rb +50 -0
  40. data/spec/data/answer.json +161 -0
  41. data/spec/data/dashboard.json +949 -0
  42. data/spec/data/game.json +147 -0
  43. data/spec/data/login.json +25 -0
  44. data/spec/data/my_profile.json +556 -0
  45. data/spec/data/new_game.json +363 -0
  46. data/spec/data/profile.json +553 -0
  47. data/spec/data/question.json +14 -0
  48. data/spec/data/question_image.json +15 -0
  49. data/spec/data/search.json +123 -0
  50. data/spec/data/user.json +25 -0
  51. data/spec/game_spec.rb +60 -0
  52. data/spec/parsers/category_statistics_parser_spec.rb +41 -0
  53. data/spec/parsers/game_parser_spec.rb +114 -0
  54. data/spec/parsers/game_statistics_parser_spec.rb +56 -0
  55. data/spec/parsers/profile_parser_spec.rb +59 -0
  56. data/spec/parsers/question_parser_spec.rb +34 -0
  57. data/spec/parsers/session_parser_spec.rb +20 -0
  58. data/spec/parsers/time_parser_spec.rb +19 -0
  59. data/spec/parsers/user_parser_spec.rb +51 -0
  60. data/spec/spec_helper.rb +48 -0
  61. data/spec/user_spec.rb +33 -0
  62. data/triviacrack.gemspec +31 -0
  63. metadata +258 -0
@@ -0,0 +1,43 @@
1
+ require "triviacrack/api/common"
2
+ require "triviacrack/parsers/game_parser"
3
+
4
+ # Public: All methods in this module make requests to the Trivia Crack answers
5
+ # API.
6
+ module TriviaCrack
7
+ module API
8
+ module Question
9
+
10
+ include TriviaCrack::API::Common
11
+
12
+ # Public: Uses the Trivia Crack API to answer the given question.
13
+ #
14
+ # game - The ID of the TriviaCrack::Game.
15
+ # question - The TriviaCrack::Question.
16
+ # answer - The index of the answer to be submitted.
17
+ #
18
+ # Examples
19
+ #
20
+ # answer_question game.id, question, 1
21
+ #
22
+ # Returns a boolean indicating whether or not the question was answered
23
+ # correctly, and the updated TriviaCrack::Game object.
24
+ # Raises TriviaCrack::Errors::RequestError if the request fails
25
+ def answer_question(game_id, question, answer)
26
+ response =
27
+ post "/api/users/#{@session.user_id}/games/#{game_id}/answers",
28
+ parameters: { type: question.type.upcase,
29
+ answers: [{
30
+ id: question.id,
31
+ answer: answer,
32
+ category: question.category.upcase
33
+ }]
34
+ }.to_json
35
+
36
+ game = TriviaCrack::Parsers::GameParser.parse response.body
37
+
38
+ [game, answer == question.correct_answer]
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ require "triviacrack/api/common"
2
+ require "triviacrack/parsers/user_parser"
3
+
4
+ # Public: All methods in this module make requests to the Trivia Crack users
5
+ # API.
6
+ module TriviaCrack
7
+ module API
8
+ module User
9
+
10
+ include TriviaCrack::API::Common
11
+
12
+ # Public: Uses the Trivia Crack Search API to find the numeric user id of
13
+ # the user identified by the given username.
14
+ #
15
+ # username - Trivia Crack username of a user (e.g. "@example").
16
+ #
17
+ # Examples
18
+ #
19
+ # get_user_id "@example"
20
+ #
21
+ # Returns the user id of the user.
22
+ # Raises TriviaCrack:Errors::RequestError if the request fails.
23
+ def get_user_id(username)
24
+ # Trim the @ character from the start of the username
25
+ if username.start_with? "@"
26
+ username = username[1..-1]
27
+ end
28
+
29
+ response = get "/api/search?username=#{username}"
30
+
31
+ body = response.body
32
+
33
+ user_id = false
34
+ body["list"].each do |user|
35
+ if user["username"] == username
36
+ user_id = user["id"]
37
+ break
38
+ end
39
+ end
40
+
41
+ user_id
42
+ end
43
+
44
+ # Public: Uses the Trivia Crack API to retrieve the user data.
45
+ #
46
+ # Returns a TriviaCrack::User representing the current user.
47
+ # Raises TriviaCrack::Errors::RequestError if the request fails
48
+ def get_user
49
+ response = get "/api/users/#{@session.user_id}"
50
+
51
+ TriviaCrack::Parsers::UserParser.parse response.body
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,27 @@
1
+ # Public: An object representing category statistics for a player within a
2
+ # single Trivia Crack game.
3
+ module TriviaCrack
4
+ class CategoryStatistics
5
+
6
+ # Public: The category for these statistics (:arts, :geography, etc).
7
+ attr_reader :category
8
+
9
+ # Public: The number of questions answered correctly for this category.
10
+ attr_reader :correct
11
+
12
+ # Public: The number of questions answered incorrectly for this category.
13
+ attr_reader :incorrect
14
+
15
+ # Public: Boolean indicating whether this is the worst category for the
16
+ # player.
17
+ attr_reader :worst
18
+
19
+ def initialize(category: nil, correct: nil, incorrect: nil, worst: nil)
20
+ @category = category
21
+ @correct = correct
22
+ @incorrect = incorrect
23
+ @worst = worst
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ # Public: This error is raised when a block of data cannot be correctly parsed.
2
+ module TriviaCrack
3
+ module Errors
4
+ class ParseError < StandardError
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # Public: This error is raised when a web request to the Trivia Crack API is
2
+ # unsuccessful.
3
+ module TriviaCrack
4
+ module Errors
5
+ class RequestError < StandardError
6
+
7
+ # Public: The HTTP status code returned by the Trivia Crack server.
8
+ attr_reader :code
9
+
10
+ # Public: Initializes a RequestError.
11
+ #
12
+ # code - The HTTP status code returned by the Trivia Crack server.
13
+ def initialize(code)
14
+ @code = code
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,100 @@
1
+ # Public: An object representing a Trivia Crack Game, including information
2
+ # about the questions to be asked in the game, the state of the game, opponent
3
+ # information, and more.
4
+ module TriviaCrack
5
+ class Game
6
+
7
+ # Public: The unique identifier of the game.
8
+ attr_reader :id
9
+
10
+ # Public: The TriviaCrack::User information for the opponent.
11
+ attr_reader :opponent
12
+
13
+ # Public: The current status of the game (e.g. :active, :ended,
14
+ # :pending_approval).
15
+ attr_reader :game_status
16
+
17
+ # Public: The language used for the game (e.g. :en).
18
+ attr_reader :language
19
+
20
+ # Public: The datetime on which the game was created.
21
+ attr_reader :created
22
+
23
+ # Public: The datetime on which the last turn was taken.
24
+ attr_reader :last_turn
25
+
26
+ # Public: The type of the game (:normal, :duel)
27
+ attr_reader :type
28
+
29
+ # Public: The datetime on which the game will expire.
30
+ attr_reader :expiration_date
31
+
32
+ # Public: Boolean indicating whether it is the user's turn.
33
+ attr_reader :my_turn
34
+
35
+ # Public: The number of the current round.
36
+ attr_reader :round_number
37
+
38
+ # Public: Boolean indicating whether the game was created with a random
39
+ # opponent.
40
+ attr_reader :is_random
41
+
42
+ # Public: Number of unread messages.
43
+ attr_reader :unread_messages
44
+
45
+ # Public: The status version.
46
+ attr_reader :status_version
47
+
48
+ # Public: Array of available crowns that can be won by the user.
49
+ attr_reader :available_crowns
50
+
51
+ # Public: Array of questions that can be answered by the user. This array
52
+ # will be empty if the user is unable to play (i.e. not their turn).
53
+ attr_reader :questions
54
+
55
+ # Public: TriviaCrack::GameStatistics for the user.
56
+ attr_reader :my_statistics
57
+
58
+ # Public: TriviaCrack::GameStatistics for the opponent.
59
+ attr_reader :opponent_statistics
60
+
61
+ def initialize(id:, opponent: nil, game_status: nil, language: nil,
62
+ created: nil, last_turn: nil, type: nil,
63
+ expiration_date: nil, my_turn: nil, round_number: nil,
64
+ is_random: nil, unread_messages: nil, status_version: nil,
65
+ available_crowns: nil, questions: nil, my_statistics: nil,
66
+ opponent_statistics: nil)
67
+ @id = id
68
+ @opponent = opponent
69
+ @game_status = game_status
70
+ @language = language
71
+ @created = created
72
+ @last_turn = last_turn
73
+ @type = type
74
+ @expiration_date = expiration_date
75
+ @my_turn = my_turn
76
+ @round_number = round_number
77
+ @is_random = is_random
78
+ @unread_messages = unread_messages
79
+ @status_version = status_version
80
+ @available_crowns = available_crowns
81
+ @questions = questions
82
+ @my_statistics = my_statistics
83
+ @opponent_statistics = opponent_statistics
84
+ end
85
+
86
+ # Public: Returns true if the game has not ended and it is the user's turn.
87
+ #
88
+ # Examples
89
+ #
90
+ # if game.playable?
91
+ # ... do something ...
92
+ # end
93
+ #
94
+ # Returns a boolean indicating if the game is playable.
95
+ def playable?
96
+ @my_turn && @game_status != :ended
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,35 @@
1
+ # Public: An object representing player statistics for a Trivia Crack game.
2
+ module TriviaCrack
3
+ class GameStatistics
4
+
5
+ # Public: The number of questions answered correctly.
6
+ attr_reader :correct_answers
7
+
8
+ # Public: The number of questions answered incorrectly.
9
+ attr_reader :incorrect_answers
10
+
11
+ # Public: The total number of questions answered.
12
+ attr_reader :questions_answered
13
+
14
+ # Public: The total number of challenges won.
15
+ attr_reader :challenges_won
16
+
17
+ # Public: The list of categories for which crowns have been won.
18
+ attr_reader :crowns
19
+
20
+ # Public: A hash of category statistics.
21
+ attr_reader :categories
22
+
23
+ def initialize(correct_answers: nil, incorrect_answers: nil,
24
+ questions_answered: nil, challenges_won: nil,
25
+ crowns: nil, categories: nil)
26
+ @correct_answers = correct_answers
27
+ @incorrect_answers = incorrect_answers
28
+ @questions_answered = questions_answered
29
+ @challenges_won = challenges_won
30
+ @crowns = crowns
31
+ @categories = categories
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ require "triviacrack/category_statistics"
2
+
3
+ # Internal: This module is used to parse data returned from the Trivia Crack API
4
+ # into a ruby object that represents category statistics for a Trivia Crack
5
+ # game.
6
+ module TriviaCrack
7
+ module Parsers
8
+ module CategoryStatisticsParser
9
+
10
+ # Internal: Parses data returned from the Trivia Crack API to create
11
+ # TriviaCrack::CategoryStatistics objects.
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
+ # stats = TriviaCrack::Parsers::CategoryStatisticsParser.parse raw_data
20
+ #
21
+ # Returns a hash of TriviaCrack::CategoryStatistics objects, keyed by
22
+ # category.
23
+ def self.parse(raw_data)
24
+ categories = {}
25
+
26
+ if raw_data
27
+ raw_data.each do |category|
28
+ category_name = category["category"].downcase.to_sym
29
+
30
+ categories[category_name] =
31
+ TriviaCrack::CategoryStatistics.new(
32
+ category: category_name,
33
+ correct: category["correct"],
34
+ incorrect: category["incorrect"],
35
+ worst: category["worst"]
36
+ )
37
+ end
38
+ end
39
+
40
+ categories
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,74 @@
1
+ require "triviacrack/game"
2
+ require "triviacrack/parsers/game_statistics_parser"
3
+ require "triviacrack/parsers/question_parser"
4
+ require "triviacrack/parsers/time_parser"
5
+ require "triviacrack/parsers/user_parser"
6
+
7
+ # Internal: This module is used to parse data returned from the Trivia Crack API
8
+ # into a ruby object that represents a Trivia Crack game.
9
+ module TriviaCrack
10
+ module Parsers
11
+ module GameParser
12
+
13
+ # Internal: Parses data returned from the Trivia Crack API to create a
14
+ # TriviaCrack::Game object.
15
+ #
16
+ # raw_data - A hash of the raw data returned by the Trivia Crack API.
17
+ #
18
+ # Examples
19
+ #
20
+ # raw_data = get_raw_data_from_API
21
+ # ...
22
+ # game = TriviaCrack::Parsers::GameParser.parse raw_data
23
+ #
24
+ # Returns a TriviaCrack::Game.
25
+ def self.parse(raw_data)
26
+ questions = []
27
+ if raw_data["spins_data"]
28
+ raw_data["spins_data"]["spins"][0]["questions"].each do |q_data|
29
+ q_data = q_data["question"]
30
+ q_data["type"] = raw_data["spins_data"]["spins"][0]["type"]
31
+ question = TriviaCrack::Parsers::QuestionParser.parse q_data
32
+ questions << question
33
+ end
34
+ end
35
+
36
+ if raw_data["available_crowns"]
37
+ crowns = raw_data["available_crowns"].map { |c| c.downcase.to_sym }
38
+ else
39
+ crowns = []
40
+ end
41
+
42
+ me = raw_data["my_player_number"] == 1 ? "player_one" : "player_two"
43
+ them = raw_data["my_player_number"] == 1 ? "player_two" : "player_one"
44
+
45
+ my_statistics = raw_data["statistics"]["#{me}_statistics"]
46
+ my_statistics["crowns"] = raw_data[me]["crowns"]
47
+
48
+ opponent_statistics = raw_data["statistics"]["#{them}_statistics"]
49
+ opponent_statistics["crowns"] = raw_data[them]["crowns"]
50
+
51
+ TriviaCrack::Game.new(
52
+ id: raw_data["id"],
53
+ opponent: UserParser.parse(raw_data["opponent"]),
54
+ game_status: raw_data["game_status"].downcase.to_sym,
55
+ language: raw_data["language"].downcase.to_sym,
56
+ created: TimeParser.parse(raw_data["created"]),
57
+ last_turn: TimeParser.parse(raw_data["last_turn"]),
58
+ type: raw_data["type"].downcase.to_sym,
59
+ expiration_date: TimeParser.parse(raw_data["expiration_date"]),
60
+ my_turn: raw_data["my_turn"],
61
+ round_number: raw_data["round_number"],
62
+ is_random: raw_data["is_random"],
63
+ unread_messages: raw_data["unread_messages"],
64
+ status_version: raw_data["status_version"],
65
+ available_crowns: crowns,
66
+ questions: questions,
67
+ my_statistics: GameStatisticsParser.parse(my_statistics),
68
+ opponent_statistics: GameStatisticsParser.parse(opponent_statistics)
69
+ )
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,44 @@
1
+ require "triviacrack/game_statistics"
2
+ require "triviacrack/parsers/category_statistics_parser"
3
+
4
+ # Internal: This module is used to parse data returned from the Trivia Crack API
5
+ # into a ruby object that represents player statistics for a Trivia Crack game.
6
+ module TriviaCrack
7
+ module Parsers
8
+ module GameStatisticsParser
9
+
10
+ # Internal: Parses data returned from the Trivia Crack API to create a
11
+ # TriviaCrack::GameStatistics 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
+ # stats = TriviaCrack::Parsers::GameStatisticsParser.parse raw_data
20
+ #
21
+ # Returns a TriviaCrack::GameStatistics.
22
+ def self.parse(raw_data)
23
+ categories =
24
+ CategoryStatisticsParser.parse raw_data["category_questions"]
25
+
26
+ if raw_data["crowns"]
27
+ crowns = raw_data["crowns"].map { |c| c.downcase.to_sym }
28
+ else
29
+ crowns = []
30
+ end
31
+
32
+ TriviaCrack::GameStatistics.new(
33
+ correct_answers: raw_data["correct_answers"],
34
+ incorrect_answers: raw_data["incorrect_answers"],
35
+ questions_answered: raw_data["questions_answered"],
36
+ challenges_won: raw_data["challenges_won"],
37
+ crowns: crowns,
38
+ categories: categories
39
+ )
40
+ end
41
+
42
+ end
43
+ end
44
+ end