chgk_rating 1.0.0 → 2.2.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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/Gemfile +3 -1
  4. data/LICENSE +1 -1
  5. data/README.md +10 -501
  6. data/Rakefile +4 -2
  7. data/chgk_rating.gemspec +23 -15
  8. data/lib/chgk_rating/attribute_mappings.rb +107 -80
  9. data/lib/chgk_rating/chgk_object.rb +6 -1
  10. data/lib/chgk_rating/client.rb +43 -10
  11. data/lib/chgk_rating/collections/base.rb +20 -15
  12. data/lib/chgk_rating/collections/players.rb +3 -1
  13. data/lib/chgk_rating/collections/ratings/player_ratings.rb +24 -0
  14. data/lib/chgk_rating/collections/{ratings.rb → ratings/team_ratings.rb} +5 -4
  15. data/lib/chgk_rating/collections/recaps.rb +4 -2
  16. data/lib/chgk_rating/collections/teams.rb +3 -1
  17. data/lib/chgk_rating/collections/tournaments/player_tournaments.rb +48 -0
  18. data/lib/chgk_rating/collections/{tournament_players.rb → tournaments/tournament_team_players.rb} +5 -3
  19. data/lib/chgk_rating/collections/{tournament_team_results.rb → tournaments/tournament_team_results.rb} +3 -1
  20. data/lib/chgk_rating/collections/{tournament_teams.rb → tournaments/tournament_teams.rb} +4 -1
  21. data/lib/chgk_rating/collections/{tournaments.rb → tournaments/tournaments.rb} +13 -8
  22. data/lib/chgk_rating/concerns/searching.rb +10 -9
  23. data/lib/chgk_rating/connection.rb +12 -7
  24. data/lib/chgk_rating/error.rb +7 -5
  25. data/lib/chgk_rating/models/base.rb +34 -15
  26. data/lib/chgk_rating/models/player.rb +28 -1
  27. data/lib/chgk_rating/models/rating/player_rating.rb +18 -0
  28. data/lib/chgk_rating/models/{tournament_player.rb → rating/rating.rb} +4 -2
  29. data/lib/chgk_rating/models/{rating.rb → rating/team_rating.rb} +4 -5
  30. data/lib/chgk_rating/models/recap.rb +3 -1
  31. data/lib/chgk_rating/models/team.rb +9 -7
  32. data/lib/chgk_rating/models/tournament/player_tournament.rb +10 -0
  33. data/lib/chgk_rating/models/{tournament.rb → tournament/tournament.rb} +7 -5
  34. data/lib/chgk_rating/models/{tournament_team.rb → tournament/tournament_team.rb} +6 -4
  35. data/lib/chgk_rating/models/tournament/tournament_team_player.rb +10 -0
  36. data/lib/chgk_rating/models/{tournament_team_result.rb → tournament/tournament_team_result.rb} +3 -1
  37. data/lib/chgk_rating/request.rb +10 -9
  38. data/lib/chgk_rating/utils/snakecase.rb +9 -7
  39. data/lib/chgk_rating/utils/transformations.rb +42 -35
  40. data/lib/chgk_rating/version.rb +4 -2
  41. data/lib/chgk_rating.rb +18 -11
  42. data/lib/ext/date.rb +6 -8
  43. data/lib/ext/date_time.rb +4 -2
  44. data/lib/ext/uri.rb +6 -6
  45. metadata +95 -90
  46. data/spec/lib/chgk_rating/client_spec.rb +0 -208
  47. data/spec/lib/chgk_rating/collections/players_spec.rb +0 -45
  48. data/spec/lib/chgk_rating/collections/ratings_spec.rb +0 -25
  49. data/spec/lib/chgk_rating/collections/recaps_spec.rb +0 -21
  50. data/spec/lib/chgk_rating/collections/teams_spec.rb +0 -42
  51. data/spec/lib/chgk_rating/collections/tournament_players_spec.rb +0 -18
  52. data/spec/lib/chgk_rating/collections/tournament_team_results_spec.rb +0 -17
  53. data/spec/lib/chgk_rating/collections/tournament_teams_spec.rb +0 -60
  54. data/spec/lib/chgk_rating/collections/tournaments_spec.rb +0 -62
  55. data/spec/lib/chgk_rating/models/base_spec.rb +0 -34
  56. data/spec/lib/chgk_rating/models/player_spec.rb +0 -35
  57. data/spec/lib/chgk_rating/models/rating_spec.rb +0 -27
  58. data/spec/lib/chgk_rating/models/recap_spec.rb +0 -35
  59. data/spec/lib/chgk_rating/models/team_spec.rb +0 -89
  60. data/spec/lib/chgk_rating/models/tournament_player_spec.rb +0 -21
  61. data/spec/lib/chgk_rating/models/tournament_spec.rb +0 -92
  62. data/spec/lib/chgk_rating/models/tournament_team_result_spec.rb +0 -19
  63. data/spec/lib/chgk_rating/models/tournament_team_spec.rb +0 -36
  64. data/spec/lib/chgk_rating/utils/snakecase_spec.rb +0 -12
  65. data/spec/lib/chgk_rating/utils/transformations_spec.rb +0 -16
  66. data/spec/lib/chgk_rating_spec.rb +0 -5
  67. data/spec/lib/ext/date_spec.rb +0 -7
  68. data/spec/lib/ext/date_time_spec.rb +0 -11
  69. data/spec/lib/ext/uri_spec.rb +0 -7
  70. data/spec/spec_helper.rb +0 -13
  71. data/spec/support/shared_examples.rb +0 -63
  72. data/spec/support/test_client.rb +0 -6
  73. data/spec/support/vcr.rb +0 -11
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Collections
3
5
  class Tournaments < Base
@@ -7,18 +9,18 @@ module ChgkRating
7
9
  @team = build_model params[:team]
8
10
 
9
11
  @season_id = params[:season_id]
10
- params.merge!(lazy: true) if @team || @season_id
12
+ params[:lazy] = true if @team || @season_id
11
13
  super
12
14
  end
13
15
 
14
16
  def revert_to_hash(key, values)
15
17
  [
16
- key,
17
- {
18
- 'idteam' => @team.id.to_s,
19
- 'idseason' => key,
20
- 'tournaments' => values.map(&:to_h)
21
- }
18
+ key,
19
+ {
20
+ 'idteam' => @team&.id.to_s,
21
+ 'idseason' => key,
22
+ 'tournaments' => values.map(&:to_h)
23
+ }
22
24
  ]
23
25
  end
24
26
 
@@ -35,13 +37,16 @@ module ChgkRating
35
37
  end
36
38
  end
37
39
 
40
+ # @return [String] Either `tournaments`, `teams/ID/tournaments`, or `teams/ID/tournaments/SEASON_ID`
38
41
  def api_path
39
42
  path = 'tournaments'
40
43
  return path unless @team
44
+
41
45
  path = "teams/#{@team.id}/#{path}"
42
46
  return path unless @season_id
47
+
43
48
  path + "/#{@season_id}"
44
49
  end
45
50
  end
46
51
  end
47
- end
52
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Concerns
3
5
  module Searching
@@ -5,21 +7,20 @@ module ChgkRating
5
7
  # Creates a Search child class of a given class that changes the URI where the
6
8
  # GET request should be sent
7
9
  def self.included(klass)
8
- klass.const_set('Search',
9
- Class.new(klass) do |*_args|
10
- def api_path
11
- super + '/search'
12
- end
13
- end
14
- )
10
+ klass.const_set(:Search,
11
+ Class.new(klass) do |*_args|
12
+ def api_path
13
+ "#{super}/search"
14
+ end
15
+ end)
15
16
 
16
17
  # The actual method to perform searching
17
18
  # Instantiates a Search class with the given search params
18
19
  # and send a GET request to the proper URI (defined above)
19
20
  klass.define_singleton_method :search do |params|
20
- klass.const_get('Search').new(params)
21
+ klass.const_get(:Search).new(params)
21
22
  end
22
23
  end
23
24
  end
24
25
  end
25
- end
26
+ end
@@ -1,19 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday/follow_redirects'
4
+
1
5
  module ChgkRating
2
6
  module Connection
3
- BASE_URL = 'http://rating.chgk.info/api'.freeze
7
+ BASE_URL = 'http://rating.chgk.info/api'
4
8
 
5
9
  def connection
6
10
  options = {
7
- headers: {
8
- accept: 'application/json',
9
- user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
10
- },
11
- url: BASE_URL + '/'
11
+ headers: {
12
+ accept: 'application/json',
13
+ user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
14
+ },
15
+ url: "#{BASE_URL}/"
12
16
  }
13
17
 
14
18
  Faraday.new options do |faraday|
19
+ faraday.use Faraday::FollowRedirects::Middleware
15
20
  faraday.adapter Faraday.default_adapter
16
21
  end
17
22
  end
18
23
  end
19
- end
24
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  class Error < StandardError
3
5
  ClientError = Class.new(self)
@@ -26,10 +28,10 @@ module ChgkRating
26
28
  NotImplemented = Class.new(ServerError)
27
29
 
28
30
  ERRORS = {
29
- 400 => ChgkRating::Error::BadRequest,
30
- 404 => ChgkRating::Error::NotFound,
31
- 501 => ChgkRating::Error::NotImplemented
32
- }
31
+ 400 => ChgkRating::Error::BadRequest,
32
+ 404 => ChgkRating::Error::NotFound,
33
+ 501 => ChgkRating::Error::NotImplemented
34
+ }.freeze
33
35
 
34
36
  class << self
35
37
  # Create a new error from an HTTP response
@@ -43,4 +45,4 @@ module ChgkRating
43
45
  super(message)
44
46
  end
45
47
  end
46
- end
48
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class Base < ChgkObject
@@ -24,7 +26,8 @@ module ChgkRating
24
26
  # Set `force` to reload data even if it is already present.
25
27
  def eager_load!(force = false)
26
28
  return unless @lazy || force
27
- extract_from raw_by(self.id)
29
+
30
+ extract_from raw_by(id)
28
31
  @lazy = false
29
32
  self
30
33
  end
@@ -36,26 +39,31 @@ module ChgkRating
36
39
  end
37
40
 
38
41
  def self.no_lazy_support!
39
- self.const_set :NO_LAZY_SUPPORT, true
42
+ const_set :NO_LAZY_SUPPORT, true
40
43
  undef_method :lazy
41
44
  end
42
45
 
43
46
  def to_h
44
- self.class.attribute_mapping.inject({}) do |memo, (attr, mapping)|
45
- data = self.send attr
46
- data = mapping[:transform_down].call(data) if mapping.has_key?(:transform_down)
47
- memo[ mapping[:raw_name] ] = data
48
- memo
47
+ self.class.attribute_mapping.each_with_object({}) do |(attr, mapping), memo|
48
+ data = send attr
49
+ data = mapping[:transform_down].call(data) if mapping.key?(:transform_down)
50
+ memo[mapping[:raw_name]] = data
49
51
  end
50
52
  end
51
53
 
52
- private
54
+ class << self
55
+ # Grab the attribute mapping for the class and its superclass
56
+ # (as superclass may present common mappings for multiple classes)
57
+ def attribute_mapping
58
+ return nil unless name
53
59
 
54
- def self.attribute_mapping
55
- return nil unless self.name
56
- ChgkRating::AttributeMappings.find self.name
60
+ ChgkRating::AttributeMappings.find(name).
61
+ merge(ChgkRating::AttributeMappings.find(superclass.name))
62
+ end
57
63
  end
58
64
 
65
+ private
66
+
59
67
  def lazy_load?(params)
60
68
  self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
61
69
  end
@@ -80,11 +88,22 @@ module ChgkRating
80
88
 
81
89
  def extract_from(raw_data)
82
90
  self.class.attribute_mapping.each do |attr, mapping|
83
- data = raw_data.is_a?(self.class) ? raw_data.send(attr) : raw_data[ mapping[:raw_name] ]
84
- data = mapping[:transform_up].call(data) if mapping.has_key?(:transform_up)
85
- instance_variable_set "@#{attr}", data
91
+ data = get_data_from raw_data, attr, mapping
92
+ next unless data
93
+
94
+ instance_variable_set "@#{attr}", transform_up(data, mapping)
86
95
  end
87
96
  end
97
+
98
+ def get_data_from(raw, attr, mapping)
99
+ raw.is_a?(self.class) ? raw.send(attr) : raw[mapping[:raw_name]]
100
+ end
101
+
102
+ def transform_up(data, mapping)
103
+ return data unless mapping.key?(:transform_up)
104
+
105
+ mapping[:transform_up].call(data)
106
+ end
88
107
  end
89
108
  end
90
- end
109
+ end
@@ -1,6 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class Player < Base
6
+ # Returns rating for the current Player in a given release
7
+ #
8
+ # @return [ChgkRating::Models::PlayerRating] The requested rating.
9
+ # @param release_id [String or Integer] Release to load rating for.
10
+ def rating(release_id)
11
+ ChgkRating::Models::PlayerRating.new release_id, player: self
12
+ end
13
+
14
+ # Returns an array-like ratings collection for the current Player.
15
+ #
16
+ # @return [ChgkRating::Collection::PlayerRatings] The collection of ratings.
17
+ def ratings
18
+ ChgkRating::Collections::PlayerRatings.new player: self
19
+ end
20
+
21
+ # Returns a collection of Tournaments that the current player participated at based on the given criteria
22
+ #
23
+ # @raise [ChgkRating::Error::NotFound] Error raised when nothing can be found based on the given criteria.
24
+ # @return [ChgkRating::Collection::PlayerTournaments] The collection of tournaments.
25
+ # @param season_id [String or Integer] Season to load tournaments for
26
+ # @option params [String or Integer] :page The requested page. Default is 1
27
+ def tournaments(season_id = nil)
28
+ ChgkRating::Collections::PlayerTournaments.new player: self, season_id: season_id
29
+ end
30
+
4
31
  private
5
32
 
6
33
  def api_path
@@ -8,4 +35,4 @@ module ChgkRating
8
35
  end
9
36
  end
10
37
  end
11
- end
38
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChgkRating
4
+ module Models
5
+ class PlayerRating < Rating
6
+ def initialize(release_id_or_hash, params = {})
7
+ @player_id = extract_id_from params[:player], ChgkRating::Models::Player
8
+ super
9
+ end
10
+
11
+ private
12
+
13
+ def api_path
14
+ "players/#{@player_id}/rating"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
- class TournamentPlayer < Base
5
+ class Rating < Base
4
6
  no_eager_loading!
5
7
  no_lazy_support!
6
8
  end
7
9
  end
8
- end
10
+ end
@@ -1,9 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
- class Rating < Base
4
- no_eager_loading!
5
- no_lazy_support!
6
-
5
+ class TeamRating < Rating
7
6
  def initialize(release_id_or_hash, params = {})
8
7
  @team_id = extract_id_from params[:team]
9
8
  super
@@ -16,4 +15,4 @@ module ChgkRating
16
15
  end
17
16
  end
18
17
  end
19
- end
18
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class Recap < Base
@@ -16,4 +18,4 @@ module ChgkRating
16
18
  end
17
19
  end
18
20
  end
19
- end
21
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class Team < Base
@@ -19,19 +21,19 @@ module ChgkRating
19
21
  ChgkRating::Models::TournamentTeam.new self, tournament: tournament_or_id, lazy: true
20
22
  end
21
23
 
22
- # Returns Rating for the current Team in a given release
24
+ # Returns rating for the current Team in a given release
23
25
  #
24
- # @return [ChgkRating::Models::Rating] The requested Rating.
26
+ # @return [ChgkRating::Models::TeamRating] The requested rating.
25
27
  # @param release_id [String or Integer] Release to load rating for.
26
28
  def rating(release_id)
27
- ChgkRating::Models::Rating.new release_id, team: self
29
+ ChgkRating::Models::TeamRating.new release_id, team: self
28
30
  end
29
31
 
30
- # Returns an array-like Ratings collection for the current team.
32
+ # Returns an array-like ratings collection for the current Team.
31
33
  #
32
- # @return [ChgkRating::Collection::Ratings] The collection of ratings.
34
+ # @return [ChgkRating::Collection::TeamRatings] The collection of ratings.
33
35
  def ratings
34
- ChgkRating::Collections::Ratings.new team: self
36
+ ChgkRating::Collections::TeamRatings.new team: self
35
37
  end
36
38
 
37
39
  # Returns an hash-like Recaps collection for the current team, grouped by seasons. Seasons act
@@ -59,4 +61,4 @@ module ChgkRating
59
61
  end
60
62
  end
61
63
  end
62
- end
64
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChgkRating
4
+ module Models
5
+ class PlayerTournament < Base
6
+ no_eager_loading!
7
+ no_lazy_support!
8
+ end
9
+ end
10
+ end
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class Tournament < Base
4
- # Returns an array-like TournamentPlayers collection containing roster for a team at the current tournament.
6
+ # Returns an array-like TournamentTeamPlayers collection containing roster for a team at the current tournament.
5
7
  #
6
8
  # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
7
- # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
9
+ # @return [ChgkRating::Collection::TournamentTeamPlayers] The collection of results.
8
10
  # @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load players for.
9
11
  def team_players(team_or_id)
10
- ChgkRating::Collections::TournamentPlayers.new tournament: self, team: team_or_id
12
+ ChgkRating::Collections::TournamentTeamPlayers.new tournament: self, team: team_or_id
11
13
  end
12
14
 
13
15
  # Returns an array-like TournamentTeamResults collection with results for a given team in the current
@@ -32,7 +34,7 @@ module ChgkRating
32
34
  # @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
33
35
  # @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
34
36
  # @param team_or_id [String, Integer or Team] Team to search for.
35
- def team(team_or_id)
37
+ def team_by(team_or_id)
36
38
  ChgkRating::Models::TournamentTeam.new team_or_id, tournament: self, lazy: true
37
39
  end
38
40
 
@@ -43,4 +45,4 @@ module ChgkRating
43
45
  end
44
46
  end
45
47
  end
46
- end
48
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class TournamentTeam < Base
@@ -11,11 +13,11 @@ module ChgkRating
11
13
  super extract_id_from(team_or_hash), params
12
14
  end
13
15
 
14
- # Returns an array-like TournamentPlayers collection containing roster for the current TournamentTeam
16
+ # Returns an array-like TournamentTeamPlayers collection containing roster for the current TournamentTeam
15
17
  #
16
- # @return [ChgkRating::Collection::TournamentPlayers] The collection of results.
18
+ # @return [ChgkRating::Collection::TournamentTeamPlayers] The collection of results.
17
19
  def players
18
- ChgkRating::Collections::TournamentPlayers.new tournament: @tournament, team: @team
20
+ ChgkRating::Collections::TournamentTeamPlayers.new tournament: @tournament, team: @team
19
21
  end
20
22
 
21
23
  # Returns an array-like TournamentTeamResults collection containing results for the current TournamentTeam
@@ -26,4 +28,4 @@ module ChgkRating
26
28
  end
27
29
  end
28
30
  end
29
- end
31
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChgkRating
4
+ module Models
5
+ class TournamentTeamPlayer < Base
6
+ no_eager_loading!
7
+ no_lazy_support!
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Models
3
5
  class TournamentTeamResult < Base
@@ -5,4 +7,4 @@ module ChgkRating
5
7
  no_lazy_support!
6
8
  end
7
9
  end
8
- end
10
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Request
3
5
  include ChgkRating::Connection
@@ -16,17 +18,16 @@ module ChgkRating
16
18
  end
17
19
 
18
20
  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
21
+ body = MultiJson.load response.body
22
+ raise MultiJson::ParseError if body.respond_to?(:has_key?) && body.key?('error')
23
+
24
+ body
25
+ rescue MultiJson::ParseError
26
+ respond_with_error response.status, response.body
26
27
  end
27
28
 
28
29
  def respond_with_error(code, body)
29
- fail ChgkRating::Error::ERRORS[code].from_response(body)
30
+ raise ChgkRating::Error::ERRORS[code].from_response(body)
30
31
  end
31
32
  end
32
- end
33
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Initial code taken from Facets gem by Rubyworks
2
4
  # https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
3
5
 
@@ -6,11 +8,11 @@ class String
6
8
  # replaced by underscores.
7
9
  def snakecase_upcase
8
10
  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
11
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
12
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
13
+ tr('-', '_').
14
+ gsub(/\s/, '_').
15
+ gsub(/__+/, '_').
16
+ upcase
15
17
  end
16
- end
18
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
4
  module Utils
3
5
  module Transformations
@@ -6,10 +8,10 @@ module ChgkRating
6
8
  up = 'integer' if up.nil? || up.empty?
7
9
  down = 'string' if down.nil? || down.empty?
8
10
 
9
- %i(up down).inject({}) do |result, t|
11
+ %i[up down].reduce({}) do |result, t|
10
12
  current_transformer = binding.local_variable_get t
11
13
  result.merge({
12
- "transform_#{t}".to_sym => send(current_transformer)
14
+ "transform_#{t}".to_sym => send(current_transformer)
13
15
  })
14
16
  end
15
17
  end
@@ -24,10 +26,12 @@ module ChgkRating
24
26
  private
25
27
 
26
28
  def chgk_object(namespace, type = 'Models')
27
- ->(d) do
28
- opts = type == 'Models' ?
29
- [d, {lazy: true}] :
30
- [{collection: d, lazy: true}]
29
+ lambda do |d|
30
+ opts = if type == 'Models'
31
+ [d, {lazy: true}]
32
+ else
33
+ [{collection: d, lazy: true}]
34
+ end
31
35
 
32
36
  Module.const_get("ChgkRating::#{type}::#{namespace}").new(*opts)
33
37
  end
@@ -42,42 +46,45 @@ module ChgkRating
42
46
  end
43
47
 
44
48
  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
+ lambda do |d|
50
+ if iterate
51
+ d.map { |obj| obj.send method }
52
+ else
53
+ d.send(method)
54
+ end
49
55
  end
50
56
  end
51
57
  end
52
58
 
53
59
  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')
60
+ string: to_star,
61
+ integer: to_star(:to_i),
62
+ float: to_star(:to_f),
63
+ id: to_star(:id),
64
+ ids: to_star(:id, true),
65
+ sym: to_star(:to_sym),
66
+ strdate: to_star(:to_s_chgk),
67
+ uri: ->(d) { URI.parse_safely d },
68
+ boolean: to_boolean,
69
+ binboolean: to_binary_boolean,
70
+ date: ->(d) { Date.parse_safely d },
71
+ datetime: ->(d) { DateTime.parse_safely d },
72
+ splitboolean: lambda do |d|
73
+ d&.split('')&.map { |result| to_boolean.call(result) }
74
+ end,
75
+ arraystrboolean: lambda do |d|
76
+ d&.map { |result| to_binary_boolean.call(result) }
77
+ end,
78
+ arrayboolean: lambda do |d|
79
+ d&.map { |result| to_boolean.call(result) }
80
+ end,
81
+ team: chgk_object('Team'),
82
+ tournament: chgk_object('Tournament'),
83
+ player: chgk_object('Player'),
84
+ players: chgk_object('Players', 'Collections')
78
85
  }.freeze
79
86
 
80
87
  load_transformers!
81
88
  end
82
89
  end
83
- end
90
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ChgkRating
2
- VERSION = '1.0.0'.freeze
3
- end
4
+ VERSION = '2.2.0'
5
+ end