mlb 0.7.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +113 -103
  5. data/bin/console +10 -0
  6. data/bin/setup +6 -0
  7. data/lib/mlb/client.rb +63 -0
  8. data/lib/mlb/connection.rb +80 -0
  9. data/lib/mlb/division.rb +42 -0
  10. data/lib/mlb/divisions.rb +30 -0
  11. data/lib/mlb/error_handler.rb +53 -0
  12. data/lib/mlb/errors/bad_gateway.rb +5 -0
  13. data/lib/mlb/errors/bad_request.rb +5 -0
  14. data/lib/mlb/errors/client_error.rb +5 -0
  15. data/lib/mlb/errors/connection_exception.rb +5 -0
  16. data/lib/mlb/errors/error.rb +3 -0
  17. data/lib/mlb/errors/forbidden.rb +5 -0
  18. data/lib/mlb/errors/gateway_timeout.rb +5 -0
  19. data/lib/mlb/errors/gone.rb +5 -0
  20. data/lib/mlb/errors/http_error.rb +14 -0
  21. data/lib/mlb/errors/internal_server_error.rb +5 -0
  22. data/lib/mlb/errors/network_error.rb +5 -0
  23. data/lib/mlb/errors/not_acceptable.rb +5 -0
  24. data/lib/mlb/errors/not_found.rb +5 -0
  25. data/lib/mlb/errors/payload_too_large.rb +5 -0
  26. data/lib/mlb/errors/server_error.rb +5 -0
  27. data/lib/mlb/errors/service_unavailable.rb +5 -0
  28. data/lib/mlb/errors/too_many_redirects.rb +5 -0
  29. data/lib/mlb/errors/too_many_requests.rb +5 -0
  30. data/lib/mlb/errors/unauthorized.rb +5 -0
  31. data/lib/mlb/errors/unprocessable_entity.rb +5 -0
  32. data/lib/mlb/handedness.rb +13 -0
  33. data/lib/mlb/league.rb +66 -0
  34. data/lib/mlb/leagues.rb +30 -0
  35. data/lib/mlb/player.rb +62 -18
  36. data/lib/mlb/players.rb +25 -0
  37. data/lib/mlb/position.rb +17 -0
  38. data/lib/mlb/redirect_handler.rb +55 -0
  39. data/lib/mlb/request_builder.rb +48 -0
  40. data/lib/mlb/roster.rb +22 -0
  41. data/lib/mlb/roster_entry.rb +22 -0
  42. data/lib/mlb/season_date_info.rb +49 -0
  43. data/lib/mlb/sport.rb +31 -0
  44. data/lib/mlb/sports.rb +22 -0
  45. data/lib/mlb/status.rb +8 -0
  46. data/lib/mlb/team.rb +56 -127
  47. data/lib/mlb/teams.rb +30 -0
  48. data/lib/mlb/transaction.rb +31 -0
  49. data/lib/mlb/transactions.rb +18 -0
  50. data/lib/mlb/venue.rb +13 -0
  51. data/lib/mlb/venues.rb +30 -0
  52. data/lib/mlb/version.rb +3 -13
  53. data/lib/mlb.rb +9 -3
  54. data/sig/mlb.rbs +186 -0
  55. metadata +74 -102
  56. checksums.yaml.gz.sig +0 -0
  57. data/.yardopts +0 -5
  58. data/CONTRIBUTING.md +0 -49
  59. data/LICENSE.md +0 -20
  60. data/Rakefile +0 -48
  61. data/lib/mlb/request.rb +0 -24
  62. data/mlb.gemspec +0 -27
  63. data/spec/helper.rb +0 -32
  64. data/spec/mlb_spec.rb +0 -114
  65. data.tar.gz.sig +0 -1
  66. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e86303e2aa1810d5213ac3b1994bad065045a89f
4
- data.tar.gz: 0e8c5d53d3912fcbc27a9eac8b53e6bb8cd5cc68
2
+ SHA256:
3
+ metadata.gz: d6e28a22c9b934399e0edab2d391ef9dacc66209850caae9ad8e70a6bd524e1e
4
+ data.tar.gz: dd0f65a56582ec130bf1d46371fdf72cc4c48b6371af994632f0d663a6851c53
5
5
  SHA512:
6
- metadata.gz: 22d39a995a4edde5e03c3a4b294aaab99b94e5aa06db7cd0906f3ec5ea2a40435bf02be75337b2a1b23435590e7ccbe4705814765524f489fd7e9404332c313e
7
- data.tar.gz: a89f2d8eb70401b875534421efd4b21ec92f32641528cc94a90b91e7169b24b05fd0f1515b584f306548fd6b32aaf06d0775766d1e0d907bde50474bb9f1ca58
6
+ metadata.gz: 7b57c8c9eda053930153f421a9447c4cd65f4fd7a55612d027faa9633a61783660a338d0468a752d2666b83223f23b2c9649c9a977e72354cb21c879000e2543
7
+ data.tar.gz: 5c866fb121cc4d6972e717b7466474d97d7dd044418d84a573b19f6f94ffd5b65569d66bf2a2eca4997c3b6bfc64f93cadefac7e6501cc659bb256d975ab5d83
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.8.0] - 2024-05-22
2
+ * Rewrite to use MLB Stats API
3
+
4
+ ## [0.8.0] - 2024-05-06
5
+ * Rewrite
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-2024 Erik Berlin, Nilesh Trivedi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,109 +1,119 @@
1
- # MLB.rb
2
- [![Gem Version](https://badge.fury.io/rb/mlb.png)][gem]
3
- [![Build Status](https://secure.travis-ci.org/sferik/mlb.png?branch=master)][travis]
4
- [![Dependency Status](https://gemnasium.com/sferik/mlb.png?travis)][gemnasium]
5
- [![Code Climate](https://codeclimate.com/github/sferik/mlb.png)][codeclimate]
6
- [![Coverage Status](https://coveralls.io/repos/sferik/mlb/badge.png?branch=master)][coveralls]
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
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/fc32f80cb778b773a5b2/test_coverage)](https://codeclimate.com/github/sferik/mlb-ruby/test_coverage)
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
+ [![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)
6
+ [![Gem Version](https://badge.fury.io/rb/mlb.svg)](https://rubygems.org/gems/mlb)
7
7
 
8
- [gem]: https://rubygems.org/gems/mlb
9
- [travis]: http://travis-ci.org/sferik/mlb
10
- [gemnasium]: https://gemnasium.com/sferik/mlb
11
- [codeclimate]: https://codeclimate.com/github/sferik/mlb
12
- [coveralls]: https://coveralls.io/r/sferik/mlb
8
+ # A [Ruby](https://www.ruby-lang.org) interface to the [MLB Stats API](https://statsapi.mlb.com)
13
9
 
14
- MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
10
+ ## Follow
11
+
12
+ For updates and announcements, follow [@sferik](https://x.com/sferik).
15
13
 
16
14
  ## Installation
15
+
16
+ Install the gem and add to the application's Gemfile:
17
+
18
+ bundle add mlb
19
+
20
+ Or, if Bundler is not being used to manage dependencies:
21
+
17
22
  gem install mlb
18
23
 
19
- To ensure the code you're installing hasn't been tampered with, it's
20
- recommended that you verify the signature. To do this, you need to add my
21
- public key as a trusted certificate (you only need to do this once):
22
-
23
- gem cert --add <(curl -Ls https://raw.github.com/sferik/mlb/master/certs/sferik.pem)
24
-
25
- Then, install the gem with the high security trust policy:
26
-
27
- gem install mlb -P HighSecurity
28
-
29
- ## Documentation
30
- [http://rdoc.info/gems/mlb][documentation]
31
-
32
- [documentation]: http://rdoc.info/gems/mlb
33
-
34
- ## Usage Examples
35
- $ irb
36
- >> require 'mlb'
37
- >> MLB::Team.all.first.name # => "Arizona Diamondbacks"
38
- >> MLB::Team.all.first.league # => "National League"
39
- >> MLB::Team.all.first.division # => "National League West"
40
- >> MLB::Team.all.first.manager # => "Bob Melvin"
41
- >> MLB::Team.all.first.wins # => 82
42
- >> MLB::Team.all.first.losses # => 80
43
- >> MLB::Team.all.first.founded # => 1998
44
- >> MLB::Team.all.first.mascot # => nil
45
- >> MLB::Team.all.first.ballpark # => "Chase Field"
46
- >> MLB::Team.all.first.logo_url # => "http://img.freebase.com/api/trans/image_thumb/wikipedia/images/en_id/13104064"
47
- >> MLB::Team.all.first.players.first.name # => "Alex Romero"
48
- >> MLB::Team.all.first.players.first.number # => 28
49
- >> MLB::Team.all.first.players.first.position # => "Right fielder"
50
-
51
- ## Supported Ruby Versions
52
- This library aims to support and is [tested against][travis] the following Ruby
53
- implementations:
54
-
55
- * Ruby 1.8.7
56
- * Ruby 1.9.2
57
- * Ruby 1.9.3
58
- * Ruby 2.0.0
59
- * Ruby 2.1.0
60
- * [Rubinius][]
61
- * [JRuby][]
62
-
63
- [rubinius]: http://rubini.us/
64
- [jruby]: http://jruby.org/
65
-
66
- If something doesn't work on one of these interpreters, it's a bug.
67
-
68
- This library may inadvertently work (or seem to work) on other Ruby
69
- implementations, however support will only be provided for the versions listed
70
- above.
71
-
72
- If you would like this library to support another Ruby version, you may
73
- volunteer to be a maintainer. Being a maintainer entails making sure all tests
74
- run and pass on that implementation. When something breaks on your
75
- implementation, you will be responsible for providing patches in a timely
76
- fashion. If critical issues for a particular implementation exist at the time
77
- of a major release, support for that Ruby version may be dropped.
78
-
79
- ## Colophon
80
- MLB was built with the following tools:
81
-
82
- * [Bundler][]
83
- * [Freebase][]
84
- * [Faraday][]
85
- * [Markdown][]
86
- * [MultiJSON][]
87
- * [RSpec][]
88
- * [SimpleCov][]
89
- * [SQLite][]
90
- * [vim][]
91
- * [WebMock][]
92
- * [YARD][]
93
-
94
- [bundler]: http://gembundler.com/
95
- [freebase]: http://www.freebase.com/
96
- [faraday]: https://github.com/technoweenie/faraday
97
- [markdown]: http://daringfireball.net/projects/markdown/
98
- [multijson]: https://github.com/intridea/multi_json
99
- [rspec]: http://relishapp.com/rspec/
100
- [simplecov]: https://github.com/colszowka/simplecov
101
- [sqlite]: http://www.sqlite.org/
102
- [vim]: http://www.vim.org/
103
- [webmock]: https://github.com/bblimke/webmock
104
- [yard]: http://yardoc.org/
105
-
106
- ## Copyright
107
- Copyright (c) 2010-2013 Erik Michaels-Ober. See [LICENSE][] for details.
108
-
109
- [license]: LICENSE.md
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require "mlb"
28
+
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.all(season: 2024).find { |player| player.id == mets.roster.first.player.id }
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
57
+ ```
58
+
59
+ ## Sponsorship
60
+
61
+ By contributing to the project, you help:
62
+
63
+ 1. Maintain the library: Keeping it up-to-date and secure.
64
+ 2. Add new features: Enhancements that make your life easier.
65
+ 3. Provide support: Faster responses to issues and feature requests.
66
+
67
+ ⭐️ Bonus: Sponsors will get priority support and influence over the project roadmap. We will also list your name or your company's logo on our GitHub page.
68
+
69
+ Building and maintaining an open-source project like this takes a considerable amount of time and effort. Your sponsorship can help sustain this project. Even a small monthly donation makes a huge difference!
70
+
71
+ [Click here to sponsor this project.](https://github.com/sponsors/sferik)
72
+
73
+ ## Development
74
+
75
+ 1. Checkout and repo:
76
+
77
+ git checkout git@github.com:sferik/mlb-ruby.git
78
+
79
+ 2. Enter the repo’s directory:
80
+
81
+ cd mlb-ruby
82
+
83
+ 3. Install dependencies via Bundler:
84
+
85
+ bin/setup
86
+
87
+ 4. Run the default Rake task to ensure all tests pass:
88
+
89
+ bundle exec rake
90
+
91
+ 5. Create a new branch for your feature or bug fix:
92
+
93
+ git checkout -b my-new-branch
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/mlb-ruby.
98
+
99
+ Pull requests will only be accepted if they meet all the following criteria:
100
+
101
+ 1. Code must conform to [Standard Ruby](https://github.com/standardrb/standard#readme). This can be verified with:
102
+
103
+ bundle exec rake standard
104
+
105
+ 2. Code must conform to the [RuboCop rules](https://github.com/rubocop/rubocop#readme). This can be verified with:
106
+
107
+ bundle exec rake rubocop
108
+
109
+ 3. 100% C0 code coverage. This can be verified with:
110
+
111
+ bundle exec rake test
112
+
113
+ 4. 100% mutation coverage. This can be verified with:
114
+
115
+ bundle exec rake mutant
116
+
117
+ ## License
118
+
119
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mlb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "irb"
10
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/lib/mlb/client.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "forwardable"
2
+ require_relative "connection"
3
+ require_relative "redirect_handler"
4
+ require_relative "request_builder"
5
+ require_relative "error_handler"
6
+
7
+ module MLB
8
+ class Client
9
+ extend Forwardable
10
+
11
+ DEFAULT_BASE_URL = "https://statsapi.mlb.com/api/v1/".freeze
12
+
13
+ attr_accessor :base_url
14
+
15
+ def_delegators :@connection, :open_timeout, :read_timeout, :write_timeout, :proxy_url, :debug_output
16
+ def_delegators :@connection, :open_timeout=, :read_timeout=, :write_timeout=, :proxy_url=, :debug_output=
17
+ def_delegators :@redirect_handler, :max_redirects
18
+ def_delegators :@redirect_handler, :max_redirects=
19
+
20
+ def initialize(base_url: DEFAULT_BASE_URL,
21
+ open_timeout: Connection::DEFAULT_OPEN_TIMEOUT,
22
+ read_timeout: Connection::DEFAULT_READ_TIMEOUT,
23
+ write_timeout: Connection::DEFAULT_WRITE_TIMEOUT,
24
+ debug_output: Connection::DEFAULT_DEBUG_OUTPUT,
25
+ proxy_url: nil,
26
+ max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS)
27
+
28
+ @base_url = base_url
29
+ @connection = Connection.new(open_timeout:, read_timeout:, write_timeout:, debug_output:, proxy_url:)
30
+ @request_builder = RequestBuilder.new
31
+ @redirect_handler = RedirectHandler.new(connection: @connection, request_builder: @request_builder, max_redirects:)
32
+ @error_handler = ErrorHandler.new
33
+ end
34
+
35
+ def get(endpoint, headers: {})
36
+ execute_request(:get, endpoint, headers:)
37
+ end
38
+
39
+ def post(endpoint, body = nil, headers: {})
40
+ execute_request(:post, endpoint, body:, headers:)
41
+ end
42
+
43
+ def put(endpoint, body = nil, headers: {})
44
+ execute_request(:put, endpoint, body:, headers:)
45
+ end
46
+
47
+ def delete(endpoint, headers: {})
48
+ execute_request(:delete, endpoint, headers:)
49
+ end
50
+
51
+ private
52
+
53
+ def execute_request(http_method, endpoint, headers:, body: nil)
54
+ uri = URI.join(base_url, endpoint)
55
+ request = @request_builder.build(http_method:, uri:, body:, headers:)
56
+ response = @connection.perform(request:)
57
+ response = @redirect_handler.handle(response:, request:, base_url:)
58
+ @error_handler.handle(response:)
59
+ end
60
+ end
61
+
62
+ CLIENT = Client.new
63
+ end
@@ -0,0 +1,80 @@
1
+ require "forwardable"
2
+ require "net/http"
3
+ require "openssl"
4
+ require "uri"
5
+ require_relative "errors/network_error"
6
+
7
+ module MLB
8
+ class Connection
9
+ extend Forwardable
10
+
11
+ DEFAULT_HOST = "statsapi.mlb.com".freeze
12
+ DEFAULT_PORT = 443
13
+ DEFAULT_OPEN_TIMEOUT = 60 # seconds
14
+ DEFAULT_READ_TIMEOUT = 60 # seconds
15
+ DEFAULT_WRITE_TIMEOUT = 60 # seconds
16
+ DEFAULT_DEBUG_OUTPUT = File.open(File::NULL, "w")
17
+ NETWORK_ERRORS = [
18
+ Errno::ECONNREFUSED,
19
+ Errno::ECONNRESET,
20
+ Net::OpenTimeout,
21
+ Net::ReadTimeout,
22
+ OpenSSL::SSL::SSLError
23
+ ].freeze
24
+
25
+ attr_accessor :open_timeout, :read_timeout, :write_timeout, :debug_output
26
+ attr_reader :proxy_url, :proxy_uri
27
+
28
+ def_delegator :proxy_uri, :host, :proxy_host
29
+ def_delegator :proxy_uri, :port, :proxy_port
30
+ def_delegator :proxy_uri, :user, :proxy_user
31
+ def_delegator :proxy_uri, :password, :proxy_pass
32
+
33
+ def initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT,
34
+ write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: DEFAULT_DEBUG_OUTPUT, proxy_url: nil)
35
+ @open_timeout = open_timeout
36
+ @read_timeout = read_timeout
37
+ @write_timeout = write_timeout
38
+ @debug_output = debug_output
39
+ self.proxy_url = proxy_url unless proxy_url.nil?
40
+ end
41
+
42
+ def perform(request:)
43
+ host = request.uri.host || DEFAULT_HOST
44
+ port = request.uri.port || DEFAULT_PORT
45
+ http_client = build_http_client(host, port)
46
+ http_client.use_ssl = request.uri.scheme.eql?("https")
47
+ http_client.request(request)
48
+ rescue *NETWORK_ERRORS => e
49
+ raise NetworkError, "Network error: #{e}"
50
+ end
51
+
52
+ def proxy_url=(proxy_url)
53
+ @proxy_url = proxy_url
54
+ proxy_uri = URI(proxy_url)
55
+ raise ArgumentError, "Invalid proxy URL: #{proxy_uri}" unless proxy_uri.is_a?(URI::HTTP)
56
+
57
+ @proxy_uri = proxy_uri
58
+ end
59
+
60
+ private
61
+
62
+ def build_http_client(host = DEFAULT_HOST, port = DEFAULT_PORT)
63
+ http_client = if proxy_uri
64
+ Net::HTTP.new(host, port, proxy_host, proxy_port, proxy_user, proxy_pass)
65
+ else
66
+ Net::HTTP.new(host, port)
67
+ end
68
+ configure_http_client(http_client)
69
+ end
70
+
71
+ def configure_http_client(http_client)
72
+ http_client.tap do |c|
73
+ c.open_timeout = open_timeout
74
+ c.read_timeout = read_timeout
75
+ c.write_timeout = write_timeout
76
+ c.set_debug_output(debug_output)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,42 @@
1
+ require "shale"
2
+ require_relative "league"
3
+ require_relative "sport"
4
+
5
+ module MLB
6
+ class Division < Shale::Mapper
7
+ include Comparable
8
+
9
+ attribute :id, Shale::Type::Integer
10
+ attribute :name, Shale::Type::String
11
+ attribute :season, Shale::Type::String
12
+ attribute :name_short, Shale::Type::String
13
+ attribute :link, Shale::Type::String
14
+ attribute :abbreviation, Shale::Type::String
15
+ attribute :league, League
16
+ attribute :sport, Sport
17
+ attribute :has_wildcard, Shale::Type::Boolean
18
+ attribute :sort_order, Shale::Type::Integer
19
+ attribute :active, Shale::Type::Boolean
20
+
21
+ alias_method :active?, :active
22
+ alias_method :wildcard?, :has_wildcard
23
+
24
+ json do
25
+ map "id", to: :id
26
+ map "name", to: :name
27
+ map "season", to: :season
28
+ map "nameShort", to: :name_short
29
+ map "link", to: :link
30
+ map "abbreviation", to: :abbreviation
31
+ map "league", to: :league
32
+ map "sport", to: :sport
33
+ map "hasWildcard", to: :has_wildcard
34
+ map "sortOrder", to: :sort_order
35
+ map "active", to: :active
36
+ end
37
+
38
+ def <=>(other)
39
+ sort_order <=> other.sort_order
40
+ end
41
+ end
42
+ 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.first
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ require "net/http"
2
+ require_relative "errors/bad_gateway"
3
+ require_relative "errors/bad_request"
4
+ require_relative "errors/connection_exception"
5
+ require_relative "errors/http_error"
6
+ require_relative "errors/forbidden"
7
+ require_relative "errors/gateway_timeout"
8
+ require_relative "errors/gone"
9
+ require_relative "errors/internal_server_error"
10
+ require_relative "errors/not_acceptable"
11
+ require_relative "errors/not_found"
12
+ require_relative "errors/payload_too_large"
13
+ require_relative "errors/service_unavailable"
14
+ require_relative "errors/too_many_requests"
15
+ require_relative "errors/unauthorized"
16
+ require_relative "errors/unprocessable_entity"
17
+
18
+ module MLB
19
+ class ErrorHandler
20
+ ERROR_MAP = {
21
+ 400 => BadRequest,
22
+ 401 => Unauthorized,
23
+ 403 => Forbidden,
24
+ 404 => NotFound,
25
+ 406 => NotAcceptable,
26
+ 409 => ConnectionException,
27
+ 410 => Gone,
28
+ 413 => PayloadTooLarge,
29
+ 422 => UnprocessableEntity,
30
+ 429 => TooManyRequests,
31
+ 500 => InternalServerError,
32
+ 502 => BadGateway,
33
+ 503 => ServiceUnavailable,
34
+ 504 => GatewayTimeout
35
+ }.freeze
36
+
37
+ def handle(response:)
38
+ raise error(response) unless response.is_a?(Net::HTTPSuccess)
39
+
40
+ response.body
41
+ end
42
+
43
+ private
44
+
45
+ def error(response)
46
+ error_class(response).new(response:)
47
+ end
48
+
49
+ def error_class(response)
50
+ ERROR_MAP[Integer(response.code)] || HTTPError
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "server_error"
2
+
3
+ module MLB
4
+ class BadGateway < ServerError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class BadRequest < ClientError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "http_error"
2
+
3
+ module MLB
4
+ class ClientError < HTTPError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class ConnectionException < ClientError; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module MLB
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class Forbidden < ClientError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "server_error"
2
+
3
+ module MLB
4
+ class GatewayTimeout < ServerError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class Gone < ClientError; end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "json"
2
+ require_relative "error"
3
+
4
+ module MLB
5
+ class HTTPError < Error
6
+ attr_reader :response, :code
7
+
8
+ def initialize(response:)
9
+ super(response.message)
10
+ @response = response
11
+ @code = response.code
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "server_error"
2
+
3
+ module MLB
4
+ class InternalServerError < ServerError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "error"
2
+
3
+ module MLB
4
+ class NetworkError < Error; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class NotAcceptable < ClientError; end
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "client_error"
2
+
3
+ module MLB
4
+ class NotFound < ClientError; end
5
+ end