triviacrack 0.5.1 → 0.8.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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +7 -2
  3. data/.github/workflows/publish.yml +8 -3
  4. data/.rubocop.yml +23 -0
  5. data/.ruby-version +1 -1
  6. data/Gemfile +1 -2
  7. data/Gemfile.lock +44 -39
  8. data/Rakefile +4 -2
  9. data/lib/triviacrack/api/client.rb +7 -7
  10. data/lib/triviacrack/api/common.rb +14 -16
  11. data/lib/triviacrack/api/game.rb +13 -14
  12. data/lib/triviacrack/api/login.rb +8 -8
  13. data/lib/triviacrack/api/profile.rb +6 -6
  14. data/lib/triviacrack/api/question.rb +16 -16
  15. data/lib/triviacrack/api/user.rb +11 -15
  16. data/lib/triviacrack/category_statistics.rb +2 -2
  17. data/lib/triviacrack/errors/parse_error.rb +2 -0
  18. data/lib/triviacrack/errors/request_error.rb +5 -3
  19. data/lib/triviacrack/game.rb +3 -3
  20. data/lib/triviacrack/game_statistics.rb +3 -3
  21. data/lib/triviacrack/parsers/category_statistics_parser.rb +13 -15
  22. data/lib/triviacrack/parsers/game_parser.rb +39 -39
  23. data/lib/triviacrack/parsers/game_statistics_parser.rb +15 -15
  24. data/lib/triviacrack/parsers/profile_parser.rb +28 -28
  25. data/lib/triviacrack/parsers/question_parser.rb +11 -11
  26. data/lib/triviacrack/parsers/session_parser.rb +10 -10
  27. data/lib/triviacrack/parsers/time_parser.rb +4 -4
  28. data/lib/triviacrack/parsers/user_parser.rb +21 -21
  29. data/lib/triviacrack/profile.rb +3 -3
  30. data/lib/triviacrack/question.rb +3 -3
  31. data/lib/triviacrack/session.rb +2 -2
  32. data/lib/triviacrack/user.rb +3 -3
  33. data/lib/triviacrack/version.rb +3 -1
  34. data/lib/triviacrack.rb +12 -10
  35. data/spec/api/game_spec.rb +49 -29
  36. data/spec/api/login_spec.rb +22 -15
  37. data/spec/api/profile_spec.rb +34 -20
  38. data/spec/api/question_spec.rb +49 -30
  39. data/spec/api/user_spec.rb +31 -20
  40. data/spec/game_spec.rb +4 -4
  41. data/spec/parsers/category_statistics_parser_spec.rb +8 -8
  42. data/spec/parsers/game_parser_spec.rb +21 -19
  43. data/spec/parsers/game_statistics_parser_spec.rb +11 -11
  44. data/spec/parsers/profile_parser_spec.rb +10 -10
  45. data/spec/parsers/question_parser_spec.rb +15 -11
  46. data/spec/parsers/session_parser_spec.rb +8 -8
  47. data/spec/parsers/time_parser_spec.rb +5 -5
  48. data/spec/parsers/user_parser_spec.rb +11 -11
  49. data/spec/spec_helper.rb +34 -32
  50. data/spec/user_spec.rb +4 -4
  51. data/triviacrack.gemspec +21 -20
  52. metadata +35 -48
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0498c4a4b88170d5974faab06d1f49c7c3b5235ab6d4224b7b82a95c019d0026'
4
- data.tar.gz: a4dd24a8012cf642a970d95170f89f30523f0ab684d1d02278e78124dbbf5d1d
3
+ metadata.gz: 3320fcde13cdbf856d5b2ff80d2cbfffa46c31d5965f5c8d65ea071689ef14ec
4
+ data.tar.gz: fcbd60485caff9ffef9acf64d5d75b7d9babcd3287d50fcc740a3d7ac1e45ade
5
5
  SHA512:
6
- metadata.gz: a9afae76cf81e4b70b60e41ec26e8c472ec243b5b8e306c1940c5bd14d2ce7749e13ed5b70ba016804021f7ce6b1c72c0248a72460cb1d363db14621adfd27e5
7
- data.tar.gz: cb80258751e28d76cc0fa3aac24512afbaa290be20eff2850687d95ec46f8b576cb7247570d7c01380771cc717af946708591ee49a155185d9f7f70a699a1f92
6
+ metadata.gz: d0e7c31c72337e4c8f431052f4015d6fc8f7f7e45a50480aec5dee2f4c9a1f6e3111dd99dee46acd8ea63e06a0aab100d617849f7d1fe6716dae1389df4ae763
7
+ data.tar.gz: b3e976d2d51adf61137188a84218622d559d5c26356c29d662fe38ade816181a4fbd8724f75e050a0db9ac0517588830aa678d38748951957bda49f83c0add07
@@ -15,15 +15,20 @@ jobs:
15
15
  steps:
16
16
  - uses: actions/checkout@v1
17
17
 
18
- - uses: actions/setup-ruby@v1
18
+ - uses: ruby/setup-ruby@v1
19
19
  with:
20
- ruby-version: 3.0.2
20
+ ruby-version: 3.1.2
21
21
 
22
22
  - name: Bundle Install
23
23
  run: |
24
24
  gem install bundler
25
25
  bundle install --jobs 4 --retry 3
26
26
 
27
+ - name: RuboCop Linter Action
28
+ uses: andrewmcodes/rubocop-linter-action@v3.3.0
29
+ env:
30
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31
+
27
32
  - name: Run RSpec and Code Coverage
28
33
  uses: paambaati/codeclimate-action@v2.4.0
29
34
  env:
@@ -3,7 +3,7 @@ name: Publish
3
3
  on:
4
4
  push:
5
5
  tags:
6
- - 'v*'
6
+ - "v*"
7
7
 
8
8
  jobs:
9
9
  build:
@@ -12,15 +12,20 @@ jobs:
12
12
  steps:
13
13
  - uses: actions/checkout@v1
14
14
 
15
- - uses: actions/setup-ruby@v1
15
+ - uses: ruby/setup-ruby@v1
16
16
  with:
17
- ruby-version: 3.0.2
17
+ ruby-version: 3.1.2
18
18
 
19
19
  - name: Bundle Install
20
20
  run: |
21
21
  gem install bundler
22
22
  bundle install --jobs 4 --retry 3
23
23
 
24
+ - name: RuboCop Linter Action
25
+ uses: andrewmcodes/rubocop-linter-action@v3.3.0
26
+ env:
27
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28
+
24
29
  - name: Run RSpec and Code Coverage
25
30
  uses: paambaati/codeclimate-action@v2.4.0
26
31
  env:
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://docs.rubocop.org/rubocop/configuration
11
+
12
+ AllCops:
13
+ TargetRubyVersion: 2.5
14
+ NewCops: enable
15
+
16
+ Metrics/BlockLength:
17
+ Enabled: false
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Gemspec/RequireMFA:
23
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.2
1
+ 3.1.2
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
- #ruby=ruby-3.0.2
2
- #ruby-gemset=triviacrack
1
+ # frozen_string_literal: true
3
2
 
4
3
  source 'https://rubygems.org'
5
4
 
data/Gemfile.lock CHANGED
@@ -1,55 +1,59 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- triviacrack (0.5.1)
5
- faraday (~> 1.7.2)
4
+ triviacrack (0.8.0)
5
+ faraday (~> 2.3.0)
6
+ json (~> 2.6.2)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- diff-lcs (1.4.4)
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
11
13
  docile (1.4.0)
12
- faraday (1.7.2)
13
- faraday-em_http (~> 1.0)
14
- faraday-em_synchrony (~> 1.0)
15
- faraday-excon (~> 1.1)
16
- faraday-httpclient (~> 1.0.1)
17
- faraday-net_http (~> 1.0)
18
- faraday-net_http_persistent (~> 1.1)
19
- faraday-patron (~> 1.0)
20
- faraday-rack (~> 1.0)
21
- multipart-post (>= 1.2, < 3)
14
+ faraday (2.3.0)
15
+ faraday-net_http (~> 2.0)
22
16
  ruby2_keywords (>= 0.0.4)
23
- faraday-em_http (1.0.0)
24
- faraday-em_synchrony (1.0.0)
25
- faraday-excon (1.1.0)
26
- faraday-httpclient (1.0.1)
27
- faraday-net_http (1.0.1)
28
- faraday-net_http_persistent (1.2.0)
29
- faraday-patron (1.0.0)
30
- faraday-rack (1.0.0)
17
+ faraday-net_http (2.0.3)
31
18
  fuubar (2.5.1)
32
19
  rspec-core (~> 3.0)
33
20
  ruby-progressbar (~> 1.4)
34
- json (2.5.1)
35
- multipart-post (2.1.1)
21
+ json (2.6.2)
22
+ parallel (1.22.1)
23
+ parser (3.1.2.0)
24
+ ast (~> 2.4.1)
25
+ rainbow (3.1.1)
36
26
  rake (13.0.6)
37
- rspec (3.10.0)
38
- rspec-core (~> 3.10.0)
39
- rspec-expectations (~> 3.10.0)
40
- rspec-mocks (~> 3.10.0)
41
- rspec-core (3.10.1)
42
- rspec-support (~> 3.10.0)
43
- rspec-expectations (3.10.1)
27
+ regexp_parser (2.5.0)
28
+ rexml (3.2.5)
29
+ rspec (3.11.0)
30
+ rspec-core (~> 3.11.0)
31
+ rspec-expectations (~> 3.11.0)
32
+ rspec-mocks (~> 3.11.0)
33
+ rspec-core (3.11.0)
34
+ rspec-support (~> 3.11.0)
35
+ rspec-expectations (3.11.0)
44
36
  diff-lcs (>= 1.2.0, < 2.0)
45
- rspec-support (~> 3.10.0)
37
+ rspec-support (~> 3.11.0)
46
38
  rspec-its (1.3.0)
47
39
  rspec-core (>= 3.0.0)
48
40
  rspec-expectations (>= 3.0.0)
49
- rspec-mocks (3.10.2)
41
+ rspec-mocks (3.11.1)
50
42
  diff-lcs (>= 1.2.0, < 2.0)
51
- rspec-support (~> 3.10.0)
52
- rspec-support (3.10.2)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ rubocop (1.31.2)
46
+ json (~> 2.3)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.1.0.0)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml (>= 3.2.5, < 4.0)
52
+ rubocop-ast (>= 1.18.0, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.19.1)
56
+ parser (>= 3.1.1.0)
53
57
  ruby-progressbar (1.11.0)
54
58
  ruby2_keywords (0.0.5)
55
59
  simplecov (0.21.2)
@@ -57,20 +61,21 @@ GEM
57
61
  simplecov-html (~> 0.11)
58
62
  simplecov_json_formatter (~> 0.1)
59
63
  simplecov-html (0.12.3)
60
- simplecov_json_formatter (0.1.3)
64
+ simplecov_json_formatter (0.1.4)
65
+ unicode-display_width (2.2.0)
61
66
 
62
67
  PLATFORMS
63
68
  ruby
64
69
 
65
70
  DEPENDENCIES
66
- bundler (~> 2.2.27)
71
+ bundler (~> 2.3.18)
67
72
  fuubar (~> 2.5.1)
68
- json (~> 2.5.1)
69
73
  rake (~> 13.0.6)
70
- rspec (~> 3.10.0)
74
+ rspec (~> 3.11.0)
71
75
  rspec-its (~> 1.3.0)
76
+ rubocop (~> 1.31.2)
72
77
  simplecov (~> 0.21.2)
73
78
  triviacrack!
74
79
 
75
80
  BUNDLED WITH
76
- 2.2.27
81
+ 2.3.18
data/Rakefile CHANGED
@@ -1,7 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
2
4
 
3
5
  begin
4
6
  require 'rspec/core/rake_task'
5
7
  RSpec::Core::RakeTask.new(:spec)
6
- rescue LoadError
8
+ rescue LoadError # rubocop:disable Lint/SuppressedException
7
9
  end
@@ -1,14 +1,15 @@
1
- require "triviacrack/api/game"
2
- require "triviacrack/api/login"
3
- require "triviacrack/api/profile"
4
- require "triviacrack/api/question"
5
- require "triviacrack/api/user"
1
+ # frozen_string_literal: true
2
+
3
+ require 'triviacrack/api/game'
4
+ require 'triviacrack/api/login'
5
+ require 'triviacrack/api/profile'
6
+ require 'triviacrack/api/question'
7
+ require 'triviacrack/api/user'
6
8
 
7
9
  # Public: Interface for interacting with the Trivia Crack API.
8
10
  module TriviaCrack
9
11
  module API
10
12
  class Client
11
-
12
13
  include TriviaCrack::API::Game
13
14
  include TriviaCrack::API::Login
14
15
  include TriviaCrack::API::User
@@ -25,7 +26,6 @@ module TriviaCrack
25
26
  @session = TriviaCrack::Session.new session_id: session_id,
26
27
  user_id: user_id
27
28
  end
28
-
29
29
  end
30
30
  end
31
31
  end
@@ -1,12 +1,14 @@
1
- require "faraday"
1
+ # frozen_string_literal: true
2
2
 
3
- require "triviacrack/errors/request_error"
3
+ require 'json'
4
+ require 'faraday'
5
+
6
+ require 'triviacrack/errors/request_error'
4
7
 
5
8
  # Internal: Common methods used for the Trivia Crack API.
6
9
  module TriviaCrack
7
10
  module API
8
11
  module Common
9
-
10
12
  # Internal: Makes a GET request to the Trivia Crack API using the set of
11
13
  # default headers.
12
14
  #
@@ -16,8 +18,7 @@ module TriviaCrack
16
18
  # Returns a Faraday Response object with the server's response.
17
19
  # Raises TriviaCrack:Errors::RequestError if the request fails.
18
20
  def get(url, parameters: nil)
19
- response = Faraday.get "#{API_HOST}#{url}", params: parameters,
20
- headers: default_headers
21
+ response = Faraday.get "#{API_HOST}#{url}", parameters, default_headers
21
22
 
22
23
  check_response url, response
23
24
  end
@@ -31,8 +32,7 @@ module TriviaCrack
31
32
  # Returns a Faraday Response object with the server's response.
32
33
  # Raises TriviaCrack:Errors::RequestError if the request fails.
33
34
  def post(url, parameters: nil)
34
- response = Faraday.post "#{API_HOST}#{url}", body: parameters,
35
- headers: default_headers
35
+ response = Faraday.post "#{API_HOST}#{url}", parameters, default_headers
36
36
 
37
37
  check_response url, response
38
38
  end
@@ -40,18 +40,16 @@ module TriviaCrack
40
40
  private
41
41
 
42
42
  # Internal: The host name of the Trivia Crack API.
43
- API_HOST = "https://api.preguntados.com"
43
+ API_HOST = 'https://api.preguntados.com'
44
44
 
45
45
  # Internal: Constructs the set of headers needed to make requests to the
46
46
  # Trivia Crack API.
47
47
  #
48
48
  # Returns a hash of headers.
49
49
  def default_headers
50
- headers = { "Content-Type": "application/json; charset=utf-8" }
50
+ headers = { 'Content-Type': 'application/json; charset=utf-8' }
51
51
 
52
- if @session
53
- headers["Cookie"] = "ap_session=#{@session.session_id}"
54
- end
52
+ headers['Cookie'] = "ap_session=#{@session.session_id}" if @session
55
53
 
56
54
  headers
57
55
  end
@@ -61,15 +59,15 @@ module TriviaCrack
61
59
  #
62
60
  # response - Faraday response returned by the API.
63
61
  #
64
- # Returns the response object.
62
+ # Returns the response body parsed from JSON
65
63
  # Raises TriviaCrack:Errors::RequestError if the request failed.
66
64
  def check_response(url, response)
67
- if not response.status.between? 200, 299
65
+ unless response.status.between? 200, 299
68
66
  raise TriviaCrack::Errors::RequestError.new(response.status, url, response.body),
69
- "Request to #{API_HOST}#{url} failed with code #{response.status}."
67
+ "Request to #{API_HOST}#{url} failed with code #{response.status}."
70
68
  end
71
69
 
72
- response
70
+ JSON.parse(response.body)
73
71
  end
74
72
  end
75
73
  end
@@ -1,12 +1,14 @@
1
- require "triviacrack/api/common"
2
- require "triviacrack/parsers/game_parser"
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'triviacrack/api/common'
5
+ require 'triviacrack/parsers/game_parser'
3
6
 
4
7
  # Public: All methods in this module make requests to the Trivia Crack games
5
8
  # API.
6
9
  module TriviaCrack
7
10
  module API
8
11
  module Game
9
-
10
12
  include TriviaCrack::API::Common
11
13
 
12
14
  # Public: Uses the Trivia Crack API to fetch the list of games for the
@@ -18,17 +20,15 @@ module TriviaCrack
18
20
  #
19
21
  # Returns a list of TriviaCrack::Game.
20
22
  # Raises TriviaCrack:Errors::RequestError if the request fails.
21
- def get_games
23
+ def get_games # rubocop:disable Naming/AccessorMethodName
22
24
  response = get "/api/users/#{@session.user_id}/dashboard"
23
25
 
24
- games_data = response.body["list"]
26
+ games_data = response['list']
25
27
 
26
28
  games = []
27
- if games_data
28
- games_data.each do |game_data|
29
- game = TriviaCrack::Parsers::GameParser.parse game_data
30
- games << game
31
- end
29
+ games_data&.each do |game_data|
30
+ game = TriviaCrack::Parsers::GameParser.parse game_data
31
+ games << game
32
32
  end
33
33
 
34
34
  games
@@ -48,7 +48,7 @@ module TriviaCrack
48
48
  def get_game(game_id)
49
49
  response = get "/api/users/#{@session.user_id}/games/#{game_id}"
50
50
 
51
- TriviaCrack::Parsers::GameParser.parse response.body
51
+ TriviaCrack::Parsers::GameParser.parse response
52
52
  end
53
53
 
54
54
  # Public: Uses the Trivia Crack API to start a new game for the current
@@ -62,11 +62,10 @@ module TriviaCrack
62
62
  # Raises TriviaCrack::Errors::RequestError if the request fails
63
63
  def start_new_game
64
64
  response = post "/api/users/#{@session.user_id}/games",
65
- parameters: { language: "EN" }.to_s
65
+ parameters: { language: 'EN' }.to_json
66
66
 
67
- TriviaCrack::Parsers::GameParser.parse response.body
67
+ TriviaCrack::Parsers::GameParser.parse response
68
68
  end
69
-
70
69
  end
71
70
  end
72
71
  end
@@ -1,12 +1,14 @@
1
- require "triviacrack/api/common"
2
- require "triviacrack/parsers/session_parser"
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'triviacrack/api/common'
5
+ require 'triviacrack/parsers/session_parser'
3
6
 
4
7
  # Public: All methods in this module make requests to the Trivia Crack login
5
8
  # API.
6
9
  module TriviaCrack
7
10
  module API
8
11
  module Login
9
-
10
12
  include TriviaCrack::API::Common
11
13
 
12
14
  # Public: Uses the given email and password to log in to Trivia Crack
@@ -22,14 +24,12 @@ module TriviaCrack
22
24
  # Returns the user_id and username of the user that has logged in.
23
25
  # Raises TriviaCrack::Errors::RequestError if the request fails.
24
26
  def login(email, password)
25
- response = post "/api/login", parameters: { email: email,
27
+ response = post '/api/login', parameters: { email: email,
26
28
  password: password,
27
- language: "en"
28
- }.to_s
29
+ language: 'en' }.to_json
29
30
 
30
- @session = TriviaCrack::Parsers::SessionParser.parse response.body
31
+ @session = TriviaCrack::Parsers::SessionParser.parse response
31
32
  end
32
-
33
33
  end
34
34
  end
35
35
  end
@@ -1,10 +1,11 @@
1
- require "triviacrack/api/common"
1
+ # frozen_string_literal: true
2
+
3
+ require 'triviacrack/api/common'
2
4
 
3
5
  # Public: Interface to the Trivia Crack Profile API.
4
6
  module TriviaCrack
5
7
  module API
6
8
  module Profile
7
-
8
9
  include TriviaCrack::API::Common
9
10
 
10
11
  # Public: Uses the Trivia Crack API to get the profile of the user with
@@ -21,7 +22,7 @@ module TriviaCrack
21
22
  def get_profile(user_id)
22
23
  response = get "/api/users/#{@session.user_id}/profiles/#{user_id}"
23
24
 
24
- TriviaCrack::Parsers::ProfileParser.parse response.body
25
+ TriviaCrack::Parsers::ProfileParser.parse response
25
26
  end
26
27
 
27
28
  # Public: Uses the Trivia Crack API to get the profile of current user.
@@ -32,13 +33,12 @@ module TriviaCrack
32
33
  #
33
34
  # Returns the TriviaCrack::Profile for the current user.
34
35
  # Raises TriviaCrack::Errors::RequestError if the request fails.
35
- def get_my_profile
36
+ def get_my_profile # rubocop:disable Naming/AccessorMethodName
36
37
  response =
37
38
  get "/api/users/#{@session.user_id}/profiles/#{@session.user_id}"
38
39
 
39
- TriviaCrack::Parsers::ProfileParser.parse response.body
40
+ TriviaCrack::Parsers::ProfileParser.parse response
40
41
  end
41
-
42
42
  end
43
43
  end
44
44
  end
@@ -1,12 +1,14 @@
1
- require "triviacrack/api/common"
2
- require "triviacrack/parsers/game_parser"
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'triviacrack/api/common'
5
+ require 'triviacrack/parsers/game_parser'
3
6
 
4
7
  # Public: All methods in this module make requests to the Trivia Crack answers
5
8
  # API.
6
9
  module TriviaCrack
7
10
  module API
8
11
  module Question
9
-
10
12
  include TriviaCrack::API::Common
11
13
 
12
14
  # Public: Uses the Trivia Crack API to answer the given question.
@@ -25,15 +27,14 @@ module TriviaCrack
25
27
  def answer_question(game_id, question, answer)
26
28
  response =
27
29
  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_s
30
+ parameters: { type: question.type.upcase,
31
+ answers: [{
32
+ id: question.id,
33
+ answer: answer,
34
+ category: question.category.upcase
35
+ }] }.to_json
35
36
 
36
- game = TriviaCrack::Parsers::GameParser.parse response.body
37
+ game = TriviaCrack::Parsers::GameParser.parse response
37
38
 
38
39
  [game, answer == question.correct_answer]
39
40
  end
@@ -56,11 +57,11 @@ module TriviaCrack
56
57
  # with an entry for each question ID and a boolean indicating if
57
58
  # the question was answered successfully.
58
59
  # Raises TriviaCrack::Errors::RequestError if the request fails
59
- def answer_questions(game_id, questions, answer_map)
60
+ def answer_questions(game_id, questions, answer_map) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
60
61
  answers = []
61
62
  correct_answers = {}
62
63
 
63
- for question in questions do
64
+ questions.each do |question|
64
65
  answer = {
65
66
  id: question.id,
66
67
  answer: answer_map[question.id],
@@ -72,13 +73,12 @@ module TriviaCrack
72
73
 
73
74
  response =
74
75
  post "/api/users/#{@session.user_id}/games/#{game_id}/answers",
75
- parameters: { type: questions.first.type.upcase, answers: answers }.to_s
76
+ parameters: { type: questions.first.type.upcase, answers: answers }.to_json
76
77
 
77
- game = TriviaCrack::Parsers::GameParser.parse response.body
78
+ game = TriviaCrack::Parsers::GameParser.parse response
78
79
 
79
80
  [game, correct_answers]
80
81
  end
81
-
82
82
  end
83
83
  end
84
84
  end
@@ -1,12 +1,13 @@
1
- require "triviacrack/api/common"
2
- require "triviacrack/parsers/user_parser"
1
+ # frozen_string_literal: true
2
+
3
+ require 'triviacrack/api/common'
4
+ require 'triviacrack/parsers/user_parser'
3
5
 
4
6
  # Public: All methods in this module make requests to the Trivia Crack users
5
7
  # API.
6
8
  module TriviaCrack
7
9
  module API
8
10
  module User
9
-
10
11
  include TriviaCrack::API::Common
11
12
 
12
13
  # Public: Uses the Trivia Crack Search API to find the numeric user id of
@@ -22,18 +23,14 @@ module TriviaCrack
22
23
  # Raises TriviaCrack:Errors::RequestError if the request fails.
23
24
  def get_user_id(username)
24
25
  # Trim the @ character from the start of the username
25
- if username.start_with? "@"
26
- username = username[1..-1]
27
- end
26
+ username = username[1..-1] if username.start_with? '@'
28
27
 
29
- response = get "/api/search?username=#{username}"
30
-
31
- body = response.body
28
+ body = get "/api/search?username=#{username}"
32
29
 
33
30
  user_id = false
34
- body["list"].each do |user|
35
- if user["username"] == username
36
- user_id = user["id"]
31
+ body['list'].each do |user|
32
+ if user['username'] == username
33
+ user_id = user['id']
37
34
  break
38
35
  end
39
36
  end
@@ -45,12 +42,11 @@ module TriviaCrack
45
42
  #
46
43
  # Returns a TriviaCrack::User representing the current user.
47
44
  # Raises TriviaCrack::Errors::RequestError if the request fails
48
- def get_user
45
+ def get_user # rubocop:disable Naming/AccessorMethodName
49
46
  response = get "/api/users/#{@session.user_id}"
50
47
 
51
- TriviaCrack::Parsers::UserParser.parse response.body
48
+ TriviaCrack::Parsers::UserParser.parse response
52
49
  end
53
-
54
50
  end
55
51
  end
56
52
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Public: An object representing category statistics for a player within a
2
4
  # single Trivia Crack game.
3
5
  module TriviaCrack
4
6
  class CategoryStatistics
5
-
6
7
  # Public: The category for these statistics (:arts, :geography, etc).
7
8
  attr_reader :category
8
9
 
@@ -22,6 +23,5 @@ module TriviaCrack
22
23
  @incorrect = incorrect
23
24
  @worst = worst
24
25
  end
25
-
26
26
  end
27
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Public: This error is raised when a block of data cannot be correctly parsed.
2
4
  module TriviaCrack
3
5
  module Errors
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Public: This error is raised when a web request to the Trivia Crack API is
2
4
  # unsuccessful.
3
5
  module TriviaCrack
4
6
  module Errors
5
7
  class RequestError < StandardError
6
-
7
8
  # Public: The HTTP status code returned by the Trivia Crack server.
8
9
  attr_reader :code
9
10
 
10
- #Public: The URL that was requested.
11
+ # Public: The URL that was requested.
11
12
  attr_reader :url
12
13
 
13
- #Public: The response body that was returned by the Trivia Crack server.
14
+ # Public: The response body that was returned by the Trivia Crack server.
14
15
  attr_reader :body
15
16
 
16
17
  # Public: Initializes a RequestError.
@@ -19,6 +20,7 @@ module TriviaCrack
19
20
  # url - The URL that was requested.
20
21
  # body - The response body that was returned by the Trivia Crack server.
21
22
  def initialize(code, url, body)
23
+ super()
22
24
  @code = code
23
25
  @url = url
24
26
  @body = body