mlb_rb 0.0.2 → 0.0.5
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 +4 -4
- data/CHANGELOG.md +15 -2
- data/Gemfile.lock +2 -1
- data/README.md +12 -2
- data/lib/mlb_rb/client.rb +26 -2
- data/lib/mlb_rb/errors.rb +5 -0
- data/lib/mlb_rb/game.rb +16 -0
- data/lib/mlb_rb/games.rb +51 -0
- data/lib/mlb_rb/version.rb +1 -1
- data/lib/mlb_rb.rb +1 -41
- metadata +9 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d8531eef783590d9d243f53f2f8a29f9cd0a590d8d4191a427c5a7997ea86f33
|
|
4
|
+
data.tar.gz: 496df599f736b806cc38aacb8df5543481d9a54eec849daa1fa6853e73bd00b5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 87ce9910bc1b335c384f8fa2307929f73aeb7996db1d44381dc65f7cc177e767ec7e27b2b32213dc420c42b3f9b320f2cc8009435b2df045a69b72aef050aa81
|
|
7
|
+
data.tar.gz: f41dded18c3439711a3b101e308dbd315a7529939c16b8ab1fd4fd1495f99372efd7fb6f42a0c7f72ccc5ba88647559994942e0f07285d9c86405056db02875b
|
data/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
-
## [0.0.
|
|
3
|
+
## [0.0.5] - 2022-04-09
|
|
4
|
+
|
|
5
|
+
- Change to only pull games that are final
|
|
6
|
+
|
|
7
|
+
## [0.0.4] - 2022-03-26
|
|
8
|
+
|
|
9
|
+
- Clean up main class and extract games methods to MlbRb::Games class
|
|
10
|
+
|
|
11
|
+
## [0.0.3] - 2022-03-26
|
|
12
|
+
|
|
13
|
+
- Add method to retrieve games for date range
|
|
14
|
+
|
|
15
|
+
## [0.0.2] - 2022-03-25
|
|
16
|
+
|
|
17
|
+
- Add method to retrieve games for one date
|
|
4
18
|
|
|
5
|
-
- Add endpoint to retrieve games for one date
|
|
6
19
|
## [0.0.1] - 2022-03-24
|
|
7
20
|
|
|
8
21
|
- Add healthcheck that returns a string
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mlb_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
I started writing this gem because I wanted to extract a piece of a rails app that I had previously used to a gem, but the API changed from the first time I used it, so I had to rewrite it anyway, so instead of starting a new rails app immediately. I figured I could kill two birds and learn to write a gem and use the new MLB API.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -22,7 +22,17 @@ Or install it yourself as:
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
There are currently only two methods available:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
MlbRb::Games.games_for_date({ date: { year: 2022, month: 3, day: 24 } })
|
|
29
|
+
MlbRb::Games.games_for_date_range(
|
|
30
|
+
{
|
|
31
|
+
start_date: { year: 2022, month: 3, day: 23 },
|
|
32
|
+
end_date: { year: 2022, month: 3, day: 24 }
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
```
|
|
26
36
|
|
|
27
37
|
## Development
|
|
28
38
|
|
data/lib/mlb_rb/client.rb
CHANGED
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "net/http"
|
|
4
|
+
require "json"
|
|
4
5
|
|
|
5
6
|
module MlbRb
|
|
6
7
|
class Client
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
attr_reader :json_response
|
|
9
|
+
|
|
10
|
+
def initialize(json_response)
|
|
11
|
+
@json_response = json_response
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def parse_games_by_date_response
|
|
15
|
+
JSON.parse(json_response)["dates"].map do |date_hash|
|
|
16
|
+
date_hash["games"].map do |game|
|
|
17
|
+
final_state = game["status"]["detailedState"] == "Final"
|
|
18
|
+
Game.new(game) if final_state
|
|
19
|
+
end
|
|
20
|
+
end.flatten.compact
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
def get_games_for_date(date)
|
|
25
|
+
json_response = Net::HTTP.get("statsapi.mlb.com", "/api/v1/schedule/games/?sportId=1&date=#{date}")
|
|
26
|
+
new(json_response).parse_games_by_date_response
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def get_games_for_range(start_date, end_date)
|
|
30
|
+
json_response = Net::HTTP.get("statsapi.mlb.com", "/api/v1/schedule/games/?sportId=1&startDate=#{start_date}&endDate=#{end_date}")
|
|
31
|
+
new(json_response).parse_games_by_date_response
|
|
32
|
+
end
|
|
9
33
|
end
|
|
10
34
|
end
|
|
11
35
|
end
|
data/lib/mlb_rb/game.rb
CHANGED
|
@@ -20,5 +20,21 @@ module MlbRb
|
|
|
20
20
|
@away_score = game_hash["teams"]["away"]["score"]
|
|
21
21
|
@game_date = Date.parse(game_hash["gameDate"])
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
def home_team_name
|
|
25
|
+
home_team.name
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def home_team_id
|
|
29
|
+
home_team.id
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def away_team_name
|
|
33
|
+
away_team.name
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def away_team_id
|
|
37
|
+
away_team.id
|
|
38
|
+
end
|
|
23
39
|
end
|
|
24
40
|
end
|
data/lib/mlb_rb/games.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require "./lib/mlb_rb/errors"
|
|
2
|
+
require "./lib/mlb_rb/client"
|
|
3
|
+
require "./lib/mlb_rb/game"
|
|
4
|
+
|
|
5
|
+
module MlbRb
|
|
6
|
+
class Games
|
|
7
|
+
class << self
|
|
8
|
+
def games_for_date(options)
|
|
9
|
+
date = options[:date]
|
|
10
|
+
raise DateError unless validate_date(date)
|
|
11
|
+
|
|
12
|
+
client.get_games_for_date(format_date(date))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def games_for_date_range(options)
|
|
16
|
+
start_date = options[:start_date]
|
|
17
|
+
end_date = options[:end_date]
|
|
18
|
+
[start_date, end_date].each do |date|
|
|
19
|
+
raise DateError unless validate_date(date)
|
|
20
|
+
end
|
|
21
|
+
client.get_games_for_range(format_date(start_date), format_date(end_date))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def format_date(date)
|
|
27
|
+
"#{"%02d" % date[:month]}/#{"%02d" % date[:day]}/#{date[:year]}"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def validate_date(date)
|
|
31
|
+
date.map do |key, value|
|
|
32
|
+
valid_number_for_date(value, key)
|
|
33
|
+
end.all?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def valid_number_for_date(date_number, type)
|
|
37
|
+
if type == :year
|
|
38
|
+
date_number.between?(1876, Date.today.year)
|
|
39
|
+
elsif type == :month
|
|
40
|
+
date_number.between?(1, 12)
|
|
41
|
+
elsif type == :day
|
|
42
|
+
date_number.between?(1, 31)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def client
|
|
47
|
+
MlbRb::Client
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/mlb_rb/version.rb
CHANGED
data/lib/mlb_rb.rb
CHANGED
|
@@ -1,52 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "./lib/mlb_rb/version"
|
|
4
|
-
require "./lib/mlb_rb/
|
|
5
|
-
require "./lib/mlb_rb/game"
|
|
6
|
-
require "json"
|
|
4
|
+
require "./lib/mlb_rb/games"
|
|
7
5
|
|
|
8
6
|
module MlbRb
|
|
9
|
-
class Error < StandardError; end
|
|
10
|
-
|
|
11
|
-
class DateError < StandardError; end
|
|
12
|
-
|
|
13
7
|
class << self
|
|
14
8
|
def healthcheck
|
|
15
9
|
"Yup, everything is fine"
|
|
16
10
|
end
|
|
17
|
-
|
|
18
|
-
def games_for_date(options)
|
|
19
|
-
date = options[:date]
|
|
20
|
-
raise DateError unless validate_date(date)
|
|
21
|
-
|
|
22
|
-
formatted_date = "#{"%02d" % date[:month]}/#{"%02d" % date[:day]}/#{date[:year]}"
|
|
23
|
-
games_json = client.get_games_for_date(formatted_date)
|
|
24
|
-
games = JSON.parse(games_json)["dates"].first["games"]
|
|
25
|
-
games.map { |game| Game.new(game) }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
private
|
|
29
|
-
|
|
30
|
-
def validate_date(date)
|
|
31
|
-
return unless date
|
|
32
|
-
|
|
33
|
-
date.map do |key, value|
|
|
34
|
-
valid_number_for_date(value, key)
|
|
35
|
-
end.all?
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def valid_number_for_date(date_number, type)
|
|
39
|
-
if type == :year
|
|
40
|
-
date_number.between?(1876, Date.today.year)
|
|
41
|
-
elsif type == :month
|
|
42
|
-
date_number.between?(1, 12)
|
|
43
|
-
elsif type == :day
|
|
44
|
-
date_number.between?(1, 31)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def client
|
|
49
|
-
MlbRb::Client
|
|
50
|
-
end
|
|
51
11
|
end
|
|
52
12
|
end
|
metadata
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mlb_rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mark Miranda
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-
|
|
11
|
+
date: 2022-04-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description:
|
|
13
|
+
description:
|
|
14
14
|
email:
|
|
15
15
|
- mmiranda@doximity.com
|
|
16
16
|
executables: []
|
|
@@ -30,7 +30,9 @@ files:
|
|
|
30
30
|
- bin/setup
|
|
31
31
|
- lib/mlb_rb.rb
|
|
32
32
|
- lib/mlb_rb/client.rb
|
|
33
|
+
- lib/mlb_rb/errors.rb
|
|
33
34
|
- lib/mlb_rb/game.rb
|
|
35
|
+
- lib/mlb_rb/games.rb
|
|
34
36
|
- lib/mlb_rb/team.rb
|
|
35
37
|
- lib/mlb_rb/version.rb
|
|
36
38
|
- mlb_rb.gemspec
|
|
@@ -42,7 +44,7 @@ metadata:
|
|
|
42
44
|
homepage_uri: https://www.github.com/notmarkmiranda/mlb_rb
|
|
43
45
|
source_code_uri: https://www.github.com/notmarkmiranda/mlb_rb
|
|
44
46
|
changelog_uri: https://www.github.com/notmarkmiranda/mlb_rb/blob/main/CHANGELOG.md
|
|
45
|
-
post_install_message:
|
|
47
|
+
post_install_message:
|
|
46
48
|
rdoc_options: []
|
|
47
49
|
require_paths:
|
|
48
50
|
- lib
|
|
@@ -57,8 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
57
59
|
- !ruby/object:Gem::Version
|
|
58
60
|
version: '0'
|
|
59
61
|
requirements: []
|
|
60
|
-
rubygems_version: 3.2.
|
|
61
|
-
signing_key:
|
|
62
|
+
rubygems_version: 3.2.32
|
|
63
|
+
signing_key:
|
|
62
64
|
specification_version: 4
|
|
63
65
|
summary: MLB API Wrapper for Ruby
|
|
64
66
|
test_files: []
|