triviacrack 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fb67144378010ccdb7d4f1c3827dcc632edf3947de9303428778ddd906196f0
4
- data.tar.gz: e25ada0b08a6a906e992e449b6d269ccd04253deb77a573b1d907c26ffd4506c
3
+ metadata.gz: 2de9839d21187467c365a29b48447c9ae9e058cd853ceac51c6c8e762e35345f
4
+ data.tar.gz: 475b9ded23d01cfe5d25d6880818c7e90afe9de582f0a8ffecc29342493b1a87
5
5
  SHA512:
6
- metadata.gz: 662f27c81247ce6b788e03727585cfb8af826e54afd93b17f38b813a684029935a2e5b276ab0b30bf8040bf9701400a843a00a6ef57bd991f1849cf9693efabb
7
- data.tar.gz: e687e13f2c5c1f03859de4e48c484f24bbb708520f3b1f76128531635298337f36ea3fe1ddd77c9aff4165b10e74b3fa51f9a45e2959db3d1fff8b1c6a9bdb97
6
+ metadata.gz: baa379d974f8be58d8d66bccee62f7702debc471c739eaef1fbc959750f46565ed1756ae26f7b8f5d5b08644bd5cbbc76cdb01cab1f7a1a7e51173611b8e5441
7
+ data.tar.gz: cd7e39d62ec509c03d4e9a93e7a9b5ff0f6250dab666377c37b2c0b3a0eb6444a47a81c9317c9218e5cf78a4f92a82ac22fe2052dbc2b933d83ac05c70a91cc2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- triviacrack (0.3.2)
4
+ triviacrack (0.4.0)
5
5
  unirest-2x (~> 1.1.3)
6
6
 
7
7
  GEM
@@ -11,7 +11,7 @@ module TriviaCrack
11
11
 
12
12
  # Public: Uses the Trivia Crack API to answer the given question.
13
13
  #
14
- # game - The ID of the TriviaCrack::Game.
14
+ # game_id - The ID of the TriviaCrack::Game.
15
15
  # question - The TriviaCrack::Question.
16
16
  # answer - The index of the answer to be submitted.
17
17
  #
@@ -38,6 +38,47 @@ module TriviaCrack
38
38
  [game, answer == question.correct_answer]
39
39
  end
40
40
 
41
+ # Public: Uses the Trivia Crack API to answer duel questions for the game.
42
+ #
43
+ # game_id - The ID of the TriviaCrack::Game.
44
+ # questions - The array of TriviaCrack::Question to answer
45
+ # answer_map - Hash of question ID / answer indices.
46
+ #
47
+ # Examples
48
+ #
49
+ # answers = {}
50
+ # for question in game.questions do
51
+ # answers[question.id] = <some answer index> // 0, 1, 2 or 3
52
+ # end
53
+ # answer_questions game.id, game.questions, answers
54
+ #
55
+ # Returns the update TriviaCrack::Game object, as well as a map
56
+ # with an entry for each question ID and a boolean indicating if
57
+ # the question was answered successfully.
58
+ # Raises TriviaCrack::Errors::RequestError if the request fails
59
+ def answer_questions(game_id, questions, answer_map)
60
+ answers = []
61
+ correct_answers = {}
62
+
63
+ for question in questions do
64
+ answer = {
65
+ id: question.id,
66
+ answer: answer_map[question.id],
67
+ category: question.category.upcase
68
+ }
69
+ answers << answer
70
+ correct_answers[question.id] = answer_map[question.id] == question.correct_answer
71
+ end
72
+
73
+ response =
74
+ post "/api/users/#{@session.user_id}/games/#{game_id}/answers",
75
+ parameters: { type: questions.first.type.upcase, answers: answers }.to_json
76
+
77
+ game = TriviaCrack::Parsers::GameParser.parse response.body
78
+
79
+ [game, correct_answers]
80
+ end
81
+
41
82
  end
42
83
  end
43
84
  end
@@ -31,6 +31,12 @@ module TriviaCrack
31
31
  question = TriviaCrack::Parsers::QuestionParser.parse q_data
32
32
  questions << question
33
33
  end
34
+ if raw_data["spins_data"]["spins"][0]["type"] == "DUEL"
35
+ q_data = raw_data["spins_data"]["spins"][0]["tie_break_question"]
36
+ q_data["type"] = raw_data["spins_data"]["spins"][0]["type"]
37
+ question = TriviaCrack::Parsers::QuestionParser.parse q_data
38
+ questions << question
39
+ end
34
40
  end
35
41
 
36
42
  if raw_data["available_crowns"]
@@ -1,4 +1,4 @@
1
1
  # Public: The version of this gem.
2
2
  module TriviaCrack
3
- VERSION = "0.3.2"
3
+ VERSION = "0.4.0"
4
4
  end
@@ -17,7 +17,7 @@ describe TriviaCrack::API::Question do
17
17
  let(:question) { double(id: 1, correct_answer: 1, type: "NORMAL", category: "SPORTS") }
18
18
  let(:game_id) { 22 }
19
19
 
20
- context 'given that the question was answer correctly' do
20
+ context 'given that the question was answered correctly' do
21
21
  let(:code) { 200 }
22
22
  let(:answer) { 1 }
23
23
 
@@ -26,7 +26,7 @@ describe TriviaCrack::API::Question do
26
26
  it { expect(subject[1]).to be true }
27
27
  end
28
28
 
29
- context 'given that the question was answer incorrectly' do
29
+ context 'given that the question was answered incorrectly' do
30
30
  let(:code) { 200 }
31
31
  let(:answer) { 0 }
32
32
 
@@ -46,4 +46,46 @@ describe TriviaCrack::API::Question do
46
46
  .and having_attributes(url: "/api/users/#{session.user_id}/games/#{game_id}/answers")) }
47
47
  end
48
48
  end
49
+
50
+ describe "#answer_questions" do
51
+
52
+ subject { client.answer_questions game_id, questions, answers }
53
+
54
+ let(:raw_data) { SpecData.get "answer.json" }
55
+ let(:question_one) { double(id: 1, correct_answer: 1, type: "DUEL", category: "SPORTS") }
56
+ let(:question_two) { double(id: 2, correct_answer: 2, type: "DUEL", category: "ART") }
57
+ let(:questions) { [question_one, question_two] }
58
+ let(:game_id) { 22 }
59
+
60
+ context 'given that the questions were answered correctly' do
61
+ let(:code) { 200 }
62
+ let(:answers) { {1 => 1, 2 => 2} }
63
+
64
+ it { expect(TriviaCrack::Parsers::GameParser).to receive(:parse).once; subject }
65
+ it { expect(subject[0]).to be_a TriviaCrack::Game }
66
+ it { expect(subject[1][1]).to be true }
67
+ it { expect(subject[1][2]).to be true }
68
+ end
69
+
70
+ context 'given that a question was answered incorrectly' do
71
+ let(:code) { 200 }
72
+ let(:answers) { {1 => 1, 2 => 3} }
73
+
74
+ it { expect(TriviaCrack::Parsers::GameParser).to receive(:parse).once; subject }
75
+ it { expect(subject[0]).to be_a TriviaCrack::Game }
76
+ it { expect(subject[1][1]).to be true }
77
+ it { expect(subject[1][2]).to be false }
78
+ end
79
+
80
+ context 'given that the request fails' do
81
+ let(:code) { 400 }
82
+ let(:answers) { {1 => 1, 2 => 2} }
83
+
84
+ it { expect{ subject }.to raise_error TriviaCrack::Errors::RequestError }
85
+ it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
86
+ .and having_attributes(code: code)) }
87
+ it { expect{ subject }.to raise_error(an_instance_of(TriviaCrack::Errors::RequestError)
88
+ .and having_attributes(url: "/api/users/#{session.user_id}/games/#{game_id}/answers")) }
89
+ end
90
+ end
49
91
  end
@@ -0,0 +1,336 @@
1
+ {
2
+ "id": 1119,
3
+ "safeOpponentSelectionType": "RANDOM",
4
+ "spins_data": {
5
+ "spins": [
6
+ {
7
+ "type": "DUEL",
8
+ "second_chance_available": false,
9
+ "questions": [
10
+ {
11
+ "question": {
12
+ "id": 17881223,
13
+ "category": "HISTORY",
14
+ "text": "The Suffrage movement pertained to what?",
15
+ "answers": [
16
+ "Prohibition",
17
+ "Racial Equality",
18
+ "Women's Rights",
19
+ "Right To Unionize"
20
+ ],
21
+ "correct_answer": 2,
22
+ "media_type": "NORMAL",
23
+ "author": {
24
+ "id": 82565321,
25
+ "name": "anniesparkles",
26
+ "username": "anniesparkles",
27
+ "fb_show_picture": false,
28
+ "fb_show_name": false,
29
+ "gender": "UNSPECIFIED"
30
+ }
31
+ }
32
+ },
33
+ {
34
+ "question": {
35
+ "id": 53333638,
36
+ "category": "GEOGRAPHY",
37
+ "text": "Where is Slovakia?",
38
+ "answers": [
39
+ "Oceania",
40
+ "Africa",
41
+ "Europe",
42
+ "Asia"
43
+ ],
44
+ "correct_answer": 2,
45
+ "media_type": "NORMAL",
46
+ "author": {
47
+ "id": 204000197,
48
+ "name": "martingres",
49
+ "username": "martingres",
50
+ "fb_show_picture": false,
51
+ "fb_show_name": false,
52
+ "gender": "UNSPECIFIED"
53
+ }
54
+ }
55
+ },
56
+ {
57
+ "question": {
58
+ "id": 62181053,
59
+ "category": "ARTS",
60
+ "text": "Who has painted the Sistine Chapel?",
61
+ "answers": [
62
+ "Michelangelo",
63
+ "Van Gogh",
64
+ "Picasso",
65
+ "Mezzosin"
66
+ ],
67
+ "correct_answer": 0,
68
+ "media_type": "NORMAL",
69
+ "author": {
70
+ "id": 223909199,
71
+ "facebook_id": "843171349124950",
72
+ "name": "Kristina Marie Poulsen",
73
+ "username": "kp89facebook",
74
+ "facebook_name": "Kristina Marie Poulsen",
75
+ "fb_show_picture": true,
76
+ "fb_show_name": true,
77
+ "gender": "female"
78
+ },
79
+ "translator": {
80
+ "id": 252287897,
81
+ "name": "ragingraids",
82
+ "username": "ragingraids",
83
+ "fb_show_picture": false,
84
+ "fb_show_name": false,
85
+ "gender": "UNSPECIFIED"
86
+ }
87
+ }
88
+ },
89
+ {
90
+ "question": {
91
+ "id": 51360542,
92
+ "category": "SPORTS",
93
+ "text": "What two Allstars have the number of 23?",
94
+ "answers": [
95
+ "Steph Curry And Blake Griffen",
96
+ "Lebron James And Michael Jordan",
97
+ "Chris Paul And Jr Smith",
98
+ "Clay Thompson And Kevin Love"
99
+ ],
100
+ "correct_answer": 1,
101
+ "media_type": "NORMAL",
102
+ "author": {
103
+ "id": 200488852,
104
+ "name": "mitchelldonegan06",
105
+ "username": "mitchelldonegan06",
106
+ "fb_show_picture": false,
107
+ "fb_show_name": false,
108
+ "gender": "UNSPECIFIED"
109
+ }
110
+ }
111
+ },
112
+ {
113
+ "question": {
114
+ "id": 11817577,
115
+ "category": "ENTERTAINMENT",
116
+ "text": "Which pop star calls her fans \"Monsters\"?",
117
+ "answers": [
118
+ "Madonna",
119
+ "Lady Gaga",
120
+ "Britney Spears",
121
+ "Christina Aguilera"
122
+ ],
123
+ "correct_answer": 1,
124
+ "media_type": "NORMAL",
125
+ "author": {
126
+ "id": 51771646,
127
+ "facebook_id": "",
128
+ "name": "linda.tsacalides",
129
+ "username": "linda.tsacalides",
130
+ "facebook_name": "UNSPECIFIED",
131
+ "fb_show_picture": false,
132
+ "fb_show_name": false,
133
+ "gender": "female"
134
+ }
135
+ }
136
+ },
137
+ {
138
+ "question": {
139
+ "id": 54733278,
140
+ "category": "SCIENCE",
141
+ "text": "Which is NOT a blood type?",
142
+ "answers": [
143
+ "Ab",
144
+ "Oa",
145
+ "O",
146
+ "B"
147
+ ],
148
+ "correct_answer": 1,
149
+ "media_type": "NORMAL",
150
+ "author": {
151
+ "id": 114837290,
152
+ "name": "rocat26",
153
+ "username": "rocat26",
154
+ "fb_show_picture": false,
155
+ "fb_show_name": false,
156
+ "gender": "female"
157
+ }
158
+ }
159
+ }
160
+ ],
161
+ "tie_break_question": {
162
+ "id": 51774,
163
+ "category": "HISTORY",
164
+ "text": "Which historical figure is famous for being short?",
165
+ "answers": [
166
+ "Napoleon",
167
+ "Stalin",
168
+ "Bismark",
169
+ "Julius Caesar"
170
+ ],
171
+ "correct_answer": 0,
172
+ "media_type": "NORMAL",
173
+ "author": {
174
+ "id": 23831917,
175
+ "facebook_id": "100006890463075",
176
+ "name": "Igor Gardella Trifunovic",
177
+ "username": "kider.jo",
178
+ "facebook_name": "Igor Gardella Trifunovic",
179
+ "fb_show_picture": true,
180
+ "fb_show_name": true,
181
+ "gender": "UNSPECIFIED"
182
+ },
183
+ "translator": {
184
+ "id": 100940141,
185
+ "facebook_id": "100002352524111",
186
+ "name": "Abigail Morgan",
187
+ "username": "almangel21",
188
+ "facebook_name": "Abigail Morgan",
189
+ "fb_show_picture": true,
190
+ "fb_show_name": true,
191
+ "gender": "female"
192
+ }
193
+ }
194
+ }
195
+ ]
196
+ },
197
+ "available_crowns": [
198
+ "SCIENCE",
199
+ "HISTORY",
200
+ "ENTERTAINMENT",
201
+ "SPORTS"
202
+ ],
203
+ "my_player_number": 1,
204
+ "player_one": {
205
+ "charges": 3,
206
+ "crowns": [
207
+ "GEOGRAPHY",
208
+ "ARTS"
209
+ ]
210
+ },
211
+ "player_two": {
212
+ "charges": 0,
213
+ "crowns": [
214
+ "SCIENCE"
215
+ ]
216
+ },
217
+ "round_number": 2,
218
+ "crown_events": [
219
+ {
220
+ "timestamp": 1583628928756,
221
+ "userId": 123,
222
+ "category": "SCIENCE",
223
+ "won": true
224
+ }
225
+ ],
226
+ "sub_status": "P1_PLAYING_TURN",
227
+ "previous_sub_status": "P1_PLAYING_TURN",
228
+ "statistics": {
229
+ "player_one_statistics": {
230
+ "category_questions": [
231
+ {
232
+ "category": "HISTORY",
233
+ "correct": 1,
234
+ "incorrect": 0,
235
+ "worst": false
236
+ },
237
+ {
238
+ "category": "SCIENCE",
239
+ "correct": 2,
240
+ "incorrect": 0,
241
+ "worst": false
242
+ },
243
+ {
244
+ "category": "ARTS",
245
+ "correct": 3,
246
+ "incorrect": 0,
247
+ "worst": false
248
+ },
249
+ {
250
+ "category": "ENTERTAINMENT",
251
+ "correct": 0,
252
+ "incorrect": 1,
253
+ "worst": true,
254
+ "performance": 0
255
+ },
256
+ {
257
+ "category": "SPORTS",
258
+ "correct": 2,
259
+ "incorrect": 0,
260
+ "worst": false
261
+ },
262
+ {
263
+ "category": "GEOGRAPHY",
264
+ "correct": 1,
265
+ "incorrect": 0,
266
+ "worst": false
267
+ }
268
+ ],
269
+ "correct_answers": 9,
270
+ "incorrect_answers": 1,
271
+ "challenges_won": 0,
272
+ "questions_answered": 10,
273
+ "crowns_won": 2
274
+ },
275
+ "player_two_statistics": {
276
+ "category_questions": [
277
+ {
278
+ "category": "HISTORY",
279
+ "correct": 0,
280
+ "incorrect": 1,
281
+ "worst": false
282
+ },
283
+ {
284
+ "category": "SCIENCE",
285
+ "correct": 1,
286
+ "incorrect": 0,
287
+ "worst": false
288
+ }
289
+ ],
290
+ "correct_answers": 1,
291
+ "incorrect_answers": 1,
292
+ "challenges_won": 0,
293
+ "questions_answered": 2,
294
+ "crowns_won": 1
295
+ }
296
+ },
297
+ "is_random": true,
298
+ "unread_messages": 0,
299
+ "last_turn": "03/07/2020 19:56:48 EST",
300
+ "status_version": 13,
301
+ "my_level_data": {
302
+ "level": 16,
303
+ "points": 152,
304
+ "goal_points": 168,
305
+ "progress": 11,
306
+ "level_up": true
307
+ },
308
+ "opponent_selection_type": "RANDOM",
309
+ "nudge_available": false,
310
+ "opponent": {
311
+ "id": 321,
312
+ "alerts_count": 0,
313
+ "username": "aaa",
314
+ "facebook_id": "123",
315
+ "facebookId": {
316
+ "value": "",
317
+ "defined": false
318
+ },
319
+ "facebook_name": "a b",
320
+ "fb_show_picture": true,
321
+ "fb_show_name": true,
322
+ "allow_og_posts": true,
323
+ "photo_url": "",
324
+ "level_data": {
325
+ "level": 6
326
+ },
327
+ "is_friend": false,
328
+ "league_name": "APPRENTICE_1"
329
+ },
330
+ "game_status": "ACTIVE",
331
+ "language": "EN",
332
+ "created": "03/07/2020 19:30:03 EST",
333
+ "type": "NORMAL",
334
+ "my_turn": true,
335
+ "expiration_date": "03/09/2020 07:56:48 EST"
336
+ }
@@ -110,5 +110,31 @@ describe TriviaCrack::Parsers::GameParser do
110
110
  its(:my_statistics) { is_expected.to be_a TriviaCrack::GameStatistics }
111
111
  its(:opponent_statistics) { is_expected.to be_a TriviaCrack::GameStatistics }
112
112
  end
113
+
114
+ context 'when given data from the games API and the game is a duel' do
115
+ let(:raw_data) { SpecData.get "game_duel.json" }
116
+
117
+ it { is_expected.to be_a TriviaCrack::Game }
118
+ its(:id) { is_expected.to be 1119 }
119
+ its(:opponent) { is_expected.to be_a TriviaCrack::User }
120
+ its('opponent.id') { is_expected.to be 321 }
121
+ its(:game_status) { is_expected.to be :active }
122
+ its(:language) { is_expected.to be :en }
123
+ its(:created) { is_expected.to be_a Time }
124
+ its(:last_turn) { is_expected.to be_a Time }
125
+ its(:type) { is_expected.to be :normal }
126
+ its(:expiration_date) { is_expected.to be_a Time }
127
+ its(:my_turn) { is_expected.to be true }
128
+ its(:round_number) { is_expected.to be 2 }
129
+ its(:is_random) { is_expected.to be true }
130
+ its(:unread_messages) { is_expected.to be 0 }
131
+ its(:status_version) { is_expected.to be 13 }
132
+ its(:available_crowns) { is_expected.to contain_exactly(:entertainment, :science, :history, :sports) }
133
+ its('questions.size') { is_expected.to eq(7) }
134
+ its('questions.first') { is_expected.to be_a TriviaCrack::Question }
135
+ its('questions.first.id') { is_expected.to be 17881223 }
136
+ its(:my_statistics) { is_expected.to be_a TriviaCrack::GameStatistics }
137
+ its(:opponent_statistics) { is_expected.to be_a TriviaCrack::GameStatistics }
138
+ end
113
139
  end
114
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triviacrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Kus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-06 00:00:00.000000000 Z
11
+ date: 2020-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -172,6 +172,7 @@ files:
172
172
  - spec/data/answer.json
173
173
  - spec/data/dashboard.json
174
174
  - spec/data/game.json
175
+ - spec/data/game_duel.json
175
176
  - spec/data/login.json
176
177
  - spec/data/my_profile.json
177
178
  - spec/data/new_game.json
@@ -224,6 +225,7 @@ test_files:
224
225
  - spec/data/answer.json
225
226
  - spec/data/dashboard.json
226
227
  - spec/data/game.json
228
+ - spec/data/game_duel.json
227
229
  - spec/data/login.json
228
230
  - spec/data/my_profile.json
229
231
  - spec/data/new_game.json