mlb 0.9.0 → 0.11.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 (165) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -1
  3. data/LICENSE.txt +1 -1
  4. data/README.md +329 -32
  5. data/lib/mlb/affiliates.rb +28 -0
  6. data/lib/mlb/alumni.rb +30 -0
  7. data/lib/mlb/attendance.rb +28 -0
  8. data/lib/mlb/attendance_record.rb +157 -0
  9. data/lib/mlb/award.rb +126 -0
  10. data/lib/mlb/awards.rb +32 -0
  11. data/lib/mlb/baseball_stat.rb +65 -0
  12. data/lib/mlb/baseball_stats.rb +12 -0
  13. data/lib/mlb/boxscore.rb +81 -0
  14. data/lib/mlb/boxscore_team_stats.rb +210 -0
  15. data/lib/mlb/client.rb +71 -1
  16. data/lib/mlb/coaches.rb +28 -0
  17. data/lib/mlb/code_description_type.rb +45 -0
  18. data/lib/mlb/collection.rb +48 -0
  19. data/lib/mlb/comparable_by_attribute.rb +56 -0
  20. data/lib/mlb/conference.rb +39 -0
  21. data/lib/mlb/conferences.rb +33 -0
  22. data/lib/mlb/connection.rb +130 -7
  23. data/lib/mlb/context_metrics.rb +90 -0
  24. data/lib/mlb/division.rb +26 -6
  25. data/lib/mlb/divisions.rb +24 -18
  26. data/lib/mlb/draft.rb +83 -0
  27. data/lib/mlb/draft_pick.rb +155 -0
  28. data/lib/mlb/error_handler.rb +21 -1
  29. data/lib/mlb/errors/bad_gateway.rb +6 -1
  30. data/lib/mlb/errors/bad_request.rb +1 -0
  31. data/lib/mlb/errors/client_error.rb +1 -0
  32. data/lib/mlb/errors/connection_exception.rb +1 -0
  33. data/lib/mlb/errors/error.rb +1 -0
  34. data/lib/mlb/errors/forbidden.rb +1 -0
  35. data/lib/mlb/errors/gateway_timeout.rb +6 -1
  36. data/lib/mlb/errors/gone.rb +1 -0
  37. data/lib/mlb/errors/http_error.rb +22 -2
  38. data/lib/mlb/errors/internal_server_error.rb +1 -0
  39. data/lib/mlb/errors/network_error.rb +6 -1
  40. data/lib/mlb/errors/not_acceptable.rb +1 -0
  41. data/lib/mlb/errors/not_found.rb +1 -0
  42. data/lib/mlb/errors/payload_too_large.rb +1 -0
  43. data/lib/mlb/errors/retryable.rb +15 -0
  44. data/lib/mlb/errors/server_error.rb +1 -0
  45. data/lib/mlb/errors/service_unavailable.rb +6 -1
  46. data/lib/mlb/errors/too_many_redirects.rb +1 -0
  47. data/lib/mlb/errors/too_many_requests.rb +6 -1
  48. data/lib/mlb/errors/unauthorized.rb +1 -0
  49. data/lib/mlb/errors/unprocessable_entity.rb +1 -0
  50. data/lib/mlb/event_type.rb +61 -0
  51. data/lib/mlb/event_types.rb +12 -0
  52. data/lib/mlb/free_agent.rb +78 -0
  53. data/lib/mlb/free_agents.rb +34 -0
  54. data/lib/mlb/game_changes.rb +87 -0
  55. data/lib/mlb/game_content.rb +251 -0
  56. data/lib/mlb/game_data.rb +155 -0
  57. data/lib/mlb/game_pace.rb +173 -0
  58. data/lib/mlb/game_status.rb +94 -0
  59. data/lib/mlb/game_statuses.rb +12 -0
  60. data/lib/mlb/game_type.rb +98 -0
  61. data/lib/mlb/game_types.rb +12 -0
  62. data/lib/mlb/handedness.rb +30 -9
  63. data/lib/mlb/high_low.rb +121 -0
  64. data/lib/mlb/hit_trajectories.rb +12 -0
  65. data/lib/mlb/hit_trajectory.rb +6 -0
  66. data/lib/mlb/home_run_derby.rb +285 -0
  67. data/lib/mlb/id_description_type.rb +44 -0
  68. data/lib/mlb/inning_score.rb +87 -0
  69. data/lib/mlb/job.rb +58 -0
  70. data/lib/mlb/job_type.rb +39 -0
  71. data/lib/mlb/job_types.rb +12 -0
  72. data/lib/mlb/jobs.rb +87 -0
  73. data/lib/mlb/language.rb +48 -0
  74. data/lib/mlb/languages.rb +12 -0
  75. data/lib/mlb/leader.rb +79 -0
  76. data/lib/mlb/leaders.rb +68 -0
  77. data/lib/mlb/league.rb +97 -14
  78. data/lib/mlb/league_leader_type.rb +21 -0
  79. data/lib/mlb/league_leader_types.rb +12 -0
  80. data/lib/mlb/leagues.rb +24 -18
  81. data/lib/mlb/linescore.rb +342 -0
  82. data/lib/mlb/linescore_teams.rb +89 -0
  83. data/lib/mlb/live_feed.rb +130 -0
  84. data/lib/mlb/logical_event.rb +17 -0
  85. data/lib/mlb/logical_events.rb +12 -0
  86. data/lib/mlb/metric.rb +48 -0
  87. data/lib/mlb/metrics.rb +12 -0
  88. data/lib/mlb/people_changes.rb +34 -0
  89. data/lib/mlb/personnel.rb +28 -0
  90. data/lib/mlb/pitch_code.rb +90 -0
  91. data/lib/mlb/pitch_codes.rb +12 -0
  92. data/lib/mlb/pitch_type.rb +6 -0
  93. data/lib/mlb/pitch_types.rb +12 -0
  94. data/lib/mlb/platform.rb +30 -0
  95. data/lib/mlb/platforms.rb +12 -0
  96. data/lib/mlb/play.rb +300 -0
  97. data/lib/mlb/play_by_play.rb +52 -0
  98. data/lib/mlb/player.rb +186 -0
  99. data/lib/mlb/player_game_stats.rb +350 -0
  100. data/lib/mlb/player_stat.rb +70 -0
  101. data/lib/mlb/players.rb +42 -11
  102. data/lib/mlb/position.rb +68 -0
  103. data/lib/mlb/positions.rb +12 -0
  104. data/lib/mlb/postseason_schedule.rb +41 -0
  105. data/lib/mlb/postseason_series.rb +54 -0
  106. data/lib/mlb/redirect_handler.rb +80 -21
  107. data/lib/mlb/request_builder.rb +33 -2
  108. data/lib/mlb/review_reason.rb +6 -0
  109. data/lib/mlb/review_reasons.rb +12 -0
  110. data/lib/mlb/roster.rb +16 -12
  111. data/lib/mlb/roster_entry.rb +38 -0
  112. data/lib/mlb/roster_type.rb +39 -0
  113. data/lib/mlb/roster_types.rb +12 -0
  114. data/lib/mlb/schedule.rb +33 -0
  115. data/lib/mlb/schedule_date.rb +37 -0
  116. data/lib/mlb/schedule_event_type.rb +25 -0
  117. data/lib/mlb/schedule_event_types.rb +12 -0
  118. data/lib/mlb/scheduled_game.rb +321 -0
  119. data/lib/mlb/season.rb +74 -0
  120. data/lib/mlb/season_date_info.rb +4 -0
  121. data/lib/mlb/seasons.rb +36 -0
  122. data/lib/mlb/single_team_stats.rb +33 -0
  123. data/lib/mlb/situation_code.rb +116 -0
  124. data/lib/mlb/situation_codes.rb +12 -0
  125. data/lib/mlb/skies.rb +12 -0
  126. data/lib/mlb/sky.rb +6 -0
  127. data/lib/mlb/sport.rb +18 -5
  128. data/lib/mlb/sport_players.rb +30 -0
  129. data/lib/mlb/sports.rb +17 -7
  130. data/lib/mlb/standings.rb +47 -0
  131. data/lib/mlb/standings_record.rb +108 -0
  132. data/lib/mlb/standings_type.rb +25 -0
  133. data/lib/mlb/standings_types.rb +12 -0
  134. data/lib/mlb/stat_group.rb +21 -0
  135. data/lib/mlb/stat_groups.rb +12 -0
  136. data/lib/mlb/stat_type.rb +21 -0
  137. data/lib/mlb/stat_types.rb +12 -0
  138. data/lib/mlb/stat_values.rb +158 -0
  139. data/lib/mlb/stats.rb +41 -0
  140. data/lib/mlb/status.rb +17 -0
  141. data/lib/mlb/streaks.rb +184 -0
  142. data/lib/mlb/team.rb +66 -7
  143. data/lib/mlb/team_history.rb +28 -0
  144. data/lib/mlb/team_leader.rb +59 -0
  145. data/lib/mlb/team_leaders.rb +45 -0
  146. data/lib/mlb/team_record.rb +285 -0
  147. data/lib/mlb/team_stat.rb +50 -0
  148. data/lib/mlb/team_stats.rb +52 -0
  149. data/lib/mlb/teams.rb +28 -18
  150. data/lib/mlb/tied_games.rb +32 -0
  151. data/lib/mlb/transaction.rb +128 -0
  152. data/lib/mlb/transactions.rb +11 -6
  153. data/lib/mlb/uniforms.rb +97 -0
  154. data/lib/mlb/utils.rb +45 -0
  155. data/lib/mlb/venue.rb +10 -0
  156. data/lib/mlb/venues.rb +28 -18
  157. data/lib/mlb/version.rb +2 -1
  158. data/lib/mlb/win_probability.rb +64 -0
  159. data/lib/mlb/wind_direction.rb +6 -0
  160. data/lib/mlb/wind_directions.rb +12 -0
  161. data/lib/mlb.rb +64 -0
  162. data/sig/equalizer.rbs +3 -0
  163. data/sig/mlb.rbs +1972 -103
  164. data/sig/shale.rbs +29 -0
  165. metadata +137 -14
data/lib/mlb/sky.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative "code_description_type"
2
+
3
+ module MLB
4
+ # Represents a sky condition (weather)
5
+ class Sky < CodeDescriptionType; end
6
+ end
data/lib/mlb/sport.rb CHANGED
@@ -1,8 +1,19 @@
1
+ require "equalizer"
1
2
  require "shale"
3
+ require_relative "comparable_by_attribute"
2
4
 
3
5
  module MLB
6
+ # Represents a sport (e.g., MLB, Minor League Baseball)
4
7
  class Sport < Shale::Mapper
5
8
  include Comparable
9
+ include ComparableByAttribute
10
+ include Equalizer.new(:id)
11
+
12
+ # Returns the attribute used for sorting
13
+ #
14
+ # @api private
15
+ # @return [Symbol] the attribute used for comparison
16
+ def comparable_attribute = :sort_order
6
17
 
7
18
  attribute :id, Shale::Type::Integer
8
19
  attribute :code, Shale::Type::String
@@ -12,7 +23,13 @@ module MLB
12
23
  attribute :sort_order, Shale::Type::Integer
13
24
  attribute :active, Shale::Type::Boolean
14
25
 
15
- alias_method :active?, :active
26
+ # Checks if the sport is active
27
+ #
28
+ # @api public
29
+ # @example
30
+ # sport.active? #=> true
31
+ # @return [Boolean] whether the sport is active
32
+ def active? = active
16
33
 
17
34
  json do
18
35
  map "id", to: :id
@@ -23,9 +40,5 @@ module MLB
23
40
  map "sortOrder", to: :sort_order
24
41
  map "activeStatus", to: :active
25
42
  end
26
-
27
- def <=>(other)
28
- sort_order <=> other.sort_order
29
- end
30
43
  end
31
44
  end
@@ -0,0 +1,30 @@
1
+ require "shale"
2
+ require_relative "player"
3
+
4
+ module MLB
5
+ # Provides methods for fetching players for a sport from the API
6
+ class SportPlayers < Shale::Mapper
7
+ attribute :people, Player, collection: true
8
+
9
+ json do
10
+ map "people", to: :people
11
+ end
12
+
13
+ # Retrieves all players for a sport
14
+ #
15
+ # @api public
16
+ # @example Get all MLB players for a season
17
+ # MLB::SportPlayers.all(season: 2024)
18
+ # @example Get all players with a sport object
19
+ # MLB::SportPlayers.all(season: 2024, sport: Sport.new(id: 1))
20
+ # @param season [Integer, nil] the season year (defaults to current year)
21
+ # @param sport [Integer, Sport] the sport ID or Sport object
22
+ # @return [Array<Player>] the players
23
+ def self.all(season: nil, sport: Utils::DEFAULT_SPORT_ID)
24
+ season ||= Utils.current_season
25
+ sport_id = Utils.extract_id(sport)
26
+ response = CLIENT.get("sports/#{sport_id}/players?#{Utils.build_query(season:)}")
27
+ from_json(response).people
28
+ end
29
+ end
30
+ end
data/lib/mlb/sports.rb CHANGED
@@ -2,21 +2,31 @@ require "shale"
2
2
  require_relative "sport"
3
3
 
4
4
  module MLB
5
+ # Collection of sports from the MLB Stats API
5
6
  class Sports < Shale::Mapper
6
- attribute :copyright, Shale::Type::String
7
7
  attribute :sports, Sport, collection: true
8
8
 
9
+ # Retrieves all sports
10
+ #
11
+ # @api public
12
+ # @example
13
+ # MLB::Sports.all
14
+ # @return [Array<Sport>] list of all sports
9
15
  def self.all
10
16
  response = CLIENT.get("sports")
11
- sports = from_json(response)
12
- sports.sports.sort!
17
+ from_json(response).sports.sort
13
18
  end
14
19
 
20
+ # Finds a sport by ID
21
+ #
22
+ # @api public
23
+ # @example
24
+ # MLB::Sports.find(1)
25
+ # @param sport [Integer, Sport] the sport ID or Sport object
26
+ # @return [Sport, nil] the sport if found
15
27
  def self.find(sport)
16
- id = sport.respond_to?(:id) ? sport.id : sport
17
- response = CLIENT.get("sports/#{id}")
18
- sports = from_json(response)
19
- sports.sports.first
28
+ response = CLIENT.get("sports/#{Utils.extract_id(sport)}")
29
+ from_json(response).sports.min_by { |s| s.sort_order || 0 }
20
30
  end
21
31
  end
22
32
  end
@@ -0,0 +1,47 @@
1
+ require "shale"
2
+ require_relative "standings_record"
3
+
4
+ module MLB
5
+ # Provides methods for fetching standings from the API
6
+ class Standings < Shale::Mapper
7
+ # Default league IDs for American League (103) and National League (104)
8
+ DEFAULT_LEAGUE_IDS = [103, 104].freeze
9
+
10
+ attribute :records, StandingsRecord, collection: true
11
+
12
+ json do
13
+ map "records", to: :records
14
+ end
15
+
16
+ # Retrieves standings for the given leagues
17
+ #
18
+ # @api public
19
+ # @example Get MLB standings
20
+ # MLB::Standings.all
21
+ # @example Get standings for a specific season
22
+ # MLB::Standings.all(season: 2023)
23
+ # @param season [Integer, nil] the season year (defaults to current year)
24
+ # @param league_ids [Array<Integer>] league IDs (default: [103, 104] for AL and NL)
25
+ # @return [Array<StandingsRecord>] the standings records
26
+ def self.all(season: nil, league_ids: DEFAULT_LEAGUE_IDS)
27
+ season ||= Utils.current_season
28
+ params = {leagueId: league_ids.join(","), season:}
29
+ response = CLIENT.get("standings?#{Utils.build_query(params)}")
30
+ from_json(response).records
31
+ end
32
+
33
+ # Retrieves standings for a specific division
34
+ #
35
+ # @api public
36
+ # @example Get AL East standings
37
+ # MLB::Standings.find(division: 201)
38
+ # @param division [Division, Integer] the division or division ID
39
+ # @param season [Integer, nil] the season year (defaults to current year)
40
+ # @param league_ids [Array<Integer>] league IDs (default: [103, 104] for AL and NL)
41
+ # @return [StandingsRecord, nil] the standings record for the division
42
+ def self.find(division:, season: nil, league_ids: DEFAULT_LEAGUE_IDS)
43
+ division_id = Utils.extract_id(division)
44
+ all(season:, league_ids:).find { |record| record.division&.id.eql?(division_id) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,108 @@
1
+ require "shale"
2
+ require_relative "division"
3
+ require_relative "league"
4
+ require_relative "sport"
5
+ require_relative "team_record"
6
+
7
+ module MLB
8
+ # Represents a standings record for a division
9
+ class StandingsRecord < Shale::Mapper
10
+ STANDINGS_REGULAR_SEASON = "regularSeason".freeze
11
+ STANDINGS_WILD_CARD = "wildCard".freeze
12
+ STANDINGS_DIVISION_LEADERS = "divisionLeaders".freeze
13
+ STANDINGS_WILD_CARD_WITH_LEADERS = "wildCardWithLeaders".freeze
14
+ STANDINGS_FIRST_HALF = "firstHalf".freeze
15
+ STANDINGS_SECOND_HALF = "secondHalf".freeze
16
+ STANDINGS_SPRING_TRAINING = "springTraining".freeze
17
+ STANDINGS_POSTSEASON = "postseason".freeze
18
+
19
+ # @!attribute [rw] standings_type
20
+ # Returns the standings type
21
+ # @api public
22
+ # @example
23
+ # standings_record.standings_type #=> "regularSeason"
24
+ # @return [String] the standings type
25
+ attribute :standings_type, Shale::Type::String
26
+
27
+ # @!attribute [rw] league
28
+ # Returns the league
29
+ # @api public
30
+ # @example
31
+ # standings_record.league #=> #<MLB::League>
32
+ # @return [League] the league
33
+ attribute :league, League
34
+
35
+ # @!attribute [rw] division
36
+ # Returns the division
37
+ # @api public
38
+ # @example
39
+ # standings_record.division #=> #<MLB::Division>
40
+ # @return [Division] the division
41
+ attribute :division, Division
42
+
43
+ # @!attribute [rw] sport
44
+ # Returns the sport
45
+ # @api public
46
+ # @example
47
+ # standings_record.sport #=> #<MLB::Sport>
48
+ # @return [Sport] the sport
49
+ attribute :sport, Sport
50
+
51
+ # @!attribute [rw] last_updated
52
+ # Returns when the standings were last updated
53
+ # @api public
54
+ # @example
55
+ # standings_record.last_updated #=> "2024-09-23T02:30:10.088Z"
56
+ # @return [String] when the standings were last updated
57
+ attribute :last_updated, Shale::Type::String
58
+
59
+ # @!attribute [rw] team_records
60
+ # Returns the team records
61
+ # @api public
62
+ # @example
63
+ # standings_record.team_records #=> [#<MLB::TeamRecord>, ...]
64
+ # @return [Array<TeamRecord>] the team records
65
+ attribute :team_records, TeamRecord, collection: true
66
+
67
+ # Returns whether this is regular season standings
68
+ #
69
+ # @api public
70
+ # @example
71
+ # standings_record.regular_season? #=> true
72
+ # @return [Boolean] whether this is regular season standings
73
+ def regular_season? = standings_type.eql?(STANDINGS_REGULAR_SEASON)
74
+
75
+ # Returns whether this is wild card standings
76
+ #
77
+ # @api public
78
+ # @example
79
+ # standings_record.wild_card? #=> false
80
+ # @return [Boolean] whether this is wild card standings
81
+ def wild_card? = standings_type.eql?(STANDINGS_WILD_CARD)
82
+
83
+ # Returns whether this is spring training standings
84
+ #
85
+ # @api public
86
+ # @example
87
+ # standings_record.spring_training? #=> false
88
+ # @return [Boolean] whether this is spring training standings
89
+ def spring_training? = standings_type.eql?(STANDINGS_SPRING_TRAINING)
90
+
91
+ # Returns whether this is postseason standings
92
+ #
93
+ # @api public
94
+ # @example
95
+ # standings_record.postseason? #=> false
96
+ # @return [Boolean] whether this is postseason standings
97
+ def postseason? = standings_type.eql?(STANDINGS_POSTSEASON)
98
+
99
+ json do
100
+ map "standingsType", to: :standings_type
101
+ map "league", to: :league
102
+ map "division", to: :division
103
+ map "sport", to: :sport
104
+ map "lastUpdated", to: :last_updated
105
+ map "teamRecords", to: :team_records
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,25 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a standings type
6
+ class StandingsType < Shale::Mapper
7
+ include Equalizer.new(:name)
8
+
9
+ # @!attribute [rw] name
10
+ # Returns the standings type name
11
+ # @api public
12
+ # @example
13
+ # standings_type.name #=> "regularSeason"
14
+ # @return [String] the standings type name
15
+ attribute :name, Shale::Type::String
16
+
17
+ # @!attribute [rw] description
18
+ # Returns the description
19
+ # @api public
20
+ # @example
21
+ # standings_type.description #=> "Regular Season Standings"
22
+ # @return [String] the description
23
+ attribute :description, Shale::Type::String
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "standings_type"
3
+
4
+ module MLB
5
+ # Provides methods for fetching standings types from the API
6
+ #
7
+ # @example Fetch all standings types
8
+ # MLB::StandingsTypes.all #=> [#<MLB::StandingsType>, ...]
9
+ class StandingsTypes < Collection
10
+ collection endpoint: "standingsTypes", item_type: StandingsType, collection_name: :standings_types
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a stat group
6
+ class StatGroup < Shale::Mapper
7
+ include Equalizer.new(:display_name)
8
+
9
+ # @!attribute [rw] display_name
10
+ # Returns the display name for the stat group
11
+ # @api public
12
+ # @example
13
+ # stat_group.display_name #=> "hitting"
14
+ # @return [String] the display name for the stat group
15
+ attribute :display_name, Shale::Type::String
16
+
17
+ json do
18
+ map "displayName", to: :display_name
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "stat_group"
3
+
4
+ module MLB
5
+ # Provides methods for fetching stat groups from the API
6
+ #
7
+ # @example Fetch all stat groups
8
+ # MLB::StatGroups.all #=> [#<MLB::StatGroup>, ...]
9
+ class StatGroups < Collection
10
+ collection endpoint: "statGroups", item_type: StatGroup, collection_name: :stat_groups
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a stat type
6
+ class StatType < Shale::Mapper
7
+ include Equalizer.new(:display_name)
8
+
9
+ # @!attribute [rw] display_name
10
+ # Returns the display name for the stat type
11
+ # @api public
12
+ # @example
13
+ # stat_type.display_name #=> "season"
14
+ # @return [String] the display name for the stat type
15
+ attribute :display_name, Shale::Type::String
16
+
17
+ json do
18
+ map "displayName", to: :display_name
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "stat_type"
3
+
4
+ module MLB
5
+ # Provides methods for fetching stat types from the API
6
+ #
7
+ # @example Fetch all stat types
8
+ # MLB::StatTypes.all #=> [#<MLB::StatType>, ...]
9
+ class StatTypes < Collection
10
+ collection endpoint: "statTypes", item_type: StatType, collection_name: :stat_types
11
+ end
12
+ end
@@ -0,0 +1,158 @@
1
+ require "shale"
2
+
3
+ module MLB
4
+ # Represents offensive stat values for players or teams
5
+ #
6
+ # @example Access player stats
7
+ # player_stat.stat.games_played #=> 162
8
+ # player_stat.stat.home_runs #=> 54
9
+ #
10
+ # @example Access team stats
11
+ # team_stat.stat.runs #=> 829
12
+ # team_stat.stat.ops #=> ".781"
13
+ class StatValues < Shale::Mapper
14
+ # @!attribute [rw] games_played
15
+ # Returns the number of games played
16
+ # @api public
17
+ # @example
18
+ # stat.games_played #=> 162
19
+ # @return [Integer] games played
20
+ attribute :games_played, Shale::Type::Integer
21
+
22
+ # @!attribute [rw] runs
23
+ # Returns the number of runs scored
24
+ # @api public
25
+ # @example
26
+ # stat.runs #=> 122
27
+ # @return [Integer] runs scored
28
+ attribute :runs, Shale::Type::Integer
29
+
30
+ # @!attribute [rw] doubles
31
+ # Returns the number of doubles
32
+ # @api public
33
+ # @example
34
+ # stat.doubles #=> 28
35
+ # @return [Integer] doubles
36
+ attribute :doubles, Shale::Type::Integer
37
+
38
+ # @!attribute [rw] triples
39
+ # Returns the number of triples
40
+ # @api public
41
+ # @example
42
+ # stat.triples #=> 3
43
+ # @return [Integer] triples
44
+ attribute :triples, Shale::Type::Integer
45
+
46
+ # @!attribute [rw] home_runs
47
+ # Returns the number of home runs
48
+ # @api public
49
+ # @example
50
+ # stat.home_runs #=> 54
51
+ # @return [Integer] home runs
52
+ attribute :home_runs, Shale::Type::Integer
53
+
54
+ # @!attribute [rw] strike_outs
55
+ # Returns the number of strikeouts
56
+ # @api public
57
+ # @example
58
+ # stat.strike_outs #=> 175
59
+ # @return [Integer] strikeouts
60
+ attribute :strike_outs, Shale::Type::Integer
61
+
62
+ # @!attribute [rw] base_on_balls
63
+ # Returns the number of walks
64
+ # @api public
65
+ # @example
66
+ # stat.base_on_balls #=> 111
67
+ # @return [Integer] walks
68
+ attribute :base_on_balls, Shale::Type::Integer
69
+
70
+ # @!attribute [rw] hits
71
+ # Returns the number of hits
72
+ # @api public
73
+ # @example
74
+ # stat.hits #=> 177
75
+ # @return [Integer] hits
76
+ attribute :hits, Shale::Type::Integer
77
+
78
+ # @!attribute [rw] avg
79
+ # Returns the batting average
80
+ # @api public
81
+ # @example
82
+ # stat.avg #=> ".311"
83
+ # @return [String] batting average
84
+ attribute :avg, Shale::Type::String
85
+
86
+ # @!attribute [rw] at_bats
87
+ # Returns the number of at bats
88
+ # @api public
89
+ # @example
90
+ # stat.at_bats #=> 570
91
+ # @return [Integer] at bats
92
+ attribute :at_bats, Shale::Type::Integer
93
+
94
+ # @!attribute [rw] obp
95
+ # Returns the on-base percentage
96
+ # @api public
97
+ # @example
98
+ # stat.obp #=> ".425"
99
+ # @return [String] on-base percentage
100
+ attribute :obp, Shale::Type::String
101
+
102
+ # @!attribute [rw] slg
103
+ # Returns the slugging percentage
104
+ # @api public
105
+ # @example
106
+ # stat.slg #=> ".686"
107
+ # @return [String] slugging percentage
108
+ attribute :slg, Shale::Type::String
109
+
110
+ # @!attribute [rw] ops
111
+ # Returns the on-base plus slugging
112
+ # @api public
113
+ # @example
114
+ # stat.ops #=> "1.111"
115
+ # @return [String] on-base plus slugging
116
+ attribute :ops, Shale::Type::String
117
+
118
+ # @!attribute [rw] stolen_bases
119
+ # Returns the number of stolen bases
120
+ # @api public
121
+ # @example
122
+ # stat.stolen_bases #=> 9
123
+ # @return [Integer] stolen bases
124
+ attribute :stolen_bases, Shale::Type::Integer
125
+
126
+ # @!attribute [rw] rbi
127
+ # Returns the number of runs batted in
128
+ # @api public
129
+ # @example
130
+ # stat.rbi #=> 130
131
+ # @return [Integer] runs batted in
132
+ attribute :rbi, Shale::Type::Integer
133
+
134
+ json do
135
+ map "gamesPlayed", to: :games_played
136
+ map "runs", to: :runs
137
+ map "doubles", to: :doubles
138
+ map "triples", to: :triples
139
+ map "homeRuns", to: :home_runs
140
+ map "strikeOuts", to: :strike_outs
141
+ map "baseOnBalls", to: :base_on_balls
142
+ map "hits", to: :hits
143
+ map "avg", to: :avg
144
+ map "atBats", to: :at_bats
145
+ map "obp", to: :obp
146
+ map "slg", to: :slg
147
+ map "ops", to: :ops
148
+ map "stolenBases", to: :stolen_bases
149
+ map "rbi", to: :rbi
150
+ end
151
+ end
152
+
153
+ # @deprecated Use {StatValues} instead
154
+ PlayerStatValues = StatValues
155
+
156
+ # @deprecated Use {StatValues} instead
157
+ TeamStatValues = StatValues
158
+ end
data/lib/mlb/stats.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "shale"
2
+ require_relative "player_stat"
3
+
4
+ module MLB
5
+ # Represents a player stat group
6
+ class PlayerStatGroup < Shale::Mapper
7
+ attribute :splits, PlayerStat, collection: true
8
+
9
+ json do
10
+ map "splits", to: :splits
11
+ end
12
+ end
13
+
14
+ # Provides methods for fetching player stats from the API
15
+ class Stats < Shale::Mapper
16
+ attribute :stats, PlayerStatGroup, collection: true
17
+
18
+ json do
19
+ map "stats", to: :stats
20
+ end
21
+
22
+ # Retrieves player stats
23
+ #
24
+ # @api public
25
+ # @example Get player hitting stats
26
+ # MLB::Stats.find(season: 2024, group: "hitting")
27
+ # @example Get player pitching stats
28
+ # MLB::Stats.find(season: 2024, group: "pitching", stats: "season")
29
+ # @param season [Integer, nil] the season year (defaults to current year)
30
+ # @param group [String] the stat group (hitting, pitching, fielding)
31
+ # @param stats [String] the stats type (default: season)
32
+ # @param sport [Integer, Sport] the sport ID or Sport object
33
+ # @return [Array<PlayerStat>] the player stats
34
+ def self.find(season: nil, group: "hitting", stats: "season", sport: Utils::DEFAULT_SPORT_ID)
35
+ season ||= Utils.current_season
36
+ params = {season:, group:, stats:, sportIds: Utils.extract_id(sport)}
37
+ response = CLIENT.get("stats?#{Utils.build_query(params)}")
38
+ from_json(response).stats.first&.splits || []
39
+ end
40
+ end
41
+ end
data/lib/mlb/status.rb CHANGED
@@ -1,8 +1,25 @@
1
+ require "equalizer"
1
2
  require "shale"
2
3
 
3
4
  module MLB
5
+ # Represents a roster status
4
6
  class Status < Shale::Mapper
7
+ include Equalizer.new(:code)
8
+
9
+ # @!attribute [rw] code
10
+ # Returns the status code
11
+ # @api public
12
+ # @example
13
+ # status.code #=> "A"
14
+ # @return [String] the status code
5
15
  attribute :code, Shale::Type::String
16
+
17
+ # @!attribute [rw] description
18
+ # Returns the status description
19
+ # @api public
20
+ # @example
21
+ # status.description #=> "Active"
22
+ # @return [String] the status description
6
23
  attribute :description, Shale::Type::String
7
24
  end
8
25
  end