chgk_rating 1.0.0.rc1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +12 -12
  3. data/.github/PULL_REQUEST_TEMPLATE.md +10 -10
  4. data/CHANGELOG.md +7 -0
  5. data/Gemfile +2 -6
  6. data/LICENSE +21 -21
  7. data/README.md +2 -1
  8. data/Rakefile +12 -12
  9. data/chgk_rating.gemspec +28 -28
  10. data/lib/chgk_rating.rb +51 -52
  11. data/lib/chgk_rating/attribute_mappings.rb +138 -137
  12. data/lib/chgk_rating/chgk_object.rb +12 -12
  13. data/lib/chgk_rating/client.rb +172 -172
  14. data/lib/chgk_rating/collections/base.rb +78 -78
  15. data/lib/chgk_rating/collections/players.rb +16 -16
  16. data/lib/chgk_rating/collections/ratings.rb +22 -22
  17. data/lib/chgk_rating/collections/recaps.rb +24 -24
  18. data/lib/chgk_rating/collections/teams.rb +16 -16
  19. data/lib/chgk_rating/collections/tournament_players.rb +23 -23
  20. data/lib/chgk_rating/collections/tournament_team_results.rb +23 -23
  21. data/lib/chgk_rating/collections/tournament_teams.rb +21 -21
  22. data/lib/chgk_rating/collections/tournaments.rb +46 -46
  23. data/lib/chgk_rating/concerns/searching.rb +24 -20
  24. data/lib/chgk_rating/connection.rb +18 -18
  25. data/lib/chgk_rating/error.rb +45 -45
  26. data/lib/chgk_rating/models/base.rb +89 -89
  27. data/lib/chgk_rating/models/player.rb +10 -10
  28. data/lib/chgk_rating/models/rating.rb +18 -18
  29. data/lib/chgk_rating/models/recap.rb +18 -18
  30. data/lib/chgk_rating/models/team.rb +61 -61
  31. data/lib/chgk_rating/models/tournament.rb +45 -45
  32. data/lib/chgk_rating/models/tournament_player.rb +7 -7
  33. data/lib/chgk_rating/models/tournament_team.rb +28 -28
  34. data/lib/chgk_rating/models/tournament_team_result.rb +7 -7
  35. data/lib/chgk_rating/request.rb +31 -31
  36. data/lib/chgk_rating/utils/snakecase.rb +15 -15
  37. data/lib/chgk_rating/utils/transformations.rb +82 -82
  38. data/lib/chgk_rating/version.rb +2 -2
  39. data/lib/ext/date.rb +10 -10
  40. data/lib/ext/date_time.rb +4 -4
  41. data/lib/ext/uri.rb +8 -8
  42. data/spec/lib/chgk_rating/client_spec.rb +207 -197
  43. data/spec/lib/chgk_rating/collections/players_spec.rb +44 -44
  44. data/spec/lib/chgk_rating/collections/ratings_spec.rb +24 -24
  45. data/spec/lib/chgk_rating/collections/recaps_spec.rb +20 -20
  46. data/spec/lib/chgk_rating/collections/teams_spec.rb +41 -41
  47. data/spec/lib/chgk_rating/collections/tournament_players_spec.rb +17 -17
  48. data/spec/lib/chgk_rating/collections/tournament_team_results_spec.rb +16 -16
  49. data/spec/lib/chgk_rating/collections/tournament_teams_spec.rb +59 -57
  50. data/spec/lib/chgk_rating/collections/tournaments_spec.rb +61 -61
  51. data/spec/lib/chgk_rating/models/base_spec.rb +33 -33
  52. data/spec/lib/chgk_rating/models/player_spec.rb +34 -34
  53. data/spec/lib/chgk_rating/models/rating_spec.rb +26 -26
  54. data/spec/lib/chgk_rating/models/recap_spec.rb +34 -34
  55. data/spec/lib/chgk_rating/models/team_spec.rb +88 -88
  56. data/spec/lib/chgk_rating/models/tournament_player_spec.rb +20 -20
  57. data/spec/lib/chgk_rating/models/tournament_spec.rb +91 -91
  58. data/spec/lib/chgk_rating/models/tournament_team_result_spec.rb +18 -18
  59. data/spec/lib/chgk_rating/models/tournament_team_spec.rb +35 -35
  60. data/spec/lib/chgk_rating/utils/snakecase_spec.rb +11 -11
  61. data/spec/lib/chgk_rating/utils/transformations_spec.rb +15 -15
  62. data/spec/lib/chgk_rating_spec.rb +4 -4
  63. data/spec/lib/ext/date_spec.rb +6 -6
  64. data/spec/lib/ext/date_time_spec.rb +10 -10
  65. data/spec/lib/ext/uri_spec.rb +6 -6
  66. data/spec/spec_helper.rb +12 -12
  67. data/spec/support/shared_examples.rb +62 -62
  68. data/spec/support/test_client.rb +5 -5
  69. data/spec/support/vcr.rb +10 -9
  70. metadata +5 -5
@@ -1,13 +1,13 @@
1
- module ChgkRating
2
- class ChgkObject
3
- def build_model(object, klass = ChgkRating::Models::Team, params = {lazy: true})
4
- return unless object
5
- object.instance_of?(klass) ? object : klass.new(object, params)
6
- end
7
-
8
- def extract_id_from(obj, klass = ChgkRating::Models::Team)
9
- return obj unless obj.is_a? klass
10
- obj&.id
11
- end
12
- end
1
+ module ChgkRating
2
+ class ChgkObject
3
+ def build_model(object, klass = ChgkRating::Models::Team, params = {lazy: true})
4
+ return unless object
5
+ object.instance_of?(klass) ? object : klass.new(object, params)
6
+ end
7
+
8
+ def extract_id_from(obj, klass = ChgkRating::Models::Team)
9
+ return obj unless obj.is_a? klass
10
+ obj&.id
11
+ end
12
+ end
13
13
  end
@@ -1,173 +1,173 @@
1
- module ChgkRating
2
- class Client
3
- # Returns a single Team
4
- #
5
- # @raise [ChgkRating::Error::NotFound] Error raised when the Team cannot be found.
6
- # @return [ChgkRating::Models::Team] The requested Team.
7
- # @param id [String or Integer] Team's id
8
- # @param lazy [Boolean] Should the Team be lazily loaded? Default is `false`.
9
- def team(id, lazy = false)
10
- ChgkRating::Models::Team.new id, lazy: lazy
11
- end
12
-
13
- # Returns a single Player
14
- #
15
- # @raise [ChgkRating::Error::NotFound] Error raised when the Player cannot be found.
16
- # @return [ChgkRating::Models::Player] The requested Player.
17
- # @param id [String or Integer] Player's id
18
- # @param lazy [Boolean] Should the Player be lazily loaded? Default is `false`.
19
- def player(id, lazy = false)
20
- ChgkRating::Models::Player.new id, lazy: lazy
21
- end
22
-
23
- # Returns a single Recap for a given Team
24
- #
25
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
26
- # @return [ChgkRating::Models::Recap] The requested Recap.
27
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load recaps for.
28
- # @param season_id [String or Integer] Season to load recap for.
29
- def recap(team_or_id, season_id)
30
- team(team_or_id, true).recap(season_id)
31
- end
32
-
33
- # Returns a single Tournament
34
- #
35
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament cannot be found.
36
- # @return [ChgkRating::Models::Tournament] The requested Tournament.
37
- # @param id [String or Integer] Tournament's id
38
- # @param lazy [Boolean] Should the Tournament be lazily loaded? Default is `false`.
39
- def tournament(id, lazy = false)
40
- ChgkRating::Models::Tournament.new id, lazy: lazy
41
- end
42
-
43
- # Returns a single TournamentTeam
44
- #
45
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
46
- # @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
47
- # @param tournament_or_id [String, Integer or Tournament] Tournament to load team for.
48
- # @param team_or_id [String, Integer or Team] Team to search for.
49
- def team_at_tournament(tournament_or_id, team_or_id)
50
- tournament(tournament_or_id, true).team(team_or_id)
51
- end
52
-
53
- # Returns Rating for a given Team in a given release
54
- #
55
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested release or Team cannot be found.
56
- # @return [ChgkRating::Models::Rating] The requested Rating.
57
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load rating for.
58
- # @param release_id [String or Integer] Release to load rating for.
59
- def rating(team_or_id, release_id)
60
- team(team_or_id, true).rating(release_id)
61
- end
62
-
63
- # Search
64
-
65
- # Returns a Players collection based on the search criteria.
66
- #
67
- # @return [ChgkRating::Collection::Players::Search] Found Players.
68
- # @option params [String] :name Player's name
69
- # @option params [String] :surname Player's surname
70
- # @option params [String] :patronymic Player's patronymic
71
- # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
72
- def search_players(params)
73
- ChgkRating::Collections::Players.search request: params
74
- end
75
-
76
- # Returns a Teams collection based on the search criteria.
77
- #
78
- # @return [ChgkRating::Collection::Teams::Search] Found Teams.
79
- # @option params [String] :name Team's name
80
- # @option params [String] :town Team's town of origin
81
- # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
82
- def search_teams(params)
83
- ChgkRating::Collections::Teams.search request: params
84
- end
85
-
86
- # Collections
87
-
88
- # Returns an array-like Teams collection that contains Team models
89
- #
90
- # @return [ChgkRating::Collection::Teams] The collection of Teams.
91
- # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
92
- # @option params [Boolean] :lazy Should the Teams models be marked as lazily loaded?
93
- # Note that the models will still contain all the information returned by the API.
94
- # @option params [Enumerable] :collection An array or collection of Teams that will be used to build a new
95
- # collection. If this option is provided, API request will not be sent.
96
- def teams(params = {})
97
- ChgkRating::Collections::Teams.new params
98
- end
99
-
100
- # Returns an array-like Players collection that contains Player models
101
- #
102
- # @return [ChgkRating::Collection::Players] The collection of Players.
103
- # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
104
- # @option params [Boolean] :lazy Should the Player models be marked as lazily loaded?
105
- # Note that the models will still contain all the information returned by the API.
106
- # @option params [Enumerable] :collection An array or collection of Players that will be used to build a new
107
- # collection. If this option is provided, API request will not be sent.
108
- def players(params = {})
109
- ChgkRating::Collections::Players.new params
110
- end
111
-
112
- # Returns an hash-like Recaps collection for a given team, grouped by seasons. Seasons act
113
- # as keys, whereas Recap models - as values.
114
- #
115
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
116
- # @return [ChgkRating::Collection::Recaps] The collection of recaps.
117
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load recaps for.
118
- def recaps(team_or_id)
119
- team(team_or_id, true).recaps
120
- end
121
-
122
- # Returns a collection of Tournaments based on the given criteria
123
- #
124
- # @raise [ChgkRating::Error::NotFound] Error raised when nothing can be found based on the given criteria.
125
- # @return [ChgkRating::Collection::Tournaments] The collection of tournaments.
126
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load tournaments for.
127
- # @param season_id [String or Integer] Season to load tournaments for
128
- # @option params [String or Integer] :page The requested page. Default is 1
129
- def tournaments(team_or_id: nil, season_id: nil, params: {})
130
- ChgkRating::Collections::Tournaments.new params.merge(team: team_or_id, season_id: season_id)
131
- end
132
-
133
- # Returns an array-like Ratings collection for a given team.
134
- #
135
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
136
- # @return [ChgkRating::Collection::Ratings] The collection of ratings.
137
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load ratings for.
138
- def ratings(team_or_id)
139
- team(team_or_id, true).ratings
140
- end
141
-
142
- # Returns an array-like TournamentTeams collection specifying which teams participated in a given tournament
143
- #
144
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament cannot be found.
145
- # @return [ChgkRating::Collection::Ratings] The collection of teams.
146
- # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load teams for.
147
- def teams_at_tournament(tournament_or_id)
148
- tournament(tournament_or_id, true).team_list
149
- end
150
-
151
- # Returns an array-like TournamentTeamResults collection with results for a given team in a given
152
- # tournament
153
- #
154
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
155
- # @return [ChgkRating::Collection::TournamentTeamResults] The collection of results.
156
- # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load results for.
157
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load results for.
158
- def team_results_at_tournament(tournament_or_id, team_or_id)
159
- team_at_tournament(tournament_or_id, team_or_id).results
160
- end
161
-
162
- # Returns an array-like TournamentPlayers collection containing roster for a
163
- # given team at a given tournament.
164
- #
165
- # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
166
- # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
167
- # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load players for.
168
- # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load players for.
169
- def team_players_at_tournament(tournament_or_id, team_or_id)
170
- team_at_tournament(tournament_or_id, team_or_id).players
171
- end
172
- end
1
+ module ChgkRating
2
+ class Client
3
+ # Returns a single Team
4
+ #
5
+ # @raise [ChgkRating::Error::NotFound] Error raised when the Team cannot be found.
6
+ # @return [ChgkRating::Models::Team] The requested Team.
7
+ # @param id [String or Integer] Team's id
8
+ # @param lazy [Boolean] Should the Team be lazily loaded? Default is `false`.
9
+ def team(id, lazy = false)
10
+ ChgkRating::Models::Team.new id, lazy: lazy
11
+ end
12
+
13
+ # Returns a single Player
14
+ #
15
+ # @raise [ChgkRating::Error::NotFound] Error raised when the Player cannot be found.
16
+ # @return [ChgkRating::Models::Player] The requested Player.
17
+ # @param id [String or Integer] Player's id
18
+ # @param lazy [Boolean] Should the Player be lazily loaded? Default is `false`.
19
+ def player(id, lazy = false)
20
+ ChgkRating::Models::Player.new id, lazy: lazy
21
+ end
22
+
23
+ # Returns a single Recap for a given Team
24
+ #
25
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
26
+ # @return [ChgkRating::Models::Recap] The requested Recap.
27
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load recaps for.
28
+ # @param season_id [String or Integer] Season to load recap for.
29
+ def recap(team_or_id, season_id)
30
+ team(team_or_id, true).recap(season_id)
31
+ end
32
+
33
+ # Returns a single Tournament
34
+ #
35
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament cannot be found.
36
+ # @return [ChgkRating::Models::Tournament] The requested Tournament.
37
+ # @param id [String or Integer] Tournament's id
38
+ # @param lazy [Boolean] Should the Tournament be lazily loaded? Default is `false`.
39
+ def tournament(id, lazy = false)
40
+ ChgkRating::Models::Tournament.new id, lazy: lazy
41
+ end
42
+
43
+ # Returns a single TournamentTeam
44
+ #
45
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
46
+ # @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
47
+ # @param tournament_or_id [String, Integer or Tournament] Tournament to load team for.
48
+ # @param team_or_id [String, Integer or Team] Team to search for.
49
+ def team_at_tournament(tournament_or_id, team_or_id)
50
+ tournament(tournament_or_id, true).team(team_or_id)
51
+ end
52
+
53
+ # Returns Rating for a given Team in a given release
54
+ #
55
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested release or Team cannot be found.
56
+ # @return [ChgkRating::Models::Rating] The requested Rating.
57
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load rating for.
58
+ # @param release_id [String or Integer] Release to load rating for.
59
+ def rating(team_or_id, release_id)
60
+ team(team_or_id, true).rating(release_id)
61
+ end
62
+
63
+ # Search
64
+
65
+ # Returns a Players collection based on the search criteria.
66
+ #
67
+ # @return [ChgkRating::Collection::Players::Search] Found Players.
68
+ # @option params [String] :name Player's name
69
+ # @option params [String] :surname Player's surname
70
+ # @option params [String] :patronymic Player's patronymic
71
+ # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
72
+ def search_players(params)
73
+ ChgkRating::Collections::Players.search request: params
74
+ end
75
+
76
+ # Returns a Teams collection based on the search criteria.
77
+ #
78
+ # @return [ChgkRating::Collection::Teams::Search] Found Teams.
79
+ # @option params [String] :name Team's name
80
+ # @option params [String] :town Team's town of origin
81
+ # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
82
+ def search_teams(params)
83
+ ChgkRating::Collections::Teams.search request: params
84
+ end
85
+
86
+ # Collections
87
+
88
+ # Returns an array-like Teams collection that contains Team models
89
+ #
90
+ # @return [ChgkRating::Collection::Teams] The collection of Teams.
91
+ # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
92
+ # @option params [Boolean] :lazy Should the Teams models be marked as lazily loaded?
93
+ # Note that the models will still contain all the information returned by the API.
94
+ # @option params [Enumerable] :collection An array or collection of Teams that will be used to build a new
95
+ # collection. If this option is provided, API request will not be sent.
96
+ def teams(params = {})
97
+ ChgkRating::Collections::Teams.new params
98
+ end
99
+
100
+ # Returns an array-like Players collection that contains Player models
101
+ #
102
+ # @return [ChgkRating::Collection::Players] The collection of Players.
103
+ # @option params [String or Integer] :page The requested page. Default is 1, and there are 1000 results per page.
104
+ # @option params [Boolean] :lazy Should the Player models be marked as lazily loaded?
105
+ # Note that the models will still contain all the information returned by the API.
106
+ # @option params [Enumerable] :collection An array or collection of Players that will be used to build a new
107
+ # collection. If this option is provided, API request will not be sent.
108
+ def players(params = {})
109
+ ChgkRating::Collections::Players.new params
110
+ end
111
+
112
+ # Returns an hash-like Recaps collection for a given team, grouped by seasons. Seasons act
113
+ # as keys, whereas Recap models - as values.
114
+ #
115
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
116
+ # @return [ChgkRating::Collection::Recaps] The collection of recaps.
117
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load recaps for.
118
+ def recaps(team_or_id)
119
+ team(team_or_id, true).recaps
120
+ end
121
+
122
+ # Returns a collection of Tournaments based on the given criteria
123
+ #
124
+ # @raise [ChgkRating::Error::NotFound] Error raised when nothing can be found based on the given criteria.
125
+ # @return [ChgkRating::Collection::Tournaments] The collection of tournaments.
126
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load tournaments for.
127
+ # @param season_id [String or Integer] Season to load tournaments for
128
+ # @option params [String or Integer] :page The requested page. Default is 1
129
+ def tournaments(team_or_id: nil, season_id: nil, params: {})
130
+ ChgkRating::Collections::Tournaments.new params.merge(team: team_or_id, season_id: season_id)
131
+ end
132
+
133
+ # Returns an array-like Ratings collection for a given team.
134
+ #
135
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
136
+ # @return [ChgkRating::Collection::Ratings] The collection of ratings.
137
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load ratings for.
138
+ def ratings(team_or_id)
139
+ team(team_or_id, true).ratings
140
+ end
141
+
142
+ # Returns an array-like TournamentTeams collection specifying which teams participated in a given tournament
143
+ #
144
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament cannot be found.
145
+ # @return [ChgkRating::Collection::Ratings] The collection of teams.
146
+ # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load teams for.
147
+ def teams_at_tournament(tournament_or_id)
148
+ tournament(tournament_or_id, true).team_list
149
+ end
150
+
151
+ # Returns an array-like TournamentTeamResults collection with results for a given team in a given
152
+ # tournament
153
+ #
154
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
155
+ # @return [ChgkRating::Collection::TournamentTeamResults] The collection of results.
156
+ # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load results for.
157
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load results for.
158
+ def team_results_at_tournament(tournament_or_id, team_or_id)
159
+ team_at_tournament(tournament_or_id, team_or_id).results
160
+ end
161
+
162
+ # Returns an array-like TournamentPlayers collection containing roster for a
163
+ # given team at a given tournament.
164
+ #
165
+ # @raise [ChgkRating::Error::NotFound] Error raised when the requested Tournament or Team cannot be found.
166
+ # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
167
+ # @param tournament_or_id [String, Integer or ChgkRating::Models::Tournament] Tournament to load players for.
168
+ # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load players for.
169
+ def team_players_at_tournament(tournament_or_id, team_or_id)
170
+ team_at_tournament(tournament_or_id, team_or_id).players
171
+ end
172
+ end
173
173
  end
@@ -1,79 +1,79 @@
1
- module ChgkRating
2
- module Collections
3
- class Base < ChgkObject
4
- include ChgkRating::Request
5
- include Enumerable
6
-
7
- attr_reader :items
8
-
9
- def initialize(params = {})
10
- results = params[:collection] ||
11
- prepare(get(api_path, build_request_params_from(params)))
12
-
13
- @items = process results, params
14
- end
15
-
16
- def each
17
- @items.each { |item| yield(*item) }
18
- end
19
-
20
- def [](index)
21
- @items[index]
22
- end
23
-
24
- def to_a
25
- raise ChgkRating::Error::NotArrayType unless self.respond_to?(:to_a)
26
- self.items.to_a.map &:to_h
27
- end
28
-
29
- def to_h
30
- raise ChgkRating::Error::NotHashType unless self.respond_to?(:to_h)
31
- self.items.map { |k,v| revert_to_hash(k, v) }.to_h
32
- end
33
-
34
- def respond_to?(method, include_all = false)
35
- method = method.to_sym
36
- if %i( to_a to_h ).include?(method.to_sym)
37
- self.convertable? method
38
- else
39
- super
40
- end
41
- end
42
-
43
- def convertable?(method)
44
- return true if (method == :to_a && self.items.is_a?(Array)) ||
45
- (method == :to_h && self.items.is_a?(Hash))
46
- false
47
- end
48
-
49
- private
50
-
51
- def revert_to_hash(key, values)
52
- [key, values.to_h]
53
- end
54
-
55
- def build_request_params_from(params)
56
- request_params = params[:request].to_h
57
- request_params[:page] = params.delete(:page).to_i if params.has_key?(:page)
58
- request_params
59
- end
60
-
61
- def prepare(raw_results)
62
- if raw_results.respond_to?(:has_key?)
63
- return raw_results['tournaments'] if raw_results.has_key?('tournaments')
64
- return raw_results['items'] if raw_results.has_key?('items')
65
- end
66
-
67
- raw_results
68
- end
69
-
70
- def process(results, *_args)
71
- if results.is_a? Hash
72
- results.each { |season, result| results[season] = yield result }
73
- else
74
- results.map { |result| yield result }
75
- end
76
- end
77
- end
78
- end
1
+ module ChgkRating
2
+ module Collections
3
+ class Base < ChgkObject
4
+ include ChgkRating::Request
5
+ include Enumerable
6
+
7
+ attr_reader :items
8
+
9
+ def initialize(params = {})
10
+ results = params[:collection] ||
11
+ prepare(get(api_path, build_request_params_from(params)))
12
+
13
+ @items = process results, params
14
+ end
15
+
16
+ def each
17
+ @items.each { |item| yield(*item) }
18
+ end
19
+
20
+ def [](index)
21
+ @items[index]
22
+ end
23
+
24
+ def to_a
25
+ raise ChgkRating::Error::NotArrayType unless self.respond_to?(:to_a)
26
+ self.items.to_a.map &:to_h
27
+ end
28
+
29
+ def to_h
30
+ raise ChgkRating::Error::NotHashType unless self.respond_to?(:to_h)
31
+ self.items.map { |k,v| revert_to_hash(k, v) }.to_h
32
+ end
33
+
34
+ def respond_to?(method, include_all = false)
35
+ method = method.to_sym
36
+ if %i( to_a to_h ).include?(method.to_sym)
37
+ self.convertable? method
38
+ else
39
+ super
40
+ end
41
+ end
42
+
43
+ def convertable?(method)
44
+ return true if (method == :to_a && self.items.is_a?(Array)) ||
45
+ (method == :to_h && self.items.is_a?(Hash))
46
+ false
47
+ end
48
+
49
+ private
50
+
51
+ def revert_to_hash(key, values)
52
+ [key, values.to_h]
53
+ end
54
+
55
+ def build_request_params_from(params)
56
+ request_params = params[:request].to_h
57
+ request_params[:page] = params.delete(:page).to_i if params.has_key?(:page)
58
+ request_params
59
+ end
60
+
61
+ def prepare(raw_results)
62
+ if raw_results.respond_to?(:has_key?)
63
+ return raw_results['tournaments'] if raw_results.has_key?('tournaments')
64
+ return raw_results['items'] if raw_results.has_key?('items')
65
+ end
66
+
67
+ raw_results
68
+ end
69
+
70
+ def process(results, *_args)
71
+ if results.is_a? Hash
72
+ results.each { |season, result| results[season] = yield result }
73
+ else
74
+ results.map { |result| yield result }
75
+ end
76
+ end
77
+ end
78
+ end
79
79
  end