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
@@ -0,0 +1,37 @@
1
+ require "shale"
2
+ require_relative "scheduled_game"
3
+
4
+ module MLB
5
+ # Represents a date within a schedule response
6
+ class ScheduleDate < Shale::Mapper
7
+ # @!attribute [rw] date
8
+ # Returns the date
9
+ # @api public
10
+ # @example
11
+ # schedule_date.date #=> #<Date: 2024-07-04>
12
+ # @return [Date] the date
13
+ attribute :date, Shale::Type::Date
14
+
15
+ # @!attribute [rw] total_games
16
+ # Returns the total number of games on this date
17
+ # @api public
18
+ # @example
19
+ # schedule_date.total_games #=> 15
20
+ # @return [Integer] the total number of games
21
+ attribute :total_games, Shale::Type::Integer
22
+
23
+ # @!attribute [rw] games
24
+ # Returns the games scheduled for this date
25
+ # @api public
26
+ # @example
27
+ # schedule_date.games #=> [#<MLB::ScheduledGame>, ...]
28
+ # @return [Array<ScheduledGame>] the games scheduled for this date
29
+ attribute :games, ScheduledGame, collection: true
30
+
31
+ json do
32
+ map "date", to: :date
33
+ map "totalGames", to: :total_games
34
+ map "games", to: :games
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a schedule event type
6
+ class ScheduleEventType < Shale::Mapper
7
+ include Equalizer.new(:code)
8
+
9
+ # @!attribute [rw] code
10
+ # Returns the event type code
11
+ # @api public
12
+ # @example
13
+ # schedule_event_type.code #=> "P"
14
+ # @return [String] the event type code
15
+ attribute :code, Shale::Type::String
16
+
17
+ # @!attribute [rw] name
18
+ # Returns the event type name
19
+ # @api public
20
+ # @example
21
+ # schedule_event_type.name #=> "Postseason Game"
22
+ # @return [String] the event type name
23
+ attribute :name, Shale::Type::String
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "schedule_event_type"
3
+
4
+ module MLB
5
+ # Provides methods for fetching schedule event types from the API
6
+ #
7
+ # @example Fetch all schedule event types
8
+ # MLB::ScheduleEventTypes.all #=> [#<MLB::ScheduleEventType>, ...]
9
+ class ScheduleEventTypes < Collection
10
+ collection endpoint: "scheduleEventTypes", item_type: ScheduleEventType, collection_name: :schedule_event_types
11
+ end
12
+ end
@@ -0,0 +1,321 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "game_status"
4
+ require_relative "team"
5
+ require_relative "venue"
6
+
7
+ module MLB
8
+ # Represents a team's information in a scheduled game
9
+ class ScheduledGameTeam < Shale::Mapper
10
+ include Equalizer.new(:team)
11
+
12
+ # @!attribute [rw] team
13
+ # Returns the team
14
+ # @api public
15
+ # @example
16
+ # scheduled_game_team.team #=> #<MLB::Team>
17
+ # @return [Team] the team
18
+ attribute :team, Team
19
+
20
+ # @!attribute [rw] score
21
+ # Returns the team's score
22
+ # @api public
23
+ # @example
24
+ # scheduled_game_team.score #=> 5
25
+ # @return [Integer] the team's score
26
+ attribute :score, Shale::Type::Integer
27
+
28
+ # @!attribute [rw] is_winner
29
+ # Returns whether this team won the game
30
+ # @api public
31
+ # @example
32
+ # scheduled_game_team.is_winner #=> true
33
+ # @return [Boolean] whether this team won the game
34
+ attribute :is_winner, Shale::Type::Boolean
35
+
36
+ # @!method winner?
37
+ # Returns whether this team won the game
38
+ # @api public
39
+ # @example
40
+ # scheduled_game_team.winner? #=> true
41
+ # @return [Boolean, nil] whether this team won the game
42
+ alias_method :winner?, :is_winner
43
+
44
+ json do
45
+ map "team", to: :team
46
+ map "score", to: :score
47
+ map "isWinner", to: :is_winner
48
+ end
49
+ end
50
+
51
+ # Represents the teams playing in a scheduled game
52
+ class ScheduledGameTeams < Shale::Mapper
53
+ # @!attribute [rw] away
54
+ # Returns the away team information
55
+ # @api public
56
+ # @example
57
+ # teams.away #=> #<MLB::ScheduledGameTeam>
58
+ # @return [ScheduledGameTeam] the away team information
59
+ attribute :away, ScheduledGameTeam
60
+
61
+ # @!attribute [rw] home
62
+ # Returns the home team information
63
+ # @api public
64
+ # @example
65
+ # teams.home #=> #<MLB::ScheduledGameTeam>
66
+ # @return [ScheduledGameTeam] the home team information
67
+ attribute :home, ScheduledGameTeam
68
+ end
69
+
70
+ # Represents a scheduled game from the MLB Stats API
71
+ class ScheduledGame < Shale::Mapper
72
+ include Equalizer.new(:game_pk)
73
+
74
+ # @!attribute [rw] game_pk
75
+ # Returns the unique game identifier
76
+ # @api public
77
+ # @example
78
+ # scheduled_game.game_pk #=> 744834
79
+ # @return [Integer] the unique game identifier
80
+ attribute :game_pk, Shale::Type::Integer
81
+
82
+ # @!attribute [rw] link
83
+ # Returns the API link for the game feed
84
+ # @api public
85
+ # @example
86
+ # scheduled_game.link #=> "/api/v1.1/game/744834/feed/live"
87
+ # @return [String] the API link for the game feed
88
+ attribute :link, Shale::Type::String
89
+
90
+ # @!attribute [rw] game_type
91
+ # Returns the game type code
92
+ # @api public
93
+ # @example
94
+ # scheduled_game.game_type #=> "R"
95
+ # @return [String] the game type code
96
+ attribute :game_type, Shale::Type::String
97
+
98
+ # @!attribute [rw] season
99
+ # Returns the season year
100
+ # @api public
101
+ # @example
102
+ # scheduled_game.season #=> "2024"
103
+ # @return [String] the season year
104
+ attribute :season, Shale::Type::String
105
+
106
+ # @!attribute [rw] game_date
107
+ # Returns the game date and time
108
+ # @api public
109
+ # @example
110
+ # scheduled_game.game_date #=> "2024-07-04T15:05:00Z"
111
+ # @return [String] the game date and time
112
+ attribute :game_date, Shale::Type::String
113
+
114
+ # @!attribute [rw] official_date
115
+ # Returns the official game date
116
+ # @api public
117
+ # @example
118
+ # scheduled_game.official_date #=> #<Date: 2024-07-04>
119
+ # @return [Date] the official game date
120
+ attribute :official_date, Shale::Type::Date
121
+
122
+ # @!attribute [rw] status
123
+ # Returns the game status
124
+ # @api public
125
+ # @example
126
+ # scheduled_game.status #=> #<MLB::GameStatus>
127
+ # @return [GameStatus] the game status
128
+ attribute :status, GameStatus
129
+
130
+ # @!attribute [rw] teams
131
+ # Returns the teams playing
132
+ # @api public
133
+ # @example
134
+ # scheduled_game.teams #=> #<MLB::ScheduledGameTeams>
135
+ # @return [ScheduledGameTeams] the teams playing
136
+ attribute :teams, ScheduledGameTeams
137
+
138
+ # @!attribute [rw] venue
139
+ # Returns the game venue
140
+ # @api public
141
+ # @example
142
+ # scheduled_game.venue #=> #<MLB::Venue>
143
+ # @return [Venue] the game venue
144
+ attribute :venue, Venue
145
+
146
+ # @!attribute [rw] is_tie
147
+ # Returns whether the game ended in a tie
148
+ # @api public
149
+ # @example
150
+ # scheduled_game.is_tie #=> false
151
+ # @return [Boolean] whether the game ended in a tie
152
+ attribute :is_tie, Shale::Type::Boolean
153
+
154
+ # @!attribute [rw] game_number
155
+ # Returns the game number (for doubleheaders)
156
+ # @api public
157
+ # @example
158
+ # scheduled_game.game_number #=> 1
159
+ # @return [Integer] the game number
160
+ attribute :game_number, Shale::Type::Integer
161
+
162
+ # @!attribute [rw] double_header
163
+ # Returns the doubleheader indicator
164
+ # @api public
165
+ # @example
166
+ # scheduled_game.double_header #=> "N"
167
+ # @return [String] the doubleheader indicator
168
+ attribute :double_header, Shale::Type::String
169
+
170
+ # @!attribute [rw] day_night
171
+ # Returns whether the game is a day or night game
172
+ # @api public
173
+ # @example
174
+ # scheduled_game.day_night #=> "day"
175
+ # @return [String] the day/night indicator
176
+ attribute :day_night, Shale::Type::String
177
+
178
+ # @!attribute [rw] scheduled_innings
179
+ # Returns the number of scheduled innings
180
+ # @api public
181
+ # @example
182
+ # scheduled_game.scheduled_innings #=> 9
183
+ # @return [Integer] the number of scheduled innings
184
+ attribute :scheduled_innings, Shale::Type::Integer
185
+
186
+ # @!attribute [rw] series_description
187
+ # Returns the series description
188
+ # @api public
189
+ # @example
190
+ # scheduled_game.series_description #=> "Regular Season"
191
+ # @return [String] the series description
192
+ attribute :series_description, Shale::Type::String
193
+
194
+ # @!attribute [rw] games_in_series
195
+ # Returns the number of games in the series
196
+ # @api public
197
+ # @example
198
+ # scheduled_game.games_in_series #=> 4
199
+ # @return [Integer] the number of games in the series
200
+ attribute :games_in_series, Shale::Type::Integer
201
+
202
+ # @!attribute [rw] series_game_number
203
+ # Returns the game number within the series
204
+ # @api public
205
+ # @example
206
+ # scheduled_game.series_game_number #=> 1
207
+ # @return [Integer] the game number within the series
208
+ attribute :series_game_number, Shale::Type::Integer
209
+
210
+ # @!method tie?
211
+ # Returns whether the game ended in a tie
212
+ # @api public
213
+ # @example
214
+ # scheduled_game.tie? #=> false
215
+ # @return [Boolean, nil] whether the game ended in a tie
216
+ alias_method :tie?, :is_tie
217
+
218
+ # Day game indicator value
219
+ DAY = "day".freeze
220
+ # Night game indicator value
221
+ NIGHT = "night".freeze
222
+ # Doubleheader indicator values that indicate a doubleheader
223
+ DOUBLE_HEADER_VALUES = %w[Y S].freeze
224
+ # Game type: Regular Season
225
+ GAME_TYPE_REGULAR = "R".freeze
226
+ # Game type: Spring Training
227
+ GAME_TYPE_SPRING = "S".freeze
228
+ # Game type: Exhibition
229
+ GAME_TYPE_EXHIBITION = "E".freeze
230
+ # Game type: All-Star Game
231
+ GAME_TYPE_ALL_STAR = "A".freeze
232
+ # Game type codes that indicate postseason games
233
+ POSTSEASON_GAME_TYPES = %w[F D L W].freeze
234
+
235
+ # Returns whether this is a day game
236
+ #
237
+ # @api public
238
+ # @example
239
+ # scheduled_game.day? #=> true
240
+ # @return [Boolean] true if the game is a day game
241
+ def day? = day_night.eql?(DAY)
242
+
243
+ # Returns whether this is a night game
244
+ #
245
+ # @api public
246
+ # @example
247
+ # scheduled_game.night? #=> false
248
+ # @return [Boolean] true if the game is a night game
249
+ def night? = day_night.eql?(NIGHT)
250
+
251
+ # Returns whether this game is part of a doubleheader
252
+ #
253
+ # @api public
254
+ # @example
255
+ # scheduled_game.double_header? #=> false
256
+ # @return [Boolean] true if the game is part of a doubleheader
257
+ # mutant:disable - .to_s is for type safety; include?(nil) returns false anyway
258
+ def double_header? = DOUBLE_HEADER_VALUES.include?(double_header.to_s)
259
+
260
+ # Returns whether this is a regular season game
261
+ #
262
+ # @api public
263
+ # @example
264
+ # scheduled_game.regular_season? #=> true
265
+ # @return [Boolean] true if the game is a regular season game
266
+ def regular_season? = game_type.eql?(GAME_TYPE_REGULAR)
267
+
268
+ # Returns whether this is a spring training game
269
+ #
270
+ # @api public
271
+ # @example
272
+ # scheduled_game.spring_training? #=> false
273
+ # @return [Boolean] true if the game is a spring training game
274
+ def spring_training? = game_type.eql?(GAME_TYPE_SPRING)
275
+
276
+ # Returns whether this is an exhibition game
277
+ #
278
+ # @api public
279
+ # @example
280
+ # scheduled_game.exhibition? #=> false
281
+ # @return [Boolean] true if the game is an exhibition game
282
+ def exhibition? = game_type.eql?(GAME_TYPE_EXHIBITION)
283
+
284
+ # Returns whether this is an All-Star game
285
+ #
286
+ # @api public
287
+ # @example
288
+ # scheduled_game.all_star? #=> false
289
+ # @return [Boolean] true if the game is an All-Star game
290
+ def all_star? = game_type.eql?(GAME_TYPE_ALL_STAR)
291
+
292
+ # Returns whether this is a postseason game
293
+ #
294
+ # @api public
295
+ # @example
296
+ # scheduled_game.postseason? #=> false
297
+ # @return [Boolean] true if the game is a postseason game
298
+ # mutant:disable - .to_s is for type safety; include?(nil) returns false anyway
299
+ def postseason? = POSTSEASON_GAME_TYPES.include?(game_type.to_s)
300
+
301
+ json do
302
+ map "gamePk", to: :game_pk
303
+ map "link", to: :link
304
+ map "gameType", to: :game_type
305
+ map "season", to: :season
306
+ map "gameDate", to: :game_date
307
+ map "officialDate", to: :official_date
308
+ map "status", to: :status
309
+ map "teams", to: :teams
310
+ map "venue", to: :venue
311
+ map "isTie", to: :is_tie
312
+ map "gameNumber", to: :game_number
313
+ map "doubleHeader", to: :double_header
314
+ map "dayNight", to: :day_night
315
+ map "scheduledInnings", to: :scheduled_innings
316
+ map "seriesDescription", to: :series_description
317
+ map "gamesInSeries", to: :games_in_series
318
+ map "seriesGameNumber", to: :series_game_number
319
+ end
320
+ end
321
+ end
data/lib/mlb/season.rb ADDED
@@ -0,0 +1,74 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "comparable_by_attribute"
4
+ require_relative "league"
5
+ require_relative "sport"
6
+
7
+ module MLB
8
+ # Represents a season
9
+ class Season < Shale::Mapper
10
+ include Comparable
11
+ include ComparableByAttribute
12
+ include Equalizer.new(:id)
13
+
14
+ # Returns the attribute used for sorting
15
+ #
16
+ # @api private
17
+ # @return [Symbol] the attribute used for comparison
18
+ def comparable_attribute = :id
19
+
20
+ attribute :id, Shale::Type::Integer
21
+ attribute :has_wildcard, Shale::Type::Boolean
22
+ attribute :preseason_start_date, Shale::Type::Date
23
+ attribute :preseason_end_date, Shale::Type::Date
24
+ attribute :season_start_date, Shale::Type::Date
25
+ attribute :spring_start_date, Shale::Type::Date
26
+ attribute :spring_end_date, Shale::Type::Date
27
+ attribute :regular_season_start_date, Shale::Type::Date
28
+ attribute :last_date_first_half, Shale::Type::Date
29
+ attribute :all_star_date, Shale::Type::Date
30
+ attribute :first_date_second_half, Shale::Type::Date
31
+ attribute :regular_season_end_date, Shale::Type::Date
32
+ attribute :postseason_start_date, Shale::Type::Date
33
+ attribute :postseason_end_date, Shale::Type::Date
34
+ attribute :season_end_date, Shale::Type::Date
35
+ attribute :offseason_start_date, Shale::Type::Date
36
+ attribute :offseason_end_date, Shale::Type::Date
37
+ attribute :season_level_gameday_type, Shale::Type::String
38
+ attribute :game_level_gameday_type, Shale::Type::String
39
+ attribute :qualifier_plate_appearances, Shale::Type::Float
40
+ attribute :qualifier_outs_pitched, Shale::Type::Float
41
+
42
+ # Returns whether the season has a wild card
43
+ #
44
+ # @api public
45
+ # @example
46
+ # season.wildcard?
47
+ # @return [Boolean, nil] true if the season has a wild card
48
+ alias_method :wildcard?, :has_wildcard
49
+
50
+ json do
51
+ map "seasonId", to: :id
52
+ map "hasWildcard", to: :has_wildcard
53
+ map "preSeasonStartDate", to: :preseason_start_date
54
+ map "preSeasonEndDate", to: :preseason_end_date
55
+ map "seasonStartDate", to: :season_start_date
56
+ map "springStartDate", to: :spring_start_date
57
+ map "springEndDate", to: :spring_end_date
58
+ map "regularSeasonStartDate", to: :regular_season_start_date
59
+ map "lastDate1stHalf", to: :last_date_first_half
60
+ map "allStarDate", to: :all_star_date
61
+ map "firstDate2ndHalf", to: :first_date_second_half
62
+ map "regularSeasonEndDate", to: :regular_season_end_date
63
+ map "postSeasonStartDate", to: :postseason_start_date
64
+ map "postSeasonEndDate", to: :postseason_end_date
65
+ map "seasonEndDate", to: :season_end_date
66
+ map "offseasonStartDate", to: :offseason_start_date
67
+ map "offSeasonEndDate", to: :offseason_end_date
68
+ map "seasonLevelGamedayType", to: :season_level_gameday_type
69
+ map "gameLevelGamedayType", to: :game_level_gameday_type
70
+ map "qualifierPlateAppearances", to: :qualifier_plate_appearances
71
+ map "qualifierOutsPitched", to: :qualifier_outs_pitched
72
+ end
73
+ end
74
+ end
@@ -1,7 +1,11 @@
1
+ require "equalizer"
1
2
  require "shale"
2
3
 
3
4
  module MLB
5
+ # Represents important dates for an MLB season
4
6
  class SeasonDateInfo < Shale::Mapper
7
+ include Equalizer.new(:season_id)
8
+
5
9
  attribute :season_id, Shale::Type::String
6
10
  attribute :pre_season_start_date, Shale::Type::Date
7
11
  attribute :pre_season_end_date, Shale::Type::Date
@@ -0,0 +1,36 @@
1
+ require "shale"
2
+ require_relative "season"
3
+
4
+ module MLB
5
+ # Collection of seasons from the MLB Stats API
6
+ class Seasons < Shale::Mapper
7
+ attribute :seasons, Season, collection: true
8
+
9
+ # Retrieves all seasons
10
+ #
11
+ # @api public
12
+ # @example
13
+ # MLB::Seasons.all
14
+ # @param sport [Integer, Sport] the sport ID or Sport object
15
+ # @return [Array<Season>] list of all seasons
16
+ def self.all(sport: Utils::DEFAULT_SPORT_ID)
17
+ params = {sportId: Utils.extract_id(sport)}
18
+ response = CLIENT.get("seasons?#{Utils.build_query(params)}")
19
+ from_json(response).seasons.sort
20
+ end
21
+
22
+ # Finds a season by ID
23
+ #
24
+ # @api public
25
+ # @example
26
+ # MLB::Seasons.find(2024)
27
+ # @param season [Integer, Season] the season ID or Season object
28
+ # @param sport [Integer, Sport] the sport ID or Sport object
29
+ # @return [Season, nil] the season if found
30
+ def self.find(season, sport: Utils::DEFAULT_SPORT_ID)
31
+ params = {sportId: Utils.extract_id(sport)}
32
+ response = CLIENT.get("seasons/#{Utils.extract_id(season)}?#{Utils.build_query(params)}")
33
+ from_json(response).seasons.min_by { |s| s.id || 0 }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ require "shale"
2
+ require_relative "team_stats"
3
+
4
+ module MLB
5
+ # Provides methods for fetching individual team stats from the API
6
+ class SingleTeamStats < Shale::Mapper
7
+ attribute :stats, TeamStatGroup, collection: true
8
+
9
+ json do
10
+ map "stats", to: :stats
11
+ end
12
+
13
+ # Retrieves stats for a specific team
14
+ #
15
+ # @api public
16
+ # @example Get team hitting stats
17
+ # MLB::SingleTeamStats.find(team: 147, season: 2024, group: "hitting")
18
+ # @example Get team pitching stats
19
+ # MLB::SingleTeamStats.find(team: Team.new(id: 147), season: 2024, group: "pitching")
20
+ # @param team [Integer, Team] the team ID or Team object
21
+ # @param season [Integer, nil] the season year (defaults to current year)
22
+ # @param group [String] the stat group (hitting, pitching, fielding)
23
+ # @param stats [String] the stats type (default: season)
24
+ # @return [Array<TeamStat>] the team stats
25
+ def self.find(team:, season: nil, group: "hitting", stats: "season")
26
+ season ||= Utils.current_season
27
+ team_id = Utils.extract_id(team)
28
+ params = {season:, group:, stats:}
29
+ response = CLIENT.get("teams/#{team_id}/stats?#{Utils.build_query(params)}")
30
+ from_json(response).stats.first&.splits || []
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,116 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ # Represents a situation code for statistical filtering
6
+ class SituationCode < Shale::Mapper
7
+ include Equalizer.new(:code)
8
+
9
+ # @!attribute [rw] code
10
+ # Returns the situation code
11
+ # @api public
12
+ # @example
13
+ # situation_code.code #=> "h"
14
+ # @return [String] the situation code
15
+ attribute :code, Shale::Type::String
16
+
17
+ # @!attribute [rw] sort_order
18
+ # Returns the sort order
19
+ # @api public
20
+ # @example
21
+ # situation_code.sort_order #=> 1
22
+ # @return [Integer] the sort order
23
+ attribute :sort_order, Shale::Type::Integer
24
+
25
+ # @!attribute [rw] navigation_menu
26
+ # Returns the navigation menu category
27
+ # @api public
28
+ # @example
29
+ # situation_code.navigation_menu #=> "Game"
30
+ # @return [String] the navigation menu category
31
+ attribute :navigation_menu, Shale::Type::String
32
+
33
+ # @!attribute [rw] description
34
+ # Returns the description
35
+ # @api public
36
+ # @example
37
+ # situation_code.description #=> "Home Games"
38
+ # @return [String] the description
39
+ attribute :description, Shale::Type::String
40
+
41
+ # @!attribute [rw] team
42
+ # Returns whether this applies to team statistics
43
+ # @api public
44
+ # @example
45
+ # situation_code.team #=> true
46
+ # @return [Boolean] whether this applies to team statistics
47
+ attribute :team, Shale::Type::Boolean
48
+
49
+ # @!attribute [rw] batting
50
+ # Returns whether this applies to batting statistics
51
+ # @api public
52
+ # @example
53
+ # situation_code.batting #=> true
54
+ # @return [Boolean] whether this applies to batting statistics
55
+ attribute :batting, Shale::Type::Boolean
56
+
57
+ # @!attribute [rw] fielding
58
+ # Returns whether this applies to fielding statistics
59
+ # @api public
60
+ # @example
61
+ # situation_code.fielding #=> true
62
+ # @return [Boolean] whether this applies to fielding statistics
63
+ attribute :fielding, Shale::Type::Boolean
64
+
65
+ # @!attribute [rw] pitching
66
+ # Returns whether this applies to pitching statistics
67
+ # @api public
68
+ # @example
69
+ # situation_code.pitching #=> true
70
+ # @return [Boolean] whether this applies to pitching statistics
71
+ attribute :pitching, Shale::Type::Boolean
72
+
73
+ # Returns whether this applies to team statistics
74
+ #
75
+ # @api public
76
+ # @example
77
+ # situation_code.team? #=> true
78
+ # @return [Boolean] whether this applies to team statistics
79
+ def team? = team
80
+
81
+ # Returns whether this applies to batting statistics
82
+ #
83
+ # @api public
84
+ # @example
85
+ # situation_code.batting? #=> true
86
+ # @return [Boolean] whether this applies to batting statistics
87
+ def batting? = batting
88
+
89
+ # Returns whether this applies to fielding statistics
90
+ #
91
+ # @api public
92
+ # @example
93
+ # situation_code.fielding? #=> true
94
+ # @return [Boolean] whether this applies to fielding statistics
95
+ def fielding? = fielding
96
+
97
+ # Returns whether this applies to pitching statistics
98
+ #
99
+ # @api public
100
+ # @example
101
+ # situation_code.pitching? #=> true
102
+ # @return [Boolean] whether this applies to pitching statistics
103
+ def pitching? = pitching
104
+
105
+ json do
106
+ map "code", to: :code
107
+ map "sortOrder", to: :sort_order
108
+ map "navigationMenu", to: :navigation_menu
109
+ map "description", to: :description
110
+ map "team", to: :team
111
+ map "batting", to: :batting
112
+ map "fielding", to: :fielding
113
+ map "pitching", to: :pitching
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "situation_code"
3
+
4
+ module MLB
5
+ # Provides methods for fetching situation codes from the API
6
+ #
7
+ # @example Fetch all situation codes
8
+ # MLB::SituationCodes.all #=> [#<MLB::SituationCode>, ...]
9
+ class SituationCodes < Collection
10
+ collection endpoint: "situationCodes", item_type: SituationCode, collection_name: :situation_codes
11
+ end
12
+ end
data/lib/mlb/skies.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative "collection"
2
+ require_relative "sky"
3
+
4
+ module MLB
5
+ # Provides methods for fetching sky conditions from the API
6
+ #
7
+ # @example Fetch all sky conditions
8
+ # MLB::Skies.all #=> [#<MLB::Sky>, ...]
9
+ class Skies < Collection
10
+ collection endpoint: "sky", item_type: Sky, collection_name: :skies
11
+ end
12
+ end