Skip to content

Commit

Permalink
Include game related links in API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
peruukki committed Apr 24, 2024
1 parent 496b490 commit ae8d045
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The `games` array contains details of the games, each game item containing these
- `gameStats` *(object)*
- `preGameStats` *(object)*
- `currentStats` *(object)*
- `links` *(object)*
- `errors` *(array)* (only present if data validity errors were detected)

The fields are described in more detail in [Response fields](#response-fields).
Expand Down Expand Up @@ -67,6 +68,7 @@ The `games` array contains details of the games, each game item containing these
- `gameStats` *(object)*
- `preGameStats` *(object)*
- `currentStats` *(object)*
- `links` *(object)*
- `errors` *(array)* (only present if data validity errors were detected)

**If a date has no scheduled games**, you will either get:
Expand Down Expand Up @@ -256,6 +258,10 @@ The fields are described in more detail in [Response fields](#response-fields).
"pointsFromPlayoffSpot": "-4"
}
}
},
"links": {
"gameCenter": "https://www.nhl.com/gamecenter/bos-vs-chi/2023/10/24/2023020092",
"videoRecap": "https://www.nhl.com/video/recap-bruins-at-blackhawks-10-24-23-6339814966112"
}
},
{
Expand Down Expand Up @@ -427,6 +433,9 @@ The fields are described in more detail in [Response fields](#response-fields).
"pointsFromPlayoffSpot": "0"
}
}
},
"links": {
"gameCenter": "https://www.nhl.com/gamecenter/ott-vs-det/2023/12/09/2023020412"
}
}
]
Expand Down Expand Up @@ -509,7 +518,8 @@ The fields are described in more detail in [Response fields](#response-fields).
"PIT": 1
}
}
}
},
"links": {}
}
]
}
Expand Down Expand Up @@ -632,6 +642,10 @@ The fields are described in more detail in [Response fields](#response-fields).
- `playoffSeries` object: current playoff series related information (only present in playoff games), with the fields:
- `round` *(number)*: the game’s playoff round; `0` for the Stanley Cup Qualifiers best-of-5 series (in 2020 due to COVID-19), actual playoffs start from `1`
- `wins` *(object)*: each team’s win count in the series
- `links` object: links to related pages on the official NHL site, with the optional fields:
- `gameCenter`: game summary with lots of related info
- `playoffSeries`: playoff series specific info (only present in playoff games)
- `videoRecap`: 5-minute video recap (once available)
- `errors` array: list of data validation errors, only present if any were detected. Sometimes the NHL Stats API temporarily contains
invalid or missing data. Currently we check if the goal data from the NHL Stats API (read from its `scoringPlays` field) contains the
same number of goals than the score data (read from its `teams` field). If it doesn't, two different errors can be reported:
Expand Down
13 changes: 12 additions & 1 deletion src/nhl_score_api/fetchers/nhl_api_web/game_scores.clj
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@
:else
nil)))

(defn- add-link-if-exists [map key link]
(cond-> map
link (assoc key (str "https://www.nhl.com" link))))

(defn- parse-links [schedule-game]
(-> {}
(add-link-if-exists :game-center (:game-center-link schedule-game))
(add-link-if-exists :playoff-series (:series-url schedule-game))
(add-link-if-exists :video-recap (:three-min-recap schedule-game))))

(defn- add-validation-errors [game-details]
(let [errors (keep identity [(validate-score-and-goal-counts game-details)])]
(if (empty? errors)
Expand All @@ -471,6 +481,7 @@
(-> {:status (parse-game-status schedule-game landing)
:start-time (parse-game-start-time schedule-game)
:goals (parse-goals landing)
:links (parse-links schedule-game)
:scores scores
:teams teams
pre-game-stats-key {}
Expand All @@ -481,7 +492,7 @@
(add-team-standings team-details current-and-pre-game-standings)
(add-playoff-series-information schedule-game)
(add-validation-errors)
(reject-empty-vals-except-for-keys #{:goals}))))
(reject-empty-vals-except-for-keys #{:goals :links}))))

(defn parse-game-scores
([date-and-schedule-games current-and-pre-game-standings]
Expand Down
19 changes: 19 additions & 0 deletions test/nhl_score_api/fetchers/nhl_api_web/game_scores_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,25 @@
(is (= [1 1 1 1]
current-stats-rounds) "Parsed current stats playoff rounds"))))

(deftest game-scores-links

(testing "Including empty links when none available"
(let [game (first (:games
(parse-game-scores
(get-latest-games resources/games-for-validation-testing)
default-standings)))]
(is (= {} (:links game)) "Empty links")))

(testing "Including all links when available"
(let [game (first (:games
(parse-game-scores
(get-latest-games resources/playoff-games-finished-with-2nd-games)
(:standings resources/standings-for-playoffs))))]
(is (= {:game-center "https://www.nhl.com/gamecenter/nyi-vs-car/2023/04/19/2022030132"
:playoff-series "https://www.nhl.com/schedule/playoff-series/2023/series-c/hurricanes-vs-islanders"
:video-recap "https://www.nhl.com/video/recap-car-4-nyi-3-f-ot-343701784"}
(:links game)) "All links included"))))

(deftest game-scores-validation

(testing "Validating valid game with goals"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
"cs": "Kuzněcov",
"sk": "Kuznecov"
}
},
"threeMinRecap": "/video/recap-capitals-at-devils-11-10-23-6340912570112",
"gameCenterLink": "/gamecenter/wsh-vs-njd/2023/11/10/2023020206"
}
},
{
"id": 2023020208,
Expand Down

0 comments on commit ae8d045

Please sign in to comment.