Skip to content

Commit

Permalink
Give flywire_expandurl a follow argument
Browse files Browse the repository at this point in the history
* and document and test it
  • Loading branch information
jefferis committed Aug 4, 2024
1 parent a789cc7 commit aa49da8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ Remotes:
Encoding: UTF-8
Language: en-GB
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
12 changes: 9 additions & 3 deletions R/flywire-urls.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ flywire_shortenurl <- function(x, include_base=TRUE, baseurl=NULL, cache=TRUE, .
#' \code{flywire_expandurl} will also expand tinyurl.com URLs as well as those
#' referencing a json fragment on a google cloud bucket (such as the flyem
#' link shortener). If a tinyurl.com URL maps to a short URL referencing a
#' json fragment, then they will successively be expanded.
#' json fragment, then they will successively be expanded unless
#' \code{follow=FALSE}.
#'
#' Finally, if the URL is actually already expanded, then this will be
#' returned unmodified. This is a change in behaviour as of May 2024
#' (previously an error was thrown).
#'
#' @param json.only Only return the JSON fragment rather than the neuroglancer
#' URL
#' URL. Defaults to \code{FALSE}.
#' @param follow Whether to follow short URLs that specify other short URLs (see
#' details). Defaults to \code{TRUE}.
#' @export
#'
#' @examples
Expand All @@ -109,7 +113,7 @@ flywire_shortenurl <- function(x, include_base=TRUE, baseurl=NULL, cache=TRUE, .
#' flywire_expandurl("https://tinyurl.com/flywirehb2")
#' }
#' @rdname flywire_shortenurl
flywire_expandurl <- function(x, json.only=FALSE, cache=TRUE, ...) {
flywire_expandurl <- function(x, json.only=FALSE, cache=TRUE, follow=TRUE, ...) {
checkmate::assert_character(x, pattern="^http[s]{0,1}://")
if(length(x)>1) {
res=pbapply::pbsapply(x, flywire_expandurl, json.only=json.only, cache=cache, ...)
Expand All @@ -123,6 +127,8 @@ flywire_expandurl <- function(x, json.only=FALSE, cache=TRUE, ...) {
if(grepl("comsync.lijit.com", url, fixed = T))
url=httr::GET(url, config(followlocation=TRUE))$url
x=url
if(!follow)
return(x)
}

if(isFALSE(su <- shorturl(url))) {
Expand Down
10 changes: 7 additions & 3 deletions man/flywire_shortenurl.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-urls.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ test_that("we can work round toJSON array issue",{
hostname(fu))
})

test_that('we can expand neuroglancer URLs without following', {
expect_equal(
flywire_expandurl('https://tinyurl.com/NeckConnective', follow = FALSE),
"https://neuroglancer-demo.appspot.com/#!gs://flyem-user-links/short/NeckConnective.json"
)
})

test_that('we can make a neuroglancer URL', {
catmaid_url <- "https://fafb.catmaid.virtualflybrain.org/?pid=2&zp=131280&yp=170014.98879622458&xp=426584.81386896875&tool=navigator&sid0=2&s0=-1"
Expand Down

0 comments on commit aa49da8

Please # to comment.