triviacrack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac03c72d98815202ea81c52004831d9110177381
4
+ data.tar.gz: fcb7b96af0d10e97e4fa1410dbb70d6b3da2f5f8
5
+ SHA512:
6
+ metadata.gz: 8cad6503fb7126a1ac00ebd716b54c2ad6c14432d405f4bcf7600dea9064755d0d69d7a4484ac5de5dbf77512a6eb7181672a4b57e60d6cdda4e636789445df7
7
+ data.tar.gz: 72abb5306d502187d8ada14bb9b218cefc747120f4815f6deb25c05b8871a85d52ec10190e43050de26a33829f96b1581c855919857c52a7c7d3e890178e34e0
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.4.1
5
+
6
+ env:
7
+ global:
8
+ - secure: FBRKEwZitjHKzgrugvvp8RbI6CJvAhChsHiI5NfbD9SIjogfmw38Qj503PvPw1h5hrYtrgeLmgFrve/ridbDDLxhN3voGyZ5ugsPmb5vITRtAi2WTI0b8ylrvKOuf/TsyPoENZ7LzLCbblJpJtREgU8UGosEe3BhZ/h6sy/T07Q=
9
+
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - "./cc-test-reporter before-build"
14
+
15
+ script:
16
+ - bundle exec rspec
17
+
18
+ after_script:
19
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ #ruby=ruby-2.4.1
2
+ #ruby-gemset=triviacrack
3
+
4
+ source 'https://rubygems.org'
5
+
6
+ # Specify your gem's dependencies in triviacrack.gemspec
7
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 David Kus
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,120 @@
1
+ # Triviacrack [![Build Status](https://travis-ci.org/davidkus/triviacrack.svg?branch=master)](https://travis-ci.org/davidkus/triviacrack) [![Code Climate](https://codeclimate.com/github/davidkus/triviacrack/badges/gpa.svg)](https://codeclimate.com/github/davidkus/triviacrack) [![Test Coverage](https://codeclimate.com/github/davidkus/triviacrack/badges/coverage.svg)](https://codeclimate.com/github/davidkus/triviacrack)
2
+
3
+ A Ruby interface for the Trivia Crack API.
4
+
5
+ The Trivia Crack iOS app uses an undocumented API to store / retrieve
6
+ information about the game state. This Ruby library wraps that API and presents
7
+ it in a clean, documented way.
8
+
9
+ *Disclaimer*: The Trivia Crack API is undocumented and subject to change at any
10
+ time. Changes in the API _may_ break this library.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem "triviacrack", github: "davidkus/triviacrack"
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ ## Usage
25
+
26
+ First, create an instance of the `TriviaCrack::API::Client`.
27
+
28
+ ```ruby
29
+ require "triviacrack"
30
+
31
+ client = TriviaCrack::API::Client.new
32
+ ```
33
+
34
+ ### Logging In
35
+
36
+ Use the client to log in using your Trivia Crack email and password.
37
+
38
+ ```ruby
39
+ client.login "user@example.com", "password123"
40
+ ```
41
+
42
+ ### User Information
43
+
44
+ You can retrieve information about the currently logged in user.
45
+
46
+ ```ruby
47
+ user = client.get_user
48
+
49
+ puts "Hello, #{user.username}!"
50
+ # => Hello, david!
51
+ ```
52
+
53
+ You can also retrieve the user ID of the user with a given username.
54
+
55
+ ```ruby
56
+ user_id = client.get_user_id "david"
57
+
58
+ puts "david's user id is #{user_id}"
59
+ # => david's user id is 1
60
+ ```
61
+
62
+ ### User Profiles
63
+
64
+ You can retrieve additional information about a user by fetching their profile.
65
+
66
+ ```ruby
67
+ user_id = 123
68
+ profile = client.get_profile user_id
69
+ ```
70
+
71
+ A user's profile contains their statistics (number of wins / losses, number of
72
+ questions answered correctly, etc), among other things.
73
+
74
+ ### Game Information
75
+
76
+ You can retrieve the list of games available to the currently logged in user.
77
+
78
+ ```ruby
79
+ games = client.get_games
80
+ ```
81
+
82
+ You can retrieve game information for a specific game (by id).
83
+
84
+ ```ruby
85
+ game = client.get_game 1
86
+ ```
87
+
88
+ You can also start a new game.
89
+
90
+ ```ruby
91
+ game = client.start_new_game
92
+ ```
93
+
94
+ The TriviaCrack::Game object holds information about the opponent, statistics,
95
+ and current questions available to be answered.
96
+
97
+ ### Answering Questions
98
+
99
+ It is possible to answer questions using the Trivia Crack API.
100
+
101
+ ```ruby
102
+ game = client.get_game 1
103
+
104
+ client.answer_question game.id, game.question.first, 0
105
+ ```
106
+
107
+ The `answer_question` also returns an updated `TriviaCrack::Game` object, so you
108
+ can avoid making additional API calls to keep the game object up to date.
109
+
110
+ ```ruby
111
+ game = client.answer_question game.id, game.question.first, 0
112
+ ```
113
+
114
+ ## Contributing
115
+
116
+ 1. Fork it ( https://github.com/davidkus/triviacrack/fork )
117
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
118
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
119
+ 4. Push to the branch (`git push origin my-new-feature`)
120
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
@@ -0,0 +1,12 @@
1
+ require "triviacrack/category_statistics"
2
+ require "triviacrack/game"
3
+ require "triviacrack/game_statistics"
4
+ require "triviacrack/profile"
5
+ require "triviacrack/question"
6
+ require "triviacrack/user"
7
+ require "triviacrack/version"
8
+
9
+ require "triviacrack/api/client"
10
+
11
+ require "triviacrack/errors/parse_error"
12
+ require "triviacrack/errors/request_error"
@@ -0,0 +1,31 @@
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"
6
+
7
+ # Public: Interface for interacting with the Trivia Crack API.
8
+ module TriviaCrack
9
+ module API
10
+ class Client
11
+
12
+ include TriviaCrack::API::Game
13
+ include TriviaCrack::API::Login
14
+ include TriviaCrack::API::User
15
+ include TriviaCrack::API::Question
16
+ include TriviaCrack::API::Profile
17
+
18
+ # Public: Creates a new TriviaCrack::Session object using the given
19
+ # session_id and user_id and assigns that session to this
20
+ # TriviaCrack::API::Client.
21
+ #
22
+ # session_id - The Trivia Crack session ID.
23
+ # user_id - The Trivia Crack user ID.
24
+ def set_session(session_id, user_id)
25
+ @session = TriviaCrack::Session.new session_id: session_id,
26
+ user_id: user_id
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,76 @@
1
+ require "unirest"
2
+
3
+ require "triviacrack/errors/request_error"
4
+
5
+ # Internal: Common methods used for the Trivia Crack API.
6
+ module TriviaCrack
7
+ module API
8
+ module Common
9
+
10
+ # Internal: Makes a GET request to the Trivia Crack API using the set of
11
+ # default headers.
12
+ #
13
+ # url - The URL of the TriviaCrack API resource.
14
+ # parameters - The parameters to send with the request.
15
+ #
16
+ # Returns a Unirest Response object with the server's response.
17
+ # Raises TriviaCrack:Errors::RequestError if the request fails.
18
+ def get(url, parameters: nil)
19
+ response = Unirest.get "#{API_HOST}#{url}", parameters: parameters,
20
+ headers: default_headers
21
+
22
+ check_response response
23
+ end
24
+
25
+ # Internal: Makes a POST request to the Trivia Crack API using the set of
26
+ # default headers.
27
+ #
28
+ # url - The URL of the TriviaCrack API resource.
29
+ # parameters - The parameters to send with the request.
30
+ #
31
+ # Returns a Unirest Response object with the server's response.
32
+ # Raises TriviaCrack:Errors::RequestError if the request fails.
33
+ def post(url, parameters: nil)
34
+ response = Unirest.post "#{API_HOST}#{url}", parameters: parameters,
35
+ headers: default_headers
36
+
37
+ check_response response
38
+ end
39
+
40
+ private
41
+
42
+ # Internal: The host name of the Trivia Crack API.
43
+ API_HOST = "https://api.preguntados.com"
44
+
45
+ # Internal: Constructs the set of headers needed to make requests to the
46
+ # Trivia Crack API.
47
+ #
48
+ # Returns a hash of headers.
49
+ def default_headers
50
+ headers = { "Content-Type": "application/json; charset=utf-8" }
51
+
52
+ if @session
53
+ headers["Cookie"] = "ap_session=#{@session.session_id}"
54
+ end
55
+
56
+ headers
57
+ end
58
+
59
+ # Internal: Checks the response's code to see if the request was
60
+ # successful
61
+ #
62
+ # response - Unirest response returned by the API.
63
+ #
64
+ # Returns the response object.
65
+ # Raises TriviaCrack:Errors::RequestError if the request failed.
66
+ def check_response(response)
67
+ if not response.code.between? 200, 299
68
+ raise TriviaCrack::Errors::RequestError.new(response.code),
69
+ "Request to the Trivia Crack API failed."
70
+ end
71
+
72
+ response
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,72 @@
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 games
5
+ # API.
6
+ module TriviaCrack
7
+ module API
8
+ module Game
9
+
10
+ include TriviaCrack::API::Common
11
+
12
+ # Public: Uses the Trivia Crack API to fetch the list of games for the
13
+ # current user.
14
+ #
15
+ # Examples
16
+ #
17
+ # get_games
18
+ #
19
+ # Returns a list of TriviaCrack::Game.
20
+ # Raises TriviaCrack:Errors::RequestError if the request fails.
21
+ def get_games
22
+ response = get "/api/users/#{@session.user_id}/dashboard"
23
+
24
+ games_data = response.body["list"]
25
+
26
+ 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
32
+ end
33
+
34
+ games
35
+ end
36
+
37
+ # Public: Uses the Trivia Crack API to fetch the information for the given
38
+ # game.
39
+ #
40
+ # game_id - The id of a Trivia Crack game.
41
+ #
42
+ # Examples
43
+ #
44
+ # get_game 123
45
+ #
46
+ # Returns the TriviaCrack::Game for the given game_id.
47
+ # Raises TriviaCrack:Errors::RequestError if the request fails.
48
+ def get_game(game_id)
49
+ response = get "/api/users/#{@session.user_id}/games/#{game_id}"
50
+
51
+ TriviaCrack::Parsers::GameParser.parse response.body
52
+ end
53
+
54
+ # Public: Uses the Trivia Crack API to start a new game for the current
55
+ # user.
56
+ #
57
+ # Examples
58
+ #
59
+ # client.start_new_game
60
+ #
61
+ # Returns the TriviaCrack::Game that was started.
62
+ # Raises TriviaCrack::Errors::RequestError if the request fails
63
+ def start_new_game
64
+ response = post "/api/users/#{@session.user_id}/games",
65
+ parameters: { language: "EN" }.to_json
66
+
67
+ TriviaCrack::Parsers::GameParser.parse response.body
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,35 @@
1
+ require "triviacrack/api/common"
2
+ require "triviacrack/parsers/session_parser"
3
+
4
+ # Public: All methods in this module make requests to the Trivia Crack login
5
+ # API.
6
+ module TriviaCrack
7
+ module API
8
+ module Login
9
+
10
+ include TriviaCrack::API::Common
11
+
12
+ # Public: Uses the given email and password to log in to Trivia Crack
13
+ # and retrieve a session id and user id.
14
+ #
15
+ # email - Email of a Trivia Crack user.
16
+ # password - Password of a Trivia Crack user.
17
+ #
18
+ # Examples
19
+ #
20
+ # login "user@example.com", "password123"
21
+ #
22
+ # Returns the user_id and username of the user that has logged in.
23
+ # Raises TriviaCrack::Errors::RequestError if the request fails.
24
+ def login(email, password)
25
+ response = post "/api/login", parameters: { email: email,
26
+ password: password,
27
+ language: "en"
28
+ }.to_json
29
+
30
+ @session = TriviaCrack::Parsers::SessionParser.parse response.body
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,44 @@
1
+ require "triviacrack/api/common"
2
+
3
+ # Public: Interface to the Trivia Crack Profile API.
4
+ module TriviaCrack
5
+ module API
6
+ module Profile
7
+
8
+ include TriviaCrack::API::Common
9
+
10
+ # Public: Uses the Trivia Crack API to get the profile of the user with
11
+ # the given user id.
12
+ #
13
+ # user_id - The unique identifier of the user.
14
+ #
15
+ # Examples
16
+ #
17
+ # profile = client.get_profile 123
18
+ #
19
+ # Returns the TriviaCrack::Profile for the given user.
20
+ # Raises TriviaCrack::Errors::RequestError if the request fails.
21
+ def get_profile(user_id)
22
+ response = get "/api/users/#{@session.user_id}/profiles/#{user_id}"
23
+
24
+ TriviaCrack::Parsers::ProfileParser.parse response.body
25
+ end
26
+
27
+ # Public: Uses the Trivia Crack API to get the profile of current user.
28
+ #
29
+ # Examples
30
+ #
31
+ # profile = client.get_my_profile
32
+ #
33
+ # Returns the TriviaCrack::Profile for the current user.
34
+ # Raises TriviaCrack::Errors::RequestError if the request fails.
35
+ def get_my_profile
36
+ response =
37
+ get "/api/users/#{@session.user_id}/profiles/#{@session.user_id}"
38
+
39
+ TriviaCrack::Parsers::ProfileParser.parse response.body
40
+ end
41
+
42
+ end
43
+ end
44
+ end