mlb_rb 0.0.1 → 0.0.4

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: 310accad978cc9d84cd83bb5239980ab8e6a131a3b961b18031982d63701dbec
4
- data.tar.gz: ae4db5eb92b6ce49cb5276b3b54c1389909dafa66dad6f10e2229133c9e2090d
3
+ metadata.gz: 9425c40e4217cb73a792654e606c3bf7f2433f666a4ce3d07a0f30a10429f073
4
+ data.tar.gz: 5a9442a40f8e4a6b5b01545f31253159fce41a937b9922918e0fdfbf27020e12
5
5
  SHA512:
6
- metadata.gz: 0d3ec6aafac47c26fcd80786ee478ce794779cd23e1c32ee1040a1db4a2f5eac6e6e78fbf5f807a67966c2605e39e2de7fe5bed07e8c42e7f08eaf9d47b392af
7
- data.tar.gz: eb3f00d63cbb92db50d81ab03248890093a466e7f06b10c4efcd1a989ae6ea5c25f488d9fc5de6d4092ec3ed708d4c1192af121da97899f50aa8e06857c80b6d
6
+ metadata.gz: cb2cb494634507cf491832e7b6a2133e933c25867f034388c7b4617eca2fc1999a297109b5dc13a8b277f8537c59b0051041ed0b9f3a244d806e9d614ae46f9e
7
+ data.tar.gz: fff2f9342f928a7df737e8b3b21d6c25ab5cf4a8a19d8b2d78e247e0ea9f8ffe309e42ddb3a23fbc07f16041c68e250b1c36163a5eb01c85e0bfc244b1c97526
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.4] - 2022-03-26
4
+
5
+ - Clean up main class and extract games methods to MlbRb::Games class
6
+
7
+ ## [0.0.3] - 2022-03-26
8
+
9
+ - Add method to retrieve games for date range
10
+
11
+ ## [0.0.2] - 2022-03-25
12
+
13
+ - Add method to retrieve games for one date
14
+
3
15
  ## [0.0.1] - 2022-03-24
4
16
 
5
17
  - Add healthcheck that returns a string
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mlb_rb (0.0.4)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.22.0)
12
+ parser (3.1.1.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.2.1)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.26.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.16.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.16.0)
41
+ parser (>= 3.1.1.0)
42
+ rubocop-performance (1.13.3)
43
+ rubocop (>= 1.7.0, < 2.0)
44
+ rubocop-ast (>= 0.4.0)
45
+ ruby-progressbar (1.11.0)
46
+ standard (1.9.0)
47
+ rubocop (= 1.26.0)
48
+ rubocop-performance (= 1.13.3)
49
+ unicode-display_width (2.1.0)
50
+
51
+ PLATFORMS
52
+ x86_64-darwin-20
53
+ x86_64-linux
54
+
55
+ DEPENDENCIES
56
+ mlb_rb!
57
+ rake (~> 13.0)
58
+ rspec (~> 3.0)
59
+ standard (~> 1.3)
60
+
61
+ BUNDLED WITH
62
+ 2.3.1
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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
 
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ module MlbRb
7
+ class Client
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 { |game| Game.new(game) }
17
+ end.flatten
18
+ end
19
+
20
+ class << self
21
+ def get_games_for_date(date)
22
+ json_response = Net::HTTP.get("statsapi.mlb.com", "/api/v1/schedule/games/?sportId=1&date=#{date}")
23
+ new(json_response).parse_games_by_date_response
24
+ end
25
+
26
+ def get_games_for_range(start_date, end_date)
27
+ json_response = Net::HTTP.get("statsapi.mlb.com", "/api/v1/schedule/games/?sportId=1&startDate=#{start_date}&endDate=#{end_date}")
28
+ new(json_response).parse_games_by_date_response
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MlbRb
4
+ class DateError < StandardError; end
5
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/mlb_rb/team"
4
+ require "date"
5
+
6
+ module MlbRb
7
+ class Game
8
+ attr_reader :game_pk,
9
+ :home_team,
10
+ :away_team,
11
+ :home_score,
12
+ :away_score,
13
+ :game_date
14
+
15
+ def initialize(game_hash)
16
+ @game_pk = game_hash["gamePk"]
17
+ @home_team = Team.new(game_hash["teams"]["home"]["team"])
18
+ @away_team = Team.new(game_hash["teams"]["away"]["team"])
19
+ @home_score = game_hash["teams"]["home"]["score"]
20
+ @away_score = game_hash["teams"]["away"]["score"]
21
+ @game_date = Date.parse(game_hash["gameDate"])
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
39
+ end
40
+ end
@@ -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
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MlbRb
4
+ class Team
5
+ attr_reader :id, :name
6
+ def initialize(team_hash)
7
+ @id = team_hash["id"]
8
+ @name = team_hash["name"]
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MlbRb
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.4"
5
5
  end
data/lib/mlb_rb.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "mlb_rb/version"
3
+ require "./lib/mlb_rb/version"
4
+ require "./lib/mlb_rb/games"
4
5
 
5
6
  module MlbRb
6
- class Error < StandardError; end
7
-
8
- def self.healthcheck
9
- "Yup, everything is fine"
7
+ class << self
8
+ def healthcheck
9
+ "Yup, everything is fine"
10
+ end
10
11
  end
11
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.1
4
+ version: 0.0.4
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-03-24 00:00:00.000000000 Z
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - mmiranda@doximity.com
16
16
  executables: []
@@ -22,12 +22,18 @@ files:
22
22
  - CHANGELOG.md
23
23
  - CODE_OF_CONDUCT.md
24
24
  - Gemfile
25
+ - Gemfile.lock
25
26
  - LICENSE.txt
26
27
  - README.md
27
28
  - Rakefile
28
29
  - bin/console
29
30
  - bin/setup
30
31
  - lib/mlb_rb.rb
32
+ - lib/mlb_rb/client.rb
33
+ - lib/mlb_rb/errors.rb
34
+ - lib/mlb_rb/game.rb
35
+ - lib/mlb_rb/games.rb
36
+ - lib/mlb_rb/team.rb
31
37
  - lib/mlb_rb/version.rb
32
38
  - mlb_rb.gemspec
33
39
  - sig/mlb_rb.rbs
@@ -38,7 +44,7 @@ metadata:
38
44
  homepage_uri: https://www.github.com/notmarkmiranda/mlb_rb
39
45
  source_code_uri: https://www.github.com/notmarkmiranda/mlb_rb
40
46
  changelog_uri: https://www.github.com/notmarkmiranda/mlb_rb/blob/main/CHANGELOG.md
41
- post_install_message:
47
+ post_install_message:
42
48
  rdoc_options: []
43
49
  require_paths:
44
50
  - lib
@@ -53,8 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
59
  - !ruby/object:Gem::Version
54
60
  version: '0'
55
61
  requirements: []
56
- rubygems_version: 3.2.3
57
- signing_key:
62
+ rubygems_version: 3.2.32
63
+ signing_key:
58
64
  specification_version: 4
59
65
  summary: MLB API Wrapper for Ruby
60
66
  test_files: []