mlb 0.8.0 → 0.10.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e82207f5345873c47ef01b18d32ed5dc131a7a2a2531b5c5adf2ca87e2c5d397
4
- data.tar.gz: 597c40742c137216cc3210362b9a157dc28ef25cd5dacff689489821323cb804
3
+ metadata.gz: 7cffb04037de85352415288a1c8756fb223d79181b7051322fa671e60a264580
4
+ data.tar.gz: 141e7eb182d2130fb4906aa9a6d93fdd640562f432004b29ecca505a8c96dd5d
5
5
  SHA512:
6
- metadata.gz: c9e096ebd1e2a1d88d618507c8e3d15b540f06c146b29c3dff1abe2dbf581521858fbf1b6362ad251c241db89330f3d2c4d6fb94753d8236056b5ec2910bfb69
7
- data.tar.gz: e962f38e4fc333cafb5618ab1c5bdf66e53aed74b12a2e8be708877a2a8100af96410bfd8e196ce51ad4570490da98c4ade2eaefee845c9cf9230dacb2a7f5a2
6
+ metadata.gz: 98a59eca40923bdeaecb637dbf863282fe9958c554afe15a3e75964745129aec710bcb24c8e2eeacc709190f37ac3a97bc76babc9531432e589985222ab3bfaf
7
+ data.tar.gz: ba7675aab23a6167c94adcb9b554b9f5b1645182d452152a690a1904af9a2af58fb9aec19624e671b3fc14cc23a4ae4ad57e2f366fa4b795905b491733e09775
data/CHANGELOG.md CHANGED
@@ -1,2 +1,11 @@
1
- ## [0.8.0] - 2024-05-02
1
+ ## [0.10.0] - 2025-04-30
2
+ * Drop support for Ruby 3.1
3
+ * Add equalizer for object identity
4
+ * Add Seasons, Conferences, and Awards
5
+ * Add Players.find and Players.find_all
6
+
7
+ ## [0.9.0] - 2024-05-22
8
+ * Rewrite to use MLB Stats API
9
+
10
+ ## [0.8.0] - 2024-05-06
2
11
  * Rewrite
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020-2024 Erik Berlin, Nilesh Trivedi
3
+ Copyright (c) 2020-2025 Erik Berlin, Nilesh Trivedi
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  [![Tests](https://github.com/sferik/mlb-ruby/actions/workflows/test.yml/badge.svg)](https://github.com/sferik/mlb-ruby/actions/workflows/test.yml)
2
- [![Linter](https://github.com/sferik/mlb-ruby/actions/workflows/lint.yml/badge.svg)](https://github.com/sferik/mlb-ruby/actions/workflows/lint.yml)
2
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/fc32f80cb778b773a5b2/test_coverage)](https://codeclimate.com/github/sferik/mlb-ruby/test_coverage)
3
3
  [![Mutant](https://github.com/sferik/mlb-ruby/actions/workflows/mutant.yml/badge.svg)](https://github.com/sferik/mlb-ruby/actions/workflows/mutant.yml)
4
- [![Typer Checker](https://github.com/sferik/mlb-ruby/actions/workflows/steep.yml/badge.svg)](https://github.com/sferik/mlb-ruby/actions/workflows/steep.yml)
4
+ [![Linter](https://github.com/sferik/mlb-ruby/actions/workflows/lint.yml/badge.svg)](https://github.com/sferik/mlb-ruby/actions/workflows/lint.yml)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/fc32f80cb778b773a5b2/maintainability)](https://codeclimate.com/github/sferik/mlb-ruby/maintainability)
5
6
  [![Gem Version](https://badge.fury.io/rb/mlb.svg)](https://rubygems.org/gems/mlb)
6
7
 
7
- # A [Ruby](https://www.ruby-lang.org) interface to the [MLB Data API](https://appac.github.io/mlb-data-api-docs/)
8
+ # A [Ruby](https://www.ruby-lang.org) interface to the [MLB Stats API](https://statsapi.mlb.com)
8
9
 
9
10
  ## Follow
10
11
 
11
- For updates and announcements, follow [@sferik](https://x.com/sferik) on X.
12
+ For updates and announcements, follow [@sferik](https://x.com/sferik).
12
13
 
13
14
  ## Installation
14
15
 
@@ -25,33 +26,34 @@ Or, if Bundler is not being used to manage dependencies:
25
26
  ```ruby
26
27
  require "mlb"
27
28
 
28
- dbacks = MLB::Team.all(season: 2024).first
29
- dbacks.name_display_full # => "Arizona Diamondbacks"
30
- dbacks.name_display_brief # => "D-backs"
31
- dbacks.name_abbrev # => "AZ"
32
- dbacks.city # => "Phoenix"
33
- dbacks.league_full # => "National League"
34
- dbacks.league_abbrev # => "NL"
35
- dbacks.spring_league_full # => "Cactus League"
36
- dbacks.spring_league_abbrev # => "CL"
37
- dbacks.division_full # => "National League West"
38
- dbacks.division_abbrev # => "NLW"
39
- dbacks.first_year_of_play # => "1996"
40
- dbacks.venue_name # => "Chase Field"
41
-
42
- blaze = dbacks.roster.first
43
- blaze.name_display_first_last # => "Blaze Alexander"
44
- blaze.jersey_number # => "9"
45
- blaze.primary_position # => "6"
46
- blaze.position_txt # => "SS"
47
- blaze.height_feet # => "5"
48
- blaze.height_inches # => "11"
49
- blaze.weight # => "160"
50
- blaze.bats # => "R"
51
- blaze.throws # => "R"
52
- blaze.birth_date # => "1999-06-11T00:00:00"
53
- blaze.start_date # => "2022-11-15T00:00:00"
54
- blaze.pro_debut_date # => "2024-03-28T00:00:00"
29
+ mets = MLB::Teams.all(season: 2024).last
30
+ mets.name # => "New York Mets"
31
+ mets.short_name # => "NY Mets"
32
+ mets.abbreviation # => "NYM"
33
+ mets.league.name # => "National League"
34
+ mets.spring_league.name # => "Grapefruit League"
35
+ mets.division.name # => "National League East"
36
+ mets.first_year_of_play # => 1962
37
+ mets.location_name # => "Flushing"
38
+ mets.venue.name # => "Citi Field"
39
+
40
+ adam = MLB::Players.find(mets.roster.first.player)
41
+
42
+ adam.full_name # => "Adam Ottavino"
43
+ adam.primary_number # => 0
44
+ adam.primary_position.name # => "Pitcher"
45
+ adam.pitch_hand.description # => "Right"
46
+ adam.bat_side.description # => "Switch"
47
+ adam.current_age # => 38
48
+ adam.birth_date.to_s # => "1985-11-22"
49
+ adam.birth_city # => "New York"
50
+ adam.birth_state_province # => "NY"
51
+ adam.birth_country # => "USA"
52
+ adam.draft_year # => 2006
53
+ adam.mlb_debut_date.to_s # => "2010-05-29"
54
+ adam.height # => "6' 5\""
55
+ adam.weight # => 246
56
+ adam.active? # => true
55
57
  ```
56
58
 
57
59
  ## Sponsorship
@@ -68,6 +70,82 @@ Building and maintaining an open-source project like this takes a considerable a
68
70
 
69
71
  [Click here to sponsor this project.](https://github.com/sponsors/sferik)
70
72
 
73
+ ## TODO
74
+
75
+ Many of the MLB Stats API endpoints have not yet been implemented in this library. You can contribute to this library by implementing one or more of these endpoinds. If you are financially unable to sponsor this project, you can contribute code instead.
76
+
77
+ - [ ] /attendance
78
+ - [ ] /baseballStats
79
+ - [ ] /draft{prospects}{year}{latest}
80
+ - [ ] /eventTypes
81
+ - [ ] /game/changes
82
+ - [ ] /game/{gamePk}/boxscore
83
+ - [ ] /game/{gamePk}/content
84
+ - [ ] /game/{gamePk}/contextMetrics
85
+ - [ ] /game/{gamePk}/feed/color
86
+ - [ ] /game/{gamePk}/feed/color/diffPatch
87
+ - [ ] /game/{gamePk}/feed/color/timestamps
88
+ - [ ] /game/{gamePk}/feed/live
89
+ - [ ] /game/{gamePk}/feed/live/diffPatch
90
+ - [ ] /game/{gamePk}/feed/live/timestamps
91
+ - [ ] /game/{gamePk}/linescore
92
+ - [ ] /game/{gamePk}/playByPlay
93
+ - [ ] /game/{gamePk}/winProbability
94
+ - [ ] /gamePace
95
+ - [ ] /gameStatus
96
+ - [ ] /gameTypes
97
+ - [ ] /highLow/{orgType}
98
+ - [ ] /hitTrajectories
99
+ - [ ] /homeRunDerby/{gamePk}{bracket}{pool}
100
+ - [ ] /jobTypes
101
+ - [ ] /jobs
102
+ - [ ] /jobs/datacasters
103
+ - [ ] /jobs/officialScorers
104
+ - [ ] /jobs/umpires
105
+ - [ ] /jobs/umpires/games/{umpireId}
106
+ - [ ] /languages
107
+ - [ ] /league/{leagueId}/allStarBallot
108
+ - [ ] /league/{leagueId}/allStarFinalVote
109
+ - [ ] /league/{leagueId}/allStarWriteIns
110
+ - [ ] /leagueLeaderTypes
111
+ - [ ] /logicalEvents
112
+ - [ ] /metrics
113
+ - [ ] /people/changes
114
+ - [ ] /people/freeAgents
115
+ - [ ] /people/{personId}
116
+ - [ ] /people/{personId}/stats/game/{gamePk}
117
+ - [ ] /pitchCodes
118
+ - [ ] /pitchTypes
119
+ - [ ] /platforms
120
+ - [ ] /positions
121
+ - [ ] /reviewReasons
122
+ - [ ] /rosterTypes
123
+ - [ ] /schedule
124
+ - [ ] /schedule/games/tied
125
+ - [ ] /schedule/postseason
126
+ - [ ] /schedule/postseason/series
127
+ - [ ] /schedule/postseason/tuneIn
128
+ - [ ] /scheduleEventTypes
129
+ - [ ] /situationCodes
130
+ - [ ] /sky
131
+ - [ ] /standings
132
+ - [ ] /standingsTypes
133
+ - [ ] /statGroups
134
+ - [ ] /statTypes
135
+ - [ ] /stats
136
+ - [ ] /stats/leaders
137
+ - [ ] /stats/streaks
138
+ - [ ] /teams/affiliates
139
+ - [ ] /teams/history
140
+ - [ ] /teams/stats
141
+ - [ ] /teams/{teamId}/alumni
142
+ - [ ] /teams/{teamId}/coaches
143
+ - [ ] /teams/{teamId}/leaders
144
+ - [ ] /teams/{teamId}/personnel
145
+ - [ ] /teams/{teamId}/roster
146
+ - [ ] /teams/{teamId}/stats
147
+ - [ ] /windDirection
148
+
71
149
  ## Development
72
150
 
73
151
  1. Checkout and repo:
@@ -112,10 +190,6 @@ Pull requests will only be accepted if they meet all the following criteria:
112
190
 
113
191
  bundle exec rake mutant
114
192
 
115
- 5. RBS type signatures (in `sig/mlb.rbs`). This can be verified with:
116
-
117
- bundle exec rake steep
118
-
119
193
  ## License
120
194
 
121
195
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/mlb/award.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require "uri"
4
+ require_relative "league"
5
+ require_relative "sport"
6
+ require_relative "player"
7
+
8
+ module MLB
9
+ class Award < Shale::Mapper
10
+ include Comparable
11
+ include Equalizer.new(:id, :player, :season)
12
+
13
+ attribute :id, Shale::Type::String
14
+ attribute :name, Shale::Type::String
15
+ attribute :notes, Shale::Type::String
16
+ attribute :votes, Shale::Type::Integer
17
+ attribute :date, Shale::Type::Date
18
+ attribute :season, Shale::Type::Integer
19
+ attribute :sport, Sport
20
+ attribute :player, Player
21
+ attribute :league, League
22
+ attribute :sort_order, Shale::Type::Integer
23
+
24
+ json do
25
+ map "id", to: :id
26
+ map "name", to: :name
27
+ map "sport", to: :sport
28
+ map "league", to: :league
29
+ map "player", to: :player
30
+ map "sortOrder", to: :sort_order
31
+ map "season", to: :season
32
+ map "notes", to: :notes
33
+ map "votes", to: :votes
34
+ end
35
+
36
+ def <=>(other)
37
+ sort_order <=> other.sort_order
38
+ end
39
+
40
+ def recipients(season: Time.now.year)
41
+ params = {season:}
42
+ query_string = URI.encode_www_form(params)
43
+ response = CLIENT.get("awards/#{id}/recipients?#{query_string}")
44
+ awards = Awards.from_json(response)
45
+ awards.awards
46
+ end
47
+ end
48
+ end
data/lib/mlb/awards.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "shale"
2
+ require_relative "award"
3
+
4
+ module MLB
5
+ class Awards < Shale::Mapper
6
+ attribute :copyright, Shale::Type::String
7
+ attribute :awards, Award, collection: true
8
+
9
+ def self.all
10
+ response = CLIENT.get("awards")
11
+ awards = from_json(response)
12
+ awards.awards.sort!
13
+ end
14
+
15
+ def self.find(award)
16
+ id = award.respond_to?(:id) ? award.id : award
17
+ response = CLIENT.get("awards")
18
+ awards = from_json(response)
19
+ awards.awards.find { |a| a.id.eql?(id) }
20
+ end
21
+ end
22
+ end
data/lib/mlb/client.rb CHANGED
@@ -2,17 +2,15 @@ require "forwardable"
2
2
  require_relative "connection"
3
3
  require_relative "redirect_handler"
4
4
  require_relative "request_builder"
5
- require_relative "response_parser"
5
+ require_relative "error_handler"
6
6
 
7
7
  module MLB
8
8
  class Client
9
9
  extend Forwardable
10
10
 
11
- DEFAULT_BASE_URL = "https://lookup-service-prod.mlb.com/json/".freeze
12
- DEFAULT_ARRAY_CLASS = Array
13
- DEFAULT_OBJECT_CLASS = Hash
11
+ DEFAULT_BASE_URL = "https://statsapi.mlb.com/api/v1/".freeze
14
12
 
15
- attr_accessor :base_url, :default_array_class, :default_object_class
13
+ attr_accessor :base_url
16
14
 
17
15
  def_delegators :@connection, :open_timeout, :read_timeout, :write_timeout, :proxy_url, :debug_output
18
16
  def_delegators :@connection, :open_timeout=, :read_timeout=, :write_timeout=, :proxy_url=, :debug_output=
@@ -25,47 +23,38 @@ module MLB
25
23
  write_timeout: Connection::DEFAULT_WRITE_TIMEOUT,
26
24
  debug_output: Connection::DEFAULT_DEBUG_OUTPUT,
27
25
  proxy_url: nil,
28
- default_array_class: DEFAULT_ARRAY_CLASS,
29
- default_object_class: DEFAULT_OBJECT_CLASS,
30
26
  max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS)
31
-
32
27
  @base_url = base_url
33
- initialize_default_classes(default_array_class, default_object_class)
34
28
  @connection = Connection.new(open_timeout:, read_timeout:, write_timeout:, debug_output:, proxy_url:)
35
29
  @request_builder = RequestBuilder.new
36
30
  @redirect_handler = RedirectHandler.new(connection: @connection, request_builder: @request_builder, max_redirects:)
37
- @response_parser = ResponseParser.new
31
+ @error_handler = ErrorHandler.new
38
32
  end
39
33
 
40
- def get(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class)
41
- execute_request(:get, endpoint, headers:, array_class:, object_class:)
34
+ def get(endpoint, headers: {})
35
+ execute_request(:get, endpoint, headers:)
42
36
  end
43
37
 
44
- def post(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class)
45
- execute_request(:post, endpoint, body:, headers:, array_class:, object_class:)
38
+ def post(endpoint, body = nil, headers: {})
39
+ execute_request(:post, endpoint, body:, headers:)
46
40
  end
47
41
 
48
- def put(endpoint, body = nil, headers: {}, array_class: default_array_class, object_class: default_object_class)
49
- execute_request(:put, endpoint, body:, headers:, array_class:, object_class:)
42
+ def put(endpoint, body = nil, headers: {})
43
+ execute_request(:put, endpoint, body:, headers:)
50
44
  end
51
45
 
52
- def delete(endpoint, headers: {}, array_class: default_array_class, object_class: default_object_class)
53
- execute_request(:delete, endpoint, headers:, array_class:, object_class:)
46
+ def delete(endpoint, headers: {})
47
+ execute_request(:delete, endpoint, headers:)
54
48
  end
55
49
 
56
50
  private
57
51
 
58
- def initialize_default_classes(default_array_class, default_object_class)
59
- @default_array_class = default_array_class
60
- @default_object_class = default_object_class
61
- end
62
-
63
- def execute_request(http_method, endpoint, body: nil, headers: {}, array_class: default_array_class, object_class: default_object_class)
52
+ def execute_request(http_method, endpoint, headers:, body: nil)
64
53
  uri = URI.join(base_url, endpoint)
65
54
  request = @request_builder.build(http_method:, uri:, body:, headers:)
66
55
  response = @connection.perform(request:)
67
56
  response = @redirect_handler.handle(response:, request:, base_url:)
68
- @response_parser.parse(response:, array_class:, object_class:)
57
+ @error_handler.handle(response:)
69
58
  end
70
59
  end
71
60
 
@@ -0,0 +1,32 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "league"
4
+ require_relative "sport"
5
+
6
+ module MLB
7
+ class Conference < Shale::Mapper
8
+ include Equalizer.new(:id)
9
+
10
+ attribute :id, Shale::Type::Integer
11
+ attribute :name, Shale::Type::String
12
+ attribute :link, Shale::Type::String
13
+ attribute :abbreviation, Shale::Type::String
14
+ attribute :has_wildcard, Shale::Type::Boolean
15
+ attribute :league, League
16
+ attribute :sport, Sport
17
+ attribute :name_short, Shale::Type::String
18
+
19
+ alias_method :wildcard?, :has_wildcard
20
+
21
+ json do
22
+ map "id", to: :id
23
+ map "name", to: :name
24
+ map "link", to: :link
25
+ map "abbreviation", to: :abbreviation
26
+ map "hasWildcard", to: :has_wildcard
27
+ map "league", to: :league
28
+ map "sport", to: :sport
29
+ map "nameShort", to: :name_short
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require "shale"
2
+ require "uri"
3
+ require_relative "conference"
4
+
5
+ module MLB
6
+ class Conferences < Shale::Mapper
7
+ attribute :copyright, Shale::Type::String
8
+ attribute :conferences, Conference, collection: true
9
+
10
+ def self.all
11
+ response = CLIENT.get("conferences")
12
+ conferences = from_json(response)
13
+ conferences.conferences
14
+ end
15
+
16
+ def self.find(conference)
17
+ id = conference.respond_to?(:id) ? conference.id : conference
18
+ params = {conferenceId: id}
19
+ query_string = URI.encode_www_form(params)
20
+ response = CLIENT.get("conferences?#{query_string}")
21
+ conferences = from_json(response)
22
+ conferences.conferences.first
23
+ end
24
+ end
25
+ end
@@ -8,7 +8,7 @@ module MLB
8
8
  class Connection
9
9
  extend Forwardable
10
10
 
11
- DEFAULT_HOST = "lookup-service-prod.mlb.com".freeze
11
+ DEFAULT_HOST = "statsapi.mlb.com".freeze
12
12
  DEFAULT_PORT = 443
13
13
  DEFAULT_OPEN_TIMEOUT = 60 # seconds
14
14
  DEFAULT_READ_TIMEOUT = 60 # seconds
@@ -0,0 +1,44 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "league"
4
+ require_relative "sport"
5
+
6
+ module MLB
7
+ class Division < Shale::Mapper
8
+ include Comparable
9
+ include Equalizer.new(:id)
10
+
11
+ attribute :id, Shale::Type::Integer
12
+ attribute :name, Shale::Type::String
13
+ attribute :season, Shale::Type::String
14
+ attribute :name_short, Shale::Type::String
15
+ attribute :link, Shale::Type::String
16
+ attribute :abbreviation, Shale::Type::String
17
+ attribute :league, League
18
+ attribute :sport, Sport
19
+ attribute :has_wildcard, Shale::Type::Boolean
20
+ attribute :sort_order, Shale::Type::Integer
21
+ attribute :active, Shale::Type::Boolean
22
+
23
+ alias_method :active?, :active
24
+ alias_method :wildcard?, :has_wildcard
25
+
26
+ json do
27
+ map "id", to: :id
28
+ map "name", to: :name
29
+ map "season", to: :season
30
+ map "nameShort", to: :name_short
31
+ map "link", to: :link
32
+ map "abbreviation", to: :abbreviation
33
+ map "league", to: :league
34
+ map "sport", to: :sport
35
+ map "hasWildcard", to: :has_wildcard
36
+ map "sortOrder", to: :sort_order
37
+ map "active", to: :active
38
+ end
39
+
40
+ def <=>(other)
41
+ sort_order <=> other.sort_order
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ require "shale"
2
+ require "uri"
3
+ require_relative "sport"
4
+ require_relative "division"
5
+
6
+ module MLB
7
+ class Divisions < Shale::Mapper
8
+ attribute :copyright, Shale::Type::String
9
+ attribute :divisions, Division, collection: true
10
+
11
+ def self.all(sport: Sport.new(id: 1))
12
+ sport_id = sport.respond_to?(:id) ? sport.id : sport
13
+ params = {sportId: sport_id}
14
+ query_string = URI.encode_www_form(params)
15
+ response = CLIENT.get("divisions?#{query_string}")
16
+ divisions = from_json(response)
17
+ divisions.divisions.sort!
18
+ end
19
+
20
+ def self.find(division, sport: Sport.new(id: 1))
21
+ id = division.respond_to?(:id) ? division.id : division
22
+ sport_id = sport.respond_to?(:id) ? sport.id : sport
23
+ params = {sportId: sport_id}
24
+ query_string = URI.encode_www_form(params)
25
+ response = CLIENT.get("divisions/#{id}?#{query_string}")
26
+ divisions = from_json(response)
27
+ divisions.divisions.sort!.first
28
+ end
29
+ end
30
+ end
@@ -1,4 +1,3 @@
1
- require "json"
2
1
  require "net/http"
3
2
  require_relative "errors/bad_gateway"
4
3
  require_relative "errors/bad_request"
@@ -17,7 +16,7 @@ require_relative "errors/unauthorized"
17
16
  require_relative "errors/unprocessable_entity"
18
17
 
19
18
  module MLB
20
- class ResponseParser
19
+ class ErrorHandler
21
20
  ERROR_MAP = {
22
21
  400 => BadRequest,
23
22
  401 => Unauthorized,
@@ -34,14 +33,11 @@ module MLB
34
33
  503 => ServiceUnavailable,
35
34
  504 => GatewayTimeout
36
35
  }.freeze
37
- JSON_CONTENT_TYPE_REGEXP = %r{application/json}
38
36
 
39
- def parse(response:, array_class: nil, object_class: nil)
37
+ def handle(response:)
40
38
  raise error(response) unless response.is_a?(Net::HTTPSuccess)
41
39
 
42
- return unless json?(response)
43
-
44
- JSON.parse(response.body, array_class:, object_class:, symbolize_names: true)
40
+ response.body
45
41
  end
46
42
 
47
43
  private
@@ -53,9 +49,5 @@ module MLB
53
49
  def error_class(response)
54
50
  ERROR_MAP[Integer(response.code)] || HTTPError
55
51
  end
56
-
57
- def json?(response)
58
- JSON_CONTENT_TYPE_REGEXP.match?(response["content-type"])
59
- end
60
52
  end
61
53
  end
@@ -0,0 +1,16 @@
1
+ require "equalizer"
2
+ require "shale"
3
+
4
+ module MLB
5
+ class Handedness < Shale::Mapper
6
+ include Equalizer.new(:code)
7
+
8
+ attribute :code, Shale::Type::String
9
+ attribute :description, Shale::Type::String
10
+
11
+ json do
12
+ map "code", to: :code
13
+ map "description", to: :description
14
+ end
15
+ end
16
+ end
data/lib/mlb/league.rb ADDED
@@ -0,0 +1,68 @@
1
+ require "equalizer"
2
+ require "shale"
3
+ require_relative "season_date_info"
4
+ require_relative "sport"
5
+
6
+ module MLB
7
+ class League < Shale::Mapper
8
+ include Comparable
9
+ include Equalizer.new(:id)
10
+
11
+ attribute :id, Shale::Type::Integer
12
+ attribute :name, Shale::Type::String
13
+ attribute :link, Shale::Type::String
14
+ attribute :abbreviation, Shale::Type::String
15
+ attribute :name_short, Shale::Type::String
16
+ attribute :season_state, Shale::Type::String
17
+ attribute :has_wild_card, Shale::Type::Boolean
18
+ attribute :has_split_season, Shale::Type::Boolean
19
+ attribute :num_games, Shale::Type::Integer
20
+ attribute :has_playoff_points, Shale::Type::Boolean
21
+ attribute :num_teams, Shale::Type::Integer
22
+ attribute :num_wildcard_teams, Shale::Type::Integer
23
+ attribute :season_date_info, SeasonDateInfo
24
+ attribute :season, Shale::Type::Integer
25
+ attribute :org_code, Shale::Type::String
26
+ attribute :conferences_in_use, Shale::Type::Boolean
27
+ attribute :divisions_in_use, Shale::Type::Boolean
28
+ attribute :sport, Sport
29
+ attribute :sort_order, Shale::Type::Integer
30
+ attribute :active, Shale::Type::Boolean
31
+
32
+ alias_method :active?, :active
33
+ alias_method :wild_card?, :has_wild_card
34
+ alias_method :has_wildcard, :has_wild_card
35
+ alias_method :wildcard?, :has_wild_card
36
+ alias_method :split_season?, :has_split_season
37
+ alias_method :playoff_points?, :has_playoff_points
38
+ alias_method :conferences?, :conferences_in_use
39
+ alias_method :divisions?, :divisions_in_use
40
+
41
+ json do
42
+ map "id", to: :id
43
+ map "name", to: :name
44
+ map "link", to: :link
45
+ map "abbreviation", to: :abbreviation
46
+ map "nameShort", to: :name_short
47
+ map "seasonState", to: :season_state
48
+ map "hasWildCard", to: :has_wild_card
49
+ map "hasSplitSeason", to: :has_split_season
50
+ map "numGames", to: :num_games
51
+ map "hasPlayoffPoints", to: :has_playoff_points
52
+ map "numTeams", to: :num_teams
53
+ map "numWildcardTeams", to: :num_wildcard_teams
54
+ map "seasonDateInfo", to: :season_date_info
55
+ map "season", to: :season
56
+ map "orgCode", to: :org_code
57
+ map "conferencesInUse", to: :conferences_in_use
58
+ map "divisionsInUse", to: :divisions_in_use
59
+ map "sport", to: :sport
60
+ map "sortOrder", to: :sort_order
61
+ map "active", to: :active
62
+ end
63
+
64
+ def <=>(other)
65
+ sort_order <=> other.sort_order
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,30 @@
1
+ require "shale"
2
+ require "uri"
3
+ require_relative "sport"
4
+ require_relative "league"
5
+
6
+ module MLB
7
+ class Leagues < Shale::Mapper
8
+ attribute :copyright, Shale::Type::String
9
+ attribute :leagues, League, collection: true
10
+
11
+ def self.all(sport: Sport.new(id: 1))
12
+ sport_id = sport.respond_to?(:id) ? sport.id : sport
13
+ params = {sportId: sport_id}
14
+ query_string = URI.encode_www_form(params)
15
+ response = CLIENT.get("leagues?#{query_string}")
16
+ leagues = from_json(response)
17
+ leagues.leagues.sort!
18
+ end
19
+
20
+ def self.find(league, sport: Sport.new(id: 1))
21
+ id = league.respond_to?(:id) ? league.id : league
22
+ sport_id = sport.respond_to?(:id) ? sport.id : sport
23
+ params = {sportId: sport_id}
24
+ query_string = URI.encode_www_form(params)
25
+ response = CLIENT.get("leagues/#{id}?#{query_string}")
26
+ leagues = from_json(response)
27
+ leagues.leagues.sort!.first
28
+ end
29
+ end
30
+ end