mlb 0.7.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -103
- data/bin/console +10 -0
- data/bin/setup +6 -0
- data/lib/mlb/client.rb +63 -0
- data/lib/mlb/connection.rb +80 -0
- data/lib/mlb/division.rb +42 -0
- data/lib/mlb/divisions.rb +30 -0
- data/lib/mlb/error_handler.rb +53 -0
- data/lib/mlb/errors/bad_gateway.rb +5 -0
- data/lib/mlb/errors/bad_request.rb +5 -0
- data/lib/mlb/errors/client_error.rb +5 -0
- data/lib/mlb/errors/connection_exception.rb +5 -0
- data/lib/mlb/errors/error.rb +3 -0
- data/lib/mlb/errors/forbidden.rb +5 -0
- data/lib/mlb/errors/gateway_timeout.rb +5 -0
- data/lib/mlb/errors/gone.rb +5 -0
- data/lib/mlb/errors/http_error.rb +14 -0
- data/lib/mlb/errors/internal_server_error.rb +5 -0
- data/lib/mlb/errors/network_error.rb +5 -0
- data/lib/mlb/errors/not_acceptable.rb +5 -0
- data/lib/mlb/errors/not_found.rb +5 -0
- data/lib/mlb/errors/payload_too_large.rb +5 -0
- data/lib/mlb/errors/server_error.rb +5 -0
- data/lib/mlb/errors/service_unavailable.rb +5 -0
- data/lib/mlb/errors/too_many_redirects.rb +5 -0
- data/lib/mlb/errors/too_many_requests.rb +5 -0
- data/lib/mlb/errors/unauthorized.rb +5 -0
- data/lib/mlb/errors/unprocessable_entity.rb +5 -0
- data/lib/mlb/handedness.rb +13 -0
- data/lib/mlb/league.rb +66 -0
- data/lib/mlb/leagues.rb +30 -0
- data/lib/mlb/player.rb +62 -18
- data/lib/mlb/players.rb +25 -0
- data/lib/mlb/position.rb +17 -0
- data/lib/mlb/redirect_handler.rb +55 -0
- data/lib/mlb/request_builder.rb +48 -0
- data/lib/mlb/roster.rb +22 -0
- data/lib/mlb/roster_entry.rb +22 -0
- data/lib/mlb/season_date_info.rb +49 -0
- data/lib/mlb/sport.rb +31 -0
- data/lib/mlb/sports.rb +22 -0
- data/lib/mlb/status.rb +8 -0
- data/lib/mlb/team.rb +56 -127
- data/lib/mlb/teams.rb +30 -0
- data/lib/mlb/transaction.rb +31 -0
- data/lib/mlb/transactions.rb +18 -0
- data/lib/mlb/venue.rb +13 -0
- data/lib/mlb/venues.rb +30 -0
- data/lib/mlb/version.rb +3 -13
- data/lib/mlb.rb +9 -3
- data/sig/mlb.rbs +186 -0
- metadata +74 -102
- checksums.yaml.gz.sig +0 -0
- data/.yardopts +0 -5
- data/CONTRIBUTING.md +0 -49
- data/LICENSE.md +0 -20
- data/Rakefile +0 -48
- data/lib/mlb/request.rb +0 -24
- data/mlb.gemspec +0 -27
- data/spec/helper.rb +0 -32
- data/spec/mlb_spec.rb +0 -114
- data.tar.gz.sig +0 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d6e28a22c9b934399e0edab2d391ef9dacc66209850caae9ad8e70a6bd524e1e
|
4
|
+
data.tar.gz: dd0f65a56582ec130bf1d46371fdf72cc4c48b6371af994632f0d663a6851c53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b57c8c9eda053930153f421a9447c4cd65f4fd7a55612d027faa9633a61783660a338d0468a752d2666b83223f23b2c9649c9a977e72354cb21c879000e2543
|
7
|
+
data.tar.gz: 5c866fb121cc4d6972e717b7466474d97d7dd044418d84a573b19f6f94ffd5b65569d66bf2a2eca4997c3b6bfc64f93cadefac7e6501cc659bb256d975ab5d83
|
data/CHANGELOG.md
ADDED
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
|
-
|
2
|
-
[![
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
[![
|
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
|
-
[
|
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
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
[
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
[
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
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
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
|
data/lib/mlb/division.rb
ADDED
@@ -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
|