chgk_rating 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.github/CODE_OF_CONDUCT.md +46 -0
  3. data/.github/ISSUE_TEMPLATE.md +13 -0
  4. data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
  5. data/CHANGELOG.md +17 -0
  6. data/CONTRIBUTING.md +13 -0
  7. data/Gemfile +7 -0
  8. data/LICENSE +22 -0
  9. data/README.md +557 -0
  10. data/Rakefile +13 -0
  11. data/chgk_rating.gemspec +29 -0
  12. data/lib/chgk_rating.rb +53 -0
  13. data/lib/chgk_rating/attribute_mappings.rb +138 -0
  14. data/lib/chgk_rating/chgk_object.rb +13 -0
  15. data/lib/chgk_rating/client.rb +173 -0
  16. data/lib/chgk_rating/collections/base.rb +79 -0
  17. data/lib/chgk_rating/collections/players.rb +17 -0
  18. data/lib/chgk_rating/collections/ratings.rb +23 -0
  19. data/lib/chgk_rating/collections/recaps.rb +25 -0
  20. data/lib/chgk_rating/collections/teams.rb +17 -0
  21. data/lib/chgk_rating/collections/tournament_players.rb +24 -0
  22. data/lib/chgk_rating/collections/tournament_team_results.rb +24 -0
  23. data/lib/chgk_rating/collections/tournament_teams.rb +22 -0
  24. data/lib/chgk_rating/collections/tournaments.rb +47 -0
  25. data/lib/chgk_rating/concerns/searching.rb +21 -0
  26. data/lib/chgk_rating/connection.rb +19 -0
  27. data/lib/chgk_rating/error.rb +46 -0
  28. data/lib/chgk_rating/models/base.rb +90 -0
  29. data/lib/chgk_rating/models/player.rb +11 -0
  30. data/lib/chgk_rating/models/rating.rb +19 -0
  31. data/lib/chgk_rating/models/recap.rb +19 -0
  32. data/lib/chgk_rating/models/team.rb +62 -0
  33. data/lib/chgk_rating/models/tournament.rb +46 -0
  34. data/lib/chgk_rating/models/tournament_player.rb +8 -0
  35. data/lib/chgk_rating/models/tournament_team.rb +29 -0
  36. data/lib/chgk_rating/models/tournament_team_result.rb +8 -0
  37. data/lib/chgk_rating/request.rb +32 -0
  38. data/lib/chgk_rating/utils/snakecase.rb +16 -0
  39. data/lib/chgk_rating/utils/transformations.rb +83 -0
  40. data/lib/chgk_rating/version.rb +3 -0
  41. data/lib/ext/date.rb +11 -0
  42. data/lib/ext/date_time.rb +5 -0
  43. data/lib/ext/uri.rb +9 -0
  44. data/spec/lib/chgk_rating/client_spec.rb +198 -0
  45. data/spec/lib/chgk_rating/collections/players_spec.rb +45 -0
  46. data/spec/lib/chgk_rating/collections/ratings_spec.rb +25 -0
  47. data/spec/lib/chgk_rating/collections/recaps_spec.rb +21 -0
  48. data/spec/lib/chgk_rating/collections/teams_spec.rb +42 -0
  49. data/spec/lib/chgk_rating/collections/tournament_players_spec.rb +18 -0
  50. data/spec/lib/chgk_rating/collections/tournament_team_results_spec.rb +17 -0
  51. data/spec/lib/chgk_rating/collections/tournament_teams_spec.rb +58 -0
  52. data/spec/lib/chgk_rating/collections/tournaments_spec.rb +62 -0
  53. data/spec/lib/chgk_rating/models/base_spec.rb +34 -0
  54. data/spec/lib/chgk_rating/models/player_spec.rb +35 -0
  55. data/spec/lib/chgk_rating/models/rating_spec.rb +27 -0
  56. data/spec/lib/chgk_rating/models/recap_spec.rb +35 -0
  57. data/spec/lib/chgk_rating/models/team_spec.rb +89 -0
  58. data/spec/lib/chgk_rating/models/tournament_player_spec.rb +21 -0
  59. data/spec/lib/chgk_rating/models/tournament_spec.rb +92 -0
  60. data/spec/lib/chgk_rating/models/tournament_team_result_spec.rb +19 -0
  61. data/spec/lib/chgk_rating/models/tournament_team_spec.rb +36 -0
  62. data/spec/lib/chgk_rating/utils/snakecase_spec.rb +12 -0
  63. data/spec/lib/chgk_rating/utils/transformations_spec.rb +16 -0
  64. data/spec/lib/chgk_rating_spec.rb +5 -0
  65. data/spec/lib/ext/date_spec.rb +7 -0
  66. data/spec/lib/ext/date_time_spec.rb +11 -0
  67. data/spec/lib/ext/uri_spec.rb +7 -0
  68. data/spec/spec_helper.rb +13 -0
  69. data/spec/support/shared_examples.rb +63 -0
  70. data/spec/support/test_client.rb +6 -0
  71. data/spec/support/vcr.rb +10 -0
  72. metadata +228 -0
@@ -0,0 +1,62 @@
1
+ module ChgkRating
2
+ module Models
3
+ class Team < Base
4
+ # Returns a single Recap for the current Team at a given season
5
+ #
6
+ # @raise [ChgkRating::Error::BadRequest] Error raised when the requested season is invalid
7
+ # @return [ChgkRating::Models::Recap] The requested Recap.
8
+ # @param season_id [String or Integer] Season to load recap for.
9
+ def recap(season_id)
10
+ ChgkRating::Models::Recap.new season_id, team: self
11
+ end
12
+
13
+ # Returns TournamentTeam for the current Team at a given tournament
14
+ #
15
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament cannot be found.
16
+ # @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
17
+ # @param tournament_or_id [String, Integer or Tournament] Tournament to load the team for
18
+ def at_tournament(tournament_or_id)
19
+ ChgkRating::Models::TournamentTeam.new self, tournament: tournament_or_id, lazy: true
20
+ end
21
+
22
+ # Returns Rating for the current Team in a given release
23
+ #
24
+ # @return [ChgkRating::Models::Rating] The requested Rating.
25
+ # @param release_id [String or Integer] Release to load rating for.
26
+ def rating(release_id)
27
+ ChgkRating::Models::Rating.new release_id, team: self
28
+ end
29
+
30
+ # Returns an array-like Ratings collection for the current team.
31
+ #
32
+ # @return [ChgkRating::Collection::Ratings] The collection of ratings.
33
+ def ratings
34
+ ChgkRating::Collections::Ratings.new team: self
35
+ end
36
+
37
+ # Returns an hash-like Recaps collection for the current team, grouped by seasons. Seasons act
38
+ # as keys, whereas Recap models - as values.
39
+ #
40
+ # @return [ChgkRating::Collection::Recaps] The collection of recaps.
41
+ def recaps
42
+ ChgkRating::Collections::Recaps.new team: self
43
+ end
44
+
45
+ # Returns a collection of Tournaments that the current team participated at based on the given criteria
46
+ #
47
+ # @raise [ChgkRating::Error::NotFound] Error raised when nothing can be found based on the given criteria.
48
+ # @return [ChgkRating::Collection::Tournaments] The collection of tournaments.
49
+ # @param season_id [String or Integer] Season to load tournaments for
50
+ # @option params [String or Integer] :page The requested page. Default is 1
51
+ def tournaments(season_id: nil, params: {})
52
+ ChgkRating::Collections::Tournaments.new params.merge team: self, season_id: season_id, lazy: true
53
+ end
54
+
55
+ private
56
+
57
+ def api_path
58
+ 'teams'
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,46 @@
1
+ module ChgkRating
2
+ module Models
3
+ class Tournament < Base
4
+ # Returns an array-like TournamentPlayers collection containing roster for a team at the current tournament.
5
+ #
6
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
7
+ # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
8
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load players for.
9
+ def team_players(team_or_id)
10
+ ChgkRating::Collections::TournamentPlayers.new tournament: self, team: team_or_id
11
+ end
12
+
13
+ # Returns an array-like TournamentTeamResults collection with results for a given team in the current
14
+ # tournament
15
+ #
16
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
17
+ # @return [ChgkRating::Collection::TournamentTeamResults] The collection of results.
18
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load results for.
19
+ def team_results(team_or_id)
20
+ ChgkRating::Collections::TournamentTeamResults.new tournament: self, team: team_or_id
21
+ end
22
+
23
+ # Returns an array-like TournamentTeams collection specifying which teams participated in the current tournament
24
+ #
25
+ # @return [ChgkRating::Collection::Ratings] The collection of teams.
26
+ def team_list
27
+ ChgkRating::Collections::TournamentTeams.new tournament: self
28
+ end
29
+
30
+ # Returns information about a single TournamentTeam in the current tournament
31
+ #
32
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
33
+ # @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
34
+ # @param team_or_id [String, Integer or Team] Team to search for.
35
+ def team(team_or_id)
36
+ ChgkRating::Models::TournamentTeam.new team_or_id, tournament: self, lazy: true
37
+ end
38
+
39
+ private
40
+
41
+ def api_path
42
+ 'tournaments'
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ module ChgkRating
2
+ module Models
3
+ class TournamentPlayer < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ module ChgkRating
2
+ module Models
3
+ class TournamentTeam < Base
4
+ no_eager_loading!
5
+
6
+ attr_reader :tournament, :team
7
+
8
+ def initialize(team_or_hash, params = {})
9
+ @team = build_model team_or_hash
10
+ @tournament = build_model params[:tournament], ChgkRating::Models::Tournament
11
+ super extract_id_from(team_or_hash), params
12
+ end
13
+
14
+ # Returns an array-like TournamentPlayers collection containing roster for the current TournamentTeam
15
+ #
16
+ # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
17
+ def players
18
+ ChgkRating::Collections::TournamentPlayers.new tournament: @tournament, team: @team
19
+ end
20
+
21
+ # Returns an array-like TournamentTeamResults collection containing results for the current TournamentTeam
22
+ #
23
+ # @return [ChgkRating::Collection::TournamentTeamResults] The collection of results.
24
+ def results
25
+ ChgkRating::Collections::TournamentTeamResults.new tournament: @tournament, team: @team
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ module ChgkRating
2
+ module Models
3
+ class TournamentTeamResult < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ module ChgkRating
2
+ module Request
3
+ include ChgkRating::Connection
4
+
5
+ def get(path, params = {})
6
+ respond perform_get(path, params)
7
+ end
8
+
9
+ private
10
+
11
+ def perform_get(path, params)
12
+ connection.get do |req|
13
+ req.url path
14
+ req.params = params
15
+ end
16
+ end
17
+
18
+ def respond(response)
19
+ begin
20
+ body = MultiJson.load response.body
21
+ raise MultiJson::ParseError if body.respond_to?(:has_key?) && body.has_key?('error')
22
+ body
23
+ rescue MultiJson::ParseError
24
+ respond_with_error response.status, response.body
25
+ end
26
+ end
27
+
28
+ def respond_with_error(code, body)
29
+ fail ChgkRating::Error::ERRORS[code].from_response(body)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,16 @@
1
+ # Initial code taken from Facets gem by Rubyworks
2
+ # https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
3
+
4
+ class String
5
+ # Underscore a string such that camelcase, dashes and spaces are
6
+ # replaced by underscores.
7
+ def snakecase_upcase
8
+ split('::').last.
9
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
10
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
11
+ tr('-', '_').
12
+ gsub(/\s/, '_').
13
+ gsub(/__+/, '_').
14
+ upcase
15
+ end
16
+ end
@@ -0,0 +1,83 @@
1
+ module ChgkRating
2
+ module Utils
3
+ module Transformations
4
+ def transformation(name = 'integer_string')
5
+ up, down = name.to_s.split '_'
6
+ up = 'integer' if up.nil? || up.empty?
7
+ down = 'string' if down.nil? || down.empty?
8
+
9
+ %i(up down).inject({}) do |result, t|
10
+ current_transformer = binding.local_variable_get t
11
+ result.merge({
12
+ "transform_#{t}".to_sym => send(current_transformer)
13
+ })
14
+ end
15
+ end
16
+
17
+ class << self
18
+ def load_transformers!
19
+ TRANSFORMERS.each do |method_name, transformer|
20
+ define_method(method_name) { transformer }
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def chgk_object(namespace, type = 'Models')
27
+ ->(d) do
28
+ opts = type == 'Models' ?
29
+ [d, {lazy: true}] :
30
+ [{collection: d, lazy: true}]
31
+
32
+ Module.const_get("ChgkRating::#{type}::#{namespace}").new(*opts)
33
+ end
34
+ end
35
+
36
+ def to_boolean
37
+ ->(d) { !d.to_i.zero? }
38
+ end
39
+
40
+ def to_binary_boolean
41
+ ->(d) { d ? '1' : '0' }
42
+ end
43
+
44
+ def to_star(method = :to_s, iterate = false)
45
+ ->(d) do
46
+ iterate ?
47
+ d.map {|obj| obj.send method } :
48
+ d.send(method)
49
+ end
50
+ end
51
+ end
52
+
53
+ TRANSFORMERS = {
54
+ string: to_star,
55
+ integer: to_star(:to_i),
56
+ float: to_star(:to_f),
57
+ id: to_star(:id),
58
+ ids: to_star(:id, true),
59
+ sym: to_star(:to_sym),
60
+ strdate: to_star(:to_s_chgk),
61
+ uri: ->(d) { URI.parse_safely d },
62
+ boolean: to_boolean,
63
+ binboolean: to_binary_boolean,
64
+ date: ->(d) { Date.parse_safely d},
65
+ datetime: ->(d) { DateTime.parse_safely d},
66
+ splitboolean: ->(d) do
67
+ d&.split('')&.map {|result| to_boolean.call(result)}
68
+ end,
69
+ arraystrboolean: ->(d) do
70
+ d&.map {|result| to_binary_boolean.call(result)}
71
+ end,
72
+ arrayboolean: ->(d) do
73
+ d&.map {|result| to_boolean.call(result)}
74
+ end,
75
+ team: chgk_object('Team'),
76
+ player: chgk_object('Player'),
77
+ players: chgk_object('Players', 'Collections')
78
+ }.freeze
79
+
80
+ load_transformers!
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module ChgkRating
2
+ VERSION = '1.0.0.rc1'.freeze
3
+ end
data/lib/ext/date.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Date
2
+ def self.parse_safely(raw_date_time)
3
+ begin
4
+ self.parse raw_date_time
5
+ rescue ArgumentError
6
+ nil
7
+ rescue TypeError
8
+ nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class DateTime
2
+ def to_s_chgk
3
+ self.strftime('%Y-%m-%d %H:%M:%S')
4
+ end
5
+ end
data/lib/ext/uri.rb ADDED
@@ -0,0 +1,9 @@
1
+ module URI
2
+ def self.parse_safely(raw_uri)
3
+ begin
4
+ URI.parse raw_uri
5
+ rescue URI::InvalidURIError
6
+ nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,198 @@
1
+ RSpec.shared_examples 'lazy loaded' do
2
+ it 'is lazy loaded' do
3
+ expect(object.lazy).to eq(true)
4
+ end
5
+ end
6
+
7
+ RSpec.describe ChgkRating::Client do
8
+ let(:team_1) { test_client.team(1, true) }
9
+ let(:team_52853) { test_client.team(52853, true) }
10
+ let(:tournament_3506) { test_client.tournament(3506, true) }
11
+
12
+ context 'errors' do
13
+ it 'should raise an error for an erroneous request' do
14
+ expect( -> { VCR.use_cassette 'erroneous_request' do
15
+ test_client.tournament '/thats/an/error'
16
+ end } ).to raise_error(ChgkRating::Error::NotFound)
17
+ end
18
+ end
19
+
20
+ describe '#team_at_tournament' do
21
+ subject { test_client.team_at_tournament tournament_3506, team_52853 }
22
+
23
+ it { is_expected.to be_an_instance_of ChgkRating::Models::TournamentTeam }
24
+
25
+ include_examples 'lazy loaded' do
26
+ let(:object) { subject }
27
+ end
28
+ end
29
+
30
+ describe '#tournament' do
31
+ subject { test_client.tournament 3506, true }
32
+
33
+ include_examples 'lazy loaded' do
34
+ let(:object) { subject }
35
+ end
36
+
37
+ it { is_expected.to be_an_instance_of ChgkRating::Models::Tournament }
38
+ end
39
+
40
+ describe '#rating' do
41
+ subject do
42
+ VCR.use_cassette('rating_release') { test_client.rating team_1, 24 }
43
+ end
44
+
45
+ it { is_expected.to be_an_instance_of ChgkRating::Models::Rating }
46
+ end
47
+
48
+ describe '#recap' do
49
+ subject do
50
+ VCR.use_cassette 'recap_last_season' do
51
+ test_client.recap test_client.team(7931, true), :last
52
+ end
53
+ end
54
+
55
+ it { is_expected.to be_an_instance_of ChgkRating::Models::Recap }
56
+ end
57
+
58
+ describe '#team' do
59
+ subject { test_client.team 1, true }
60
+
61
+ it { is_expected.to be_an_instance_of ChgkRating::Models::Team }
62
+
63
+ include_examples 'lazy loaded' do
64
+ let(:object) { subject }
65
+ end
66
+ end
67
+
68
+ describe '#player' do
69
+ subject { test_client.player 42511, true }
70
+
71
+ it { is_expected.to be_an_instance_of ChgkRating::Models::Player }
72
+
73
+ include_examples 'lazy loaded' do
74
+ let(:object) { subject }
75
+ end
76
+ end
77
+
78
+ describe '#ratings' do
79
+ subject do
80
+ VCR.use_cassette 'team_ratings' do
81
+ test_client.ratings team_1
82
+ end
83
+ end
84
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Ratings }
85
+ end
86
+
87
+ describe '#team_players_at_tournament' do
88
+ subject do
89
+ VCR.use_cassette 'team_players_at_tournament' do
90
+ test_client.team_players_at_tournament tournament_3506, team_52853
91
+ end
92
+ end
93
+
94
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::TournamentPlayers }
95
+ end
96
+
97
+ describe '#team_results_at_tournament' do
98
+ subject do
99
+ VCR.use_cassette 'team_results_at_tournament' do
100
+ test_client.team_results_at_tournament tournament_3506, team_52853
101
+ end
102
+ end
103
+
104
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::TournamentTeamResults }
105
+ end
106
+
107
+ describe '#teams_at_tournament' do
108
+ subject do
109
+ VCR.use_cassette 'teams_at_tournament' do
110
+ test_client.teams_at_tournament tournament_3506
111
+ end
112
+ end
113
+
114
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::TournamentTeams }
115
+ end
116
+
117
+ describe '#tournaments' do
118
+ context 'all tournaments for a team by season' do
119
+ subject do
120
+ VCR.use_cassette 'team_tournaments_season' do
121
+ test_client.tournaments team_or_id: team_1, season_id: 4
122
+ end
123
+ end
124
+
125
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Tournaments }
126
+ end
127
+
128
+ context 'tournaments for a team' do
129
+ subject do
130
+ VCR.use_cassette 'team_tournaments' do
131
+ test_client.tournaments team_or_id: team_1
132
+ end
133
+ end
134
+
135
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Tournaments }
136
+ end
137
+
138
+ context 'all tournaments' do
139
+ subject do
140
+ VCR.use_cassette 'tournaments' do
141
+ test_client.tournaments
142
+ end
143
+ end
144
+
145
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Tournaments }
146
+ end
147
+ end
148
+
149
+ describe '#recaps' do
150
+ subject do
151
+ VCR.use_cassette 'recaps' do
152
+ test_client.recaps team_1
153
+ end
154
+ end
155
+
156
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Recaps }
157
+ end
158
+
159
+ describe '#search_teams' do
160
+ subject do
161
+ VCR.use_cassette 'teams_searching' do
162
+ test_client.search_teams name: 'э', town: 'мин'
163
+ end
164
+ end
165
+
166
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Teams::Search }
167
+ end
168
+
169
+ describe '#teams' do
170
+ subject do
171
+ VCR.use_cassette 'teams' do
172
+ test_client.teams
173
+ end
174
+ end
175
+
176
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Teams }
177
+ end
178
+
179
+ describe '#search_players' do
180
+ subject do
181
+ VCR.use_cassette 'players_searching' do
182
+ test_client.search_players name: 'вас', surname: 'а'
183
+ end
184
+ end
185
+
186
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Players::Search }
187
+ end
188
+
189
+ describe '#players' do
190
+ subject do
191
+ VCR.use_cassette 'players' do
192
+ test_client.players
193
+ end
194
+ end
195
+
196
+ it { is_expected.to be_an_instance_of ChgkRating::Collections::Players }
197
+ end
198
+ end