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,19 +1,19 @@
1
- module ChgkRating
2
- module Connection
3
- BASE_URL = 'http://rating.chgk.info/api'.freeze
4
-
5
- def connection
6
- options = {
7
- headers: {
8
- accept: 'application/json',
9
- user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
10
- },
11
- url: BASE_URL + '/'
12
- }
13
-
14
- Faraday.new options do |faraday|
15
- faraday.adapter Faraday.default_adapter
16
- end
17
- end
18
- end
1
+ module ChgkRating
2
+ module Connection
3
+ BASE_URL = 'http://rating.chgk.info/api'.freeze
4
+
5
+ def connection
6
+ options = {
7
+ headers: {
8
+ accept: 'application/json',
9
+ user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
10
+ },
11
+ url: BASE_URL + '/'
12
+ }
13
+
14
+ Faraday.new options do |faraday|
15
+ faraday.adapter Faraday.default_adapter
16
+ end
17
+ end
18
+ end
19
19
  end
@@ -1,46 +1,46 @@
1
- module ChgkRating
2
- class Error < StandardError
3
- ClientError = Class.new(self)
4
- ServerError = Class.new(self)
5
-
6
- EagerLoadingNotSupported = Class.new(ClientError) do
7
- def to_s
8
- 'Eager loading is not supported for this resource.'
9
- end
10
- end
11
-
12
- NotHashType = Class.new(ClientError) do
13
- def to_s
14
- 'This is not a hash-like collection, so it cannot be converted to an ordinary hash.'
15
- end
16
- end
17
-
18
- NotArrayType = Class.new(ClientError) do
19
- def to_s
20
- 'This is not an array-like collection, so it cannot be converted to an ordinary array.'
21
- end
22
- end
23
-
24
- BadRequest = Class.new(ServerError)
25
- NotFound = Class.new(ServerError)
26
- NotImplemented = Class.new(ServerError)
27
-
28
- ERRORS = {
29
- 400 => ChgkRating::Error::BadRequest,
30
- 404 => ChgkRating::Error::NotFound,
31
- 501 => ChgkRating::Error::NotImplemented
32
- }
33
-
34
- class << self
35
- # Create a new error from an HTTP response
36
- def from_response(body)
37
- new(body.to_s)
38
- end
39
- end
40
-
41
- # Initializes a new Error object
42
- def initialize(message = '')
43
- super(message)
44
- end
45
- end
1
+ module ChgkRating
2
+ class Error < StandardError
3
+ ClientError = Class.new(self)
4
+ ServerError = Class.new(self)
5
+
6
+ EagerLoadingNotSupported = Class.new(ClientError) do
7
+ def to_s
8
+ 'Eager loading is not supported for this resource.'
9
+ end
10
+ end
11
+
12
+ NotHashType = Class.new(ClientError) do
13
+ def to_s
14
+ 'This is not a hash-like collection, so it cannot be converted to an ordinary hash.'
15
+ end
16
+ end
17
+
18
+ NotArrayType = Class.new(ClientError) do
19
+ def to_s
20
+ 'This is not an array-like collection, so it cannot be converted to an ordinary array.'
21
+ end
22
+ end
23
+
24
+ BadRequest = Class.new(ServerError)
25
+ NotFound = Class.new(ServerError)
26
+ NotImplemented = Class.new(ServerError)
27
+
28
+ ERRORS = {
29
+ 400 => ChgkRating::Error::BadRequest,
30
+ 404 => ChgkRating::Error::NotFound,
31
+ 501 => ChgkRating::Error::NotImplemented
32
+ }
33
+
34
+ class << self
35
+ # Create a new error from an HTTP response
36
+ def from_response(body)
37
+ new(body.to_s)
38
+ end
39
+ end
40
+
41
+ # Initializes a new Error object
42
+ def initialize(message = '')
43
+ super(message)
44
+ end
45
+ end
46
46
  end
@@ -1,90 +1,90 @@
1
- module ChgkRating
2
- module Models
3
- class Base < ChgkObject
4
- include ChgkRating::Request
5
-
6
- attr_reader :lazy
7
-
8
- def self.inherited(subclass)
9
- attr_reader(*subclass.attribute_mapping.keys) if subclass.attribute_mapping
10
- super
11
- end
12
-
13
- def initialize(id_or_hash, params = {})
14
- raw = raw_by id_or_hash, lazy_load?(params)
15
-
16
- # If we only have an ID and `lazy` is set to true, then simply store this ID
17
- # Otherwise extract all data from the hash
18
- raw.nil? ? @id = id_or_hash : extract_from(raw)
19
-
20
- @lazy = (params[:lazy] || false) unless self.class.const_defined?(:NO_LAZY_SUPPORT)
21
- end
22
-
23
- # Load data from API if the resource was initially lazily loaded.
24
- # Set `force` to reload data even if it is already present.
25
- def eager_load!(force = false)
26
- return unless @lazy || force
27
- extract_from raw_by(self.id)
28
- @lazy = false
29
- self
30
- end
31
-
32
- def self.no_eager_loading!
33
- define_method :eager_load! do |*_args|
34
- raise ChgkRating::Error::EagerLoadingNotSupported
35
- end
36
- end
37
-
38
- def self.no_lazy_support!
39
- self.const_set :NO_LAZY_SUPPORT, true
40
- undef_method :lazy
41
- end
42
-
43
- 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
49
- end
50
- end
51
-
52
- private
53
-
54
- def self.attribute_mapping
55
- return nil unless self.name
56
- ChgkRating::AttributeMappings.find self.name
57
- end
58
-
59
- def lazy_load?(params)
60
- self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
61
- end
62
-
63
- # If the `object` is a `String` or `Integer` - that's an id.
64
- # If `lazy` is not set to `true` we need to perform an API request.
65
- # If `lazy` is set to `true` - do nothing,
66
- # we are going to only save this ID.
67
- # If the `object` is not a `String` or `Integer`, it already has all the necessary information and
68
- # so we simply create a new instance of the class using it.
69
- def raw_by(object, lazy = false)
70
- return object unless object.is_a?(String) || object.is_a?(Integer) || object.is_a?(Symbol)
71
- return nil if lazy
72
-
73
- get_result_by object
74
- end
75
-
76
- def get_result_by(object)
77
- result = get "#{api_path}/#{object}"
78
- result.is_a?(Array) ? result.first : result
79
- end
80
-
81
- def extract_from(raw_data)
82
- 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
86
- end
87
- end
88
- end
89
- end
1
+ module ChgkRating
2
+ module Models
3
+ class Base < ChgkObject
4
+ include ChgkRating::Request
5
+
6
+ attr_reader :lazy
7
+
8
+ def self.inherited(subclass)
9
+ attr_reader(*subclass.attribute_mapping.keys) if subclass.attribute_mapping
10
+ super
11
+ end
12
+
13
+ def initialize(id_or_hash, params = {})
14
+ raw = raw_by id_or_hash, lazy_load?(params)
15
+
16
+ # If we only have an ID and `lazy` is set to true, then simply store this ID
17
+ # Otherwise extract all data from the hash
18
+ raw.nil? ? @id = id_or_hash : extract_from(raw)
19
+
20
+ @lazy = (params[:lazy] || false) unless self.class.const_defined?(:NO_LAZY_SUPPORT)
21
+ end
22
+
23
+ # Load data from API if the resource was initially lazily loaded.
24
+ # Set `force` to reload data even if it is already present.
25
+ def eager_load!(force = false)
26
+ return unless @lazy || force
27
+ extract_from raw_by(self.id)
28
+ @lazy = false
29
+ self
30
+ end
31
+
32
+ def self.no_eager_loading!
33
+ define_method :eager_load! do |*_args|
34
+ raise ChgkRating::Error::EagerLoadingNotSupported
35
+ end
36
+ end
37
+
38
+ def self.no_lazy_support!
39
+ self.const_set :NO_LAZY_SUPPORT, true
40
+ undef_method :lazy
41
+ end
42
+
43
+ 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
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def self.attribute_mapping
55
+ return nil unless self.name
56
+ ChgkRating::AttributeMappings.find self.name
57
+ end
58
+
59
+ def lazy_load?(params)
60
+ self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
61
+ end
62
+
63
+ # If the `object` is a `String` or `Integer` - that's an id.
64
+ # If `lazy` is not set to `true` we need to perform an API request.
65
+ # If `lazy` is set to `true` - do nothing,
66
+ # we are going to only save this ID.
67
+ # If the `object` is not a `String` or `Integer`, it already has all the necessary information and
68
+ # so we simply create a new instance of the class using it.
69
+ def raw_by(object, lazy = false)
70
+ return object unless object.is_a?(String) || object.is_a?(Integer) || object.is_a?(Symbol)
71
+ return nil if lazy
72
+
73
+ get_result_by object
74
+ end
75
+
76
+ def get_result_by(object)
77
+ result = get "#{api_path}/#{object}"
78
+ result.is_a?(Array) ? result.first : result
79
+ end
80
+
81
+ def extract_from(raw_data)
82
+ 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
86
+ end
87
+ end
88
+ end
89
+ end
90
90
  end
@@ -1,11 +1,11 @@
1
- module ChgkRating
2
- module Models
3
- class Player < Base
4
- private
5
-
6
- def api_path
7
- 'players'
8
- end
9
- end
10
- end
1
+ module ChgkRating
2
+ module Models
3
+ class Player < Base
4
+ private
5
+
6
+ def api_path
7
+ 'players'
8
+ end
9
+ end
10
+ end
11
11
  end
@@ -1,19 +1,19 @@
1
- module ChgkRating
2
- module Models
3
- class Rating < Base
4
- no_eager_loading!
5
- no_lazy_support!
6
-
7
- def initialize(release_id_or_hash, params = {})
8
- @team_id = extract_id_from params[:team]
9
- super
10
- end
11
-
12
- private
13
-
14
- def api_path
15
- "teams/#{@team_id}/rating"
16
- end
17
- end
18
- end
1
+ module ChgkRating
2
+ module Models
3
+ class Rating < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+
7
+ def initialize(release_id_or_hash, params = {})
8
+ @team_id = extract_id_from params[:team]
9
+ super
10
+ end
11
+
12
+ private
13
+
14
+ def api_path
15
+ "teams/#{@team_id}/rating"
16
+ end
17
+ end
18
+ end
19
19
  end
@@ -1,19 +1,19 @@
1
- module ChgkRating
2
- module Models
3
- class Recap < Base
4
- no_eager_loading!
5
- no_lazy_support!
6
-
7
- def initialize(season_id_or_hash, params = {})
8
- @team_id = extract_id_from params[:team]
9
- super
10
- end
11
-
12
- private
13
-
14
- def api_path
15
- "teams/#{@team_id}/recaps"
16
- end
17
- end
18
- end
1
+ module ChgkRating
2
+ module Models
3
+ class Recap < Base
4
+ no_eager_loading!
5
+ no_lazy_support!
6
+
7
+ def initialize(season_id_or_hash, params = {})
8
+ @team_id = extract_id_from params[:team]
9
+ super
10
+ end
11
+
12
+ private
13
+
14
+ def api_path
15
+ "teams/#{@team_id}/recaps"
16
+ end
17
+ end
18
+ end
19
19
  end
@@ -1,62 +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
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
62
  end