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,46 +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
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
46
  end
@@ -1,8 +1,8 @@
1
- module ChgkRating
2
- module Models
3
- class TournamentPlayer < Base
4
- no_eager_loading!
5
- no_lazy_support!
6
- end
7
- end
1
+ module ChgkRating
2
+ module Models
3
+ class TournamentPlayer < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+ end
7
+ end
8
8
  end
@@ -1,29 +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
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
29
  end
@@ -1,8 +1,8 @@
1
- module ChgkRating
2
- module Models
3
- class TournamentTeamResult < Base
4
- no_eager_loading!
5
- no_lazy_support!
6
- end
7
- end
1
+ module ChgkRating
2
+ module Models
3
+ class TournamentTeamResult < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+ end
7
+ end
8
8
  end
@@ -1,32 +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
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
32
  end
@@ -1,16 +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
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
16
  end
@@ -1,83 +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
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
83
  end
@@ -1,3 +1,3 @@
1
- module ChgkRating
2
- VERSION = '1.0.0.rc1'.freeze
1
+ module ChgkRating
2
+ VERSION = '1.0.0'.freeze
3
3
  end