Skip to content

Commit

Permalink
Merge pull request #3 from jasonhancock/venues
Browse files Browse the repository at this point in the history
Adds support for retrieving venue information
  • Loading branch information
stevepartridge authored Jan 10, 2023
2 parents cc329d0 + e84f5c2 commit 1103370
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ for i := range teams {
start, _ := time.Parse("2006/01/02", "2017/05/17")
end := start.AddDate(0, 1, 0) // one month

games, err := mlbApi.GetGamesForRange(start, end)
games, err := mlbApi.GetGamesByDateRange(start, end)
if err != nil {
fmt.Println("well, dang", err.Error())
}
Expand Down
3 changes: 3 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
// schedule?lang=en&sportId=1&season=2018&startDate=2018-08-01&endDate=2018-08-31&teamId=119&eventTypes=primary&scheduleTypes=games

func (m *Mlb) Call(endpoint string, query map[string]string) (Response, error) {
if query == nil {
query = make(map[string]string)
}

result := Response{}

Expand Down
1 change: 1 addition & 0 deletions api_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Response struct {
TotalGamesInProgress int `json:"totalGamesInProgress,omitempty"`
Dates []Date `json:"dates,omitempty"`
Teams []Team `json:"teams,omitempty"`
Venues []Venue `json:"venues,omitempty"`
Divisions []Division `json:"divisions,omitempty"`
}

Expand Down
90 changes: 39 additions & 51 deletions divisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,45 @@ import (
)

const (
AmericanLeagueEast = 201
AmericanLeagueCentral = 202
AmericanLeagueWest = 200
NationalLeagueEast = 204
NationalLeagueCentral = 205
NationalLeagueWest = 203
InternationalLeagueNorth = 219
InternationalLeagueSouth = 220
InternationalLeagueWest = 221
PacificCoastLeagueAmericanNorthern = 232
PacificCoastLeagueAmericanSouthern = 234
PacificCoastLeaguePacificNorthern = 231
PacificCoastLeaguePacificSouthern = 233
MexicanLeagueNorte = 222
EasternLeagueWestern = 213
SouthernLeagueNorth = 239
SouthernLeagueSouth = 240
TexasLeagueNorth = 241
TexasLeagueSouth = 242
CaliforniaLeagueNorth = 208
CaliforniaLeagueSouth = 209
CarolinaLeagueNorthern = 210
CarolinaLeagueSouthern = 211
FloridaStateLeagueNorth = 214
FloridaStateLeagueSouth = 215
MidwestLeagueEastern = 224
MidwestLeagueWestern = 225
SouthAtlanticLeagueNorthern = 237
SouthAtlanticLeagueSouthern = 238
NewYorkPennLeagueMcNamara = 228
NewYorkPennLeaguePinckney = 229
NewYorkPennLeagueStedler = 230
NorthwestLeagueNorth = 226
NorthwestLeagueSouth = 227
PioneerLeagueNorth = 235
PioneerLeagueSouth = 236
AppalachianLeagueEast = 206
AppalachianLeagueWest = 207
ArizonaLeagueEast = 560
ArizonaLeagueCentral = 570
ArizonaLeagueWest = 561
GulfCoastLeagueEast = 216
GulfCoastLeagueNortheast = 217
GulfCoastLeagueNorthwest = 615
GulfCoastLeagueSouth = 218
DominicanSummerLeagueNorth = 247
DominicanSummerLeagueSouth = 248
DominicanSummerLeagueNorthwest = 249
DominicanSummerLeagueBaseballCity = 250
DominicanSummerLeagueSanPedro = 246
DominicanSummerLeagueNortheast = 401
AmericanLeagueEast = 201
AmericanLeagueCentral = 202
AmericanLeagueWest = 200
NationalLeagueEast = 204
NationalLeagueCentral = 205
NationalLeagueWest = 203
InternationalLeagueEast = 219
InternationalLeagueWest = 221
PacificCoastLeagueWest = 231
PacificCoastLeagueEast = 233
MexicanLeagueNorte = 222
EasternLeagueSouthwest = 213
SouthernLeagueNorth = 239
SouthernLeagueSouth = 240
TexasLeagueNorth = 241
TexasLeagueSouth = 242
CaliforniaLeagueNorth = 208
CaliforniaLeagueSouth = 209
CarolinaLeagueNorth = 210
CarolinaLeagueSouth = 211
FloridaStateLeagueEast = 214
FloridaStateLeagueWest = 215
MidwestLeagueEastern = 224
MidwestLeagueWestern = 225
SouthAtlanticLeagueNorth = 237
SouthAtlanticLeagueSouth = 238
ArizonaLeagueEast = 560
ArizonaLeagueCentral = 570
ArizonaLeagueWest = 561
GulfCoastLeagueEast = 216
GulfCoastLeagueNortheast = 217
GulfCoastLeagueNorthwest = 615
GulfCoastLeagueSouth = 218
DominicanSummerLeagueNorth = 247
DominicanSummerLeagueSouth = 248
DominicanSummerLeagueNorthwest = 249
DominicanSummerLeagueBaseballCity = 250
DominicanSummerLeagueSanPedro = 246
DominicanSummerLeagueNortheast = 401
)

// GetDivisions returns all or specific divisions based on if any divisionIds are provided or not
Expand Down
109 changes: 46 additions & 63 deletions divisions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,52 +64,35 @@ func Test_GetSpecificDivisions_Success(t *testing.T) {

func Test_GetEachDivision_Success(t *testing.T) {
testDivisions := map[int]string{
mlb.AmericanLeagueEast: "American League East",
mlb.AmericanLeagueCentral: "American League Central",
mlb.AmericanLeagueWest: "American League West",
mlb.NationalLeagueEast: "National League East",
mlb.NationalLeagueCentral: "National League Central",
mlb.NationalLeagueWest: "National League West",
mlb.InternationalLeagueNorth: "International League North",
mlb.InternationalLeagueSouth: "International League South",
mlb.InternationalLeagueWest: "International League West",
mlb.PacificCoastLeagueAmericanNorthern: "Pacific Coast League American Northern",
mlb.PacificCoastLeagueAmericanSouthern: "Pacific Coast League American Southern",
mlb.PacificCoastLeaguePacificNorthern: "Pacific Coast League Pacific Northern",
mlb.PacificCoastLeaguePacificSouthern: "Pacific Coast League Pacific Southern",
mlb.MexicanLeagueNorte: "Mexican League Norte",
mlb.EasternLeagueWestern: "Eastern League Western",
mlb.SouthernLeagueNorth: "Southern League North",
mlb.SouthernLeagueSouth: "Southern League South",
mlb.TexasLeagueNorth: "Texas League North",
mlb.TexasLeagueSouth: "Texas League South",
mlb.CaliforniaLeagueNorth: "California League North",
mlb.CaliforniaLeagueSouth: "California League South",
mlb.CarolinaLeagueNorthern: "Carolina League Northern",
mlb.CarolinaLeagueSouthern: "Carolina League Southern",
mlb.FloridaStateLeagueNorth: "Florida State League North",
mlb.FloridaStateLeagueSouth: "Florida State League South",
mlb.MidwestLeagueEastern: "Midwest League Eastern",
mlb.MidwestLeagueWestern: "Midwest League Western",
mlb.SouthAtlanticLeagueNorthern: "South Atlantic League Northern",
mlb.SouthAtlanticLeagueSouthern: "South Atlantic League Southern",
mlb.NewYorkPennLeagueMcNamara: "New York-Penn League McNamara",
mlb.NewYorkPennLeaguePinckney: "New York-Penn League Pinckney",
mlb.NewYorkPennLeagueStedler: "New York-Penn League Stedler",
mlb.NorthwestLeagueNorth: "Northwest League North",
mlb.NorthwestLeagueSouth: "Northwest League South",
mlb.PioneerLeagueNorth: "Pioneer League North",
mlb.PioneerLeagueSouth: "Pioneer League South",
mlb.AppalachianLeagueEast: "Appalachian League East",
mlb.AppalachianLeagueWest: "Appalachian League West",
mlb.ArizonaLeagueEast: "Arizona League East",
mlb.ArizonaLeagueCentral: "Arizona League Central",
mlb.ArizonaLeagueWest: "Arizona League West",
// mlb.GulfCoastLeagueEast: "Gulf Coast League East",
// mlb.GulfCoastLeagueNorth: "Gulf Coast League North",
// mlb.GulfCoastLeagueNortheast: "Gulf Coast League Northeast",
// mlb.GulfCoastLeagueNorthwest: "Gulf Coast League Northwest",
// mlb.GulfCoastLeagueSouth: "Gulf Coast League South",
mlb.AmericanLeagueEast: "American League East",
mlb.AmericanLeagueCentral: "American League Central",
mlb.AmericanLeagueWest: "American League West",
mlb.NationalLeagueEast: "National League East",
mlb.NationalLeagueCentral: "National League Central",
mlb.NationalLeagueWest: "National League West",
mlb.InternationalLeagueEast: "International League East",
mlb.InternationalLeagueWest: "International League West",
mlb.PacificCoastLeagueWest: "Pacific Coast League West",
mlb.PacificCoastLeagueEast: "Pacific Coast League East",
mlb.MexicanLeagueNorte: "Mexican League Norte",
mlb.EasternLeagueSouthwest: "Eastern League Southwest",
mlb.SouthernLeagueNorth: "Southern League North",
mlb.SouthernLeagueSouth: "Southern League South",
mlb.TexasLeagueNorth: "Texas League North",
mlb.TexasLeagueSouth: "Texas League South",
mlb.CaliforniaLeagueNorth: "California League North",
mlb.CaliforniaLeagueSouth: "California League South",
mlb.CarolinaLeagueNorth: "Carolina League North",
mlb.CarolinaLeagueSouth: "Carolina League South",
mlb.FloridaStateLeagueEast: "Florida State League East",
mlb.FloridaStateLeagueWest: "Florida State League West",
mlb.MidwestLeagueEastern: "Midwest League East",
mlb.MidwestLeagueWestern: "Midwest League West",
mlb.SouthAtlanticLeagueNorth: "South Atlantic League North",
mlb.SouthAtlanticLeagueSouth: "South Atlantic League South",
mlb.ArizonaLeagueEast: "Arizona Complex League East",
mlb.ArizonaLeagueCentral: "Arizona Complex League Central",
mlb.ArizonaLeagueWest: "Arizona Complex League West",
mlb.DominicanSummerLeagueNorth: "Dominican Summer League North",
mlb.DominicanSummerLeagueSouth: "Dominican Summer League South",
mlb.DominicanSummerLeagueNorthwest: "Dominican Summer League Northwest",
Expand All @@ -119,22 +102,22 @@ func Test_GetEachDivision_Success(t *testing.T) {
}

for id, division := range testDivisions {

divisions, err := mlbApi.GetDivisions(id)
if err != nil {
t.Error("Should not error when retrieving divisions")
}

if len(divisions) != 1 {
t.Error("Should be exactly 1 division found.")
}

if divisions[0].Name != division {
t.Errorf("Division found (%s) does not match expected division (%s)",
divisions[0].Name,
division,
)
}
t.Run(division, func(t *testing.T) {
divisions, err := mlbApi.GetDivisions(id)
if err != nil {
t.Fatal("Should not error when retrieving divisions")
}

if len(divisions) != 1 {
t.Fatal("Should be exactly 1 division found.")
}

if divisions[0].Name != division {
t.Errorf("Division found (%s) does not match expected division (%s)",
divisions[0].Name,
division,
)
}
})
}

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/stevepartridge/mlb

go 1.13
3 changes: 2 additions & 1 deletion teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const (
RedSox = 111 // Boston Red Sox
Cubs = 112 // Chicago Cubs
Reds = 113 // Cincinnati Reds
Indians = 114 // Cleveland Indians
Indians = 114 // Cleveland Indians - renamed to Guardians
Guardians = 114 // Cleveland Guardians - formerly the Indians
Rockies = 115 // Colorado Rockies
Tigers = 116 // Detroit Tigers
Astros = 117 // Houston Astros
Expand Down
2 changes: 1 addition & 1 deletion teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_GetEachTeam_Success(t *testing.T) {
mlb.RedSox: "Boston Red Sox",
mlb.Cubs: "Chicago Cubs",
mlb.Reds: "Cincinnati Reds",
mlb.Indians: "Cleveland Indians",
mlb.Indians: "Cleveland Guardians",
mlb.Rockies: "Colorado Rockies",
mlb.Tigers: "Detroit Tigers",
mlb.Astros: "Houston Astros",
Expand Down
1 change: 0 additions & 1 deletion utils.go

This file was deleted.

30 changes: 30 additions & 0 deletions venues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mlb

import (
"errors"
"fmt"
)

func (m *Mlb) GetVenues() ([]Venue, error) {
resp, err := m.Call("/venues", nil)
if err != nil {
return []Venue{}, err
}

return resp.Venues, nil
}

func (m *Mlb) GetVenue(venueID int) (Venue, error) {
resp, err := m.Call(fmt.Sprintf("/venues/%d", venueID), nil)
if err != nil {
return Venue{}, err
}

for _, v := range resp.Venues {
if v.Id == venueID {
return v, nil
}
}

return Venue{}, errors.New("not found")
}
35 changes: 35 additions & 0 deletions venues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mlb_test

import "testing"

func Test_GetVenues_Success(t *testing.T) {

venues, err := mlbApi.GetVenues()
if err != nil {
t.Error("Should not error when retrieving venues")
}

if len(venues) == 0 {
t.Error("Should be more than zero")
}

}

func Test_GetVenue_Success(t *testing.T) {
venue, err := mlbApi.GetVenue(10) // "Oakland Coliseum"
if err != nil {
t.Error("Should not error when retrieving venue")
}

if venue.Name != "Oakland Coliseum" {
t.Errorf("Expected `Oakland Coliseum`, got %q", venue.Name)

}
}

func Test_GetVenue_Failure_InvalidVenueID(t *testing.T) {
_, err := mlbApi.GetVenue(8675309)
if err == nil {
t.Error("Should be error when retrieving invalid venue")
}
}

0 comments on commit 1103370

Please sign in to comment.