Skip to content

Commit

Permalink
Fixed get_current_season with new no-internet recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
pbulsink committed Mar 6, 2024
1 parent 3453c36 commit 669aa2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
26 changes: 12 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,18 @@ get_ergast_content <- function(url) {
#' @export
#' @return Year (four digit number) representation of current season, as numeric.
get_current_season <- function() {
current_season <- ifelse(as.numeric(strftime(Sys.Date(), "%m")) < 3,
as.numeric(strftime(Sys.Date(), "%Y")) - 1,
as.numeric(strftime(Sys.Date(), "%Y"))
)
tryCatch(
{
url <- "current.json?limit=30"
data <- get_ergast_content(url)
current_season <- as.numeric(data$MRData$RaceTable$season)
},
error = function(e) {
cli::cli_inform("Falling back to manually determined 'current' season")
}
)
url <- "current.json?limit=30"
data <- get_ergast_content(url)

if(is.null(data)){
cli::cli_inform("Falling back to manually determined 'current' season")
current_season <- ifelse(as.numeric(strftime(Sys.Date(), "%m")) < 3,
as.numeric(strftime(Sys.Date(), "%Y")) - 1,
as.numeric(strftime(Sys.Date(), "%Y"))
)
} else {
current_season <- as.numeric(data$MRData$RaceTable$season)
}
return(current_season)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("Utility Functions work without internet", {
if (requireNamespace("httptest2", quietly = TRUE)) {
# Test internet failures for get_current_season
httptest2::without_internet({
expect_message(get_current_season(), "f1dataR: Error getting current season from Ergast")
expect_message(get_current_season(), "f1dataR: Error getting data from Ergast")
expect_gte(get_current_season(), 2022)
})
}
Expand Down

0 comments on commit 669aa2a

Please sign in to comment.