Skip to content

Commit

Permalink
fixes #72 - updating squiggle api
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyday12 committed Jul 2, 2019
1 parent 5d866f0 commit 0663601
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fitzRoy
Title: Easily Scrape and Process AFL Data
Version: 0.1.12
Version: 0.1.13.9000
Authors@R:
c(person(given = "James",
family = "Day",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# fitzRoy 0.1.13
* Updated Squiggle API to accept new parameters `ladder`, `standings` and `complete`. ([#73](https://github.com/jimmyday12/fitzRoy/issues/73))

# fitzRoy 0.1.12
* Fixed an error with player ID's for 2019 season where new data was breaking an internal function ([#67](https://github.com/jimmyday12/fitzRoy/issues/67))
* Fixed an error where Geelong v Melbourne game wasn't getting parsed properly ([#68](https://github.com/jimmyday12/fitzRoy/issues/68))
Expand Down
2 changes: 1 addition & 1 deletion R/footywire-calcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ get_fixture <- function(season = lubridate::year(Sys.Date())) {
"The data for {season} season seems to be empty.
Check the following url on footywire
{url_fixture}"))
return(games_df <- tiblle())
return(games_df <- dplyr::tibble())
}

# Put this into dataframe format
Expand Down
48 changes: 35 additions & 13 deletions R/squiggle.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#'
#' For full instructions, see [api.squiggle.com.au](api.squiggle.com.au)
#'
#' @param query A text string. The main query to use with the API. to one of `sources`, `games` or `tips`.
#' @param query A text string. The main query to use with the API. Must be one of `sources`, `games`, `tips`, `ladder` or `standings`
#'
#' @param ... (optional) An optional argument provided to the [Squiggle API](api.squiggle.com.au). See details for more info.
#'
Expand All @@ -30,14 +30,14 @@
#'
#' # Get tips from Squiggle
#' squiggle <- get_squiggle_data(query = "tips", source = 1)
get_squiggle_data <- function(query = c("sources", "games", "tips"), ...) {
get_squiggle_data <- function(query = c("sources", "games", "tips", "ladder", "standings"), ...) {

# Ensure query is valid
query <- match.arg(query)

# Get optional expressions and check that they are valid
exp <- rlang::enexprs(...)
valid <- c("year", "round", "game", "source")
valid <- c("year", "round", "game", "source", "complete")

if (!all(names(exp) %in% valid)) {
rlang::abort(paste0(
Expand All @@ -62,17 +62,39 @@ get_squiggle_data <- function(query = c("sources", "games", "tips"), ...) {
}

message(paste("Getting data from", url))

# set user agent
ua <- httr::user_agent("https://github.com/jimmyday12/fitzRoy/")

dat <- tryCatch(
jsonlite::fromJSON(url),
error = function(e) rlang::abort(paste(
"The URL did not work",
"Did your query make sense?\n",
"Try the following URL in your",
"browser:",
url
))
)
resp <- httr::GET(url, ua)
httr::warn_for_status(resp)

if (httr::status_code(resp) != 200) {
rlang::abort(
glue::glue(
"The URL responded with the following status:
{httr::http_status(resp)$message}
Does your query make sense? Try the following URL in your browser
{resp$url}"))
}

if (httr::http_type(resp) != "application/json") {
stop("API did not return json", call. = FALSE)
}

dat <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = TRUE)

#dat <- tryCatch(
# jsonlite::fromJSON(url),
# error = function(e) rlang::abort(paste(
# "The URL did not work",
# "Did your query make sense?\n",
# "Try the following URL in your",
# "browser:",
# url
# ))
#)

# Convert the
df <- as.data.frame(dat[[1]])
Expand Down
5 changes: 3 additions & 2 deletions man/get_squiggle_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/player_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-squiggle-api-works.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ test_that("Squiggle API optional arguments work", {
get_squiggle_data(query = "games", round = 1, year = 2018),
"list"
)
expect_type(get_squiggle_data(query = "games", year = 2018, complete = 100), "list")
expect_type(get_squiggle_data(query = "ladder", year = 2019, round = 9), "list")
#expect_type(get_squiggle_data(query = "games", year = 2018, round = 22, complete = !100), "list")
expect_type(get_squiggle_data(query = "sources", source = 1), "list")
expect_error(get_squiggle_data(query = "sources", round = 1, year = 2018))
expect_error(get_squiggle_data(query = "tips", x = "", year = 2018))

})

0 comments on commit 0663601

Please sign in to comment.