Skip to content

Commit f7edcbc

Browse files
authored
Prepend https:// to url in use_course() (#219)
* Prepend https:// to url in use_course() * Tweak docs
1 parent 7c25468 commit f7edcbc

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

R/course.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
#' Special-purpose function to download a folder of course materials. The only
66
#' demand on the user is to confirm or specify where the new folder should be
77
#' stored. Workflow:
8-
#' * User executes something like: `use_course("http://bit.ly/xxx-yyy-zzz")`.
8+
#' * User executes something like: `use_course("bit.ly/xxx-yyy-zzz")`.
99
#' * User is asked to notice and confirm the location of the new folder. Specify
1010
#' `destdir` to skip this.
1111
#' * User is asked if they'd like to delete the ZIP file.
1212
#' * New folder is opened in the file manager, e.g. Finder or File Explorer.
1313
#'
14+
#' If `url` has no "http" prefix, "https://" is prepended, allowing for even
15+
#' less typing by the user. Most URL shorteners give HTTPS links and,
16+
#' anecdotally, we note this appears to work with [bit.ly](https://bitly.com/)
17+
#' links, even though they are nominally HTTP.
18+
#'
1419
#' @param url Link to a ZIP file containing the materials, possibly behind a
1520
#' shortlink. Function developed with DropBox and GitHub in mind, but should
16-
#' work for ZIP files generally. See [use_course_details] for more.
21+
#' work for ZIP files generally. If no "http" prefix is found, "https://" is
22+
#' prepended. See [use_course_details] for more.
1723
#' @param destdir The new folder is stored here. Defaults to user's Desktop.
1824
#'
1925
#' @return Path to the new directory holding the course materials, invisibly.
@@ -34,6 +40,7 @@
3440
#' use_course("https://api.github.com/repos/r-lib/rematch2/zipball/master")
3541
#' }
3642
use_course <- function(url, destdir = NULL) {
43+
url <- normalize_url(url)
3744
zipfile <- download_zip(
3845
url,
3946
destdir = destdir %||% conspicuous_place(),
@@ -60,7 +67,7 @@ use_course <- function(url, destdir = NULL) {
6067
#'
6168
#' ## as called inside use_course()
6269
#' download_zip(
63-
#' url,
70+
#' url, ## after post-processing with normalize_url()
6471
#' ## conspicuous_place() = Desktop or home directory or working directory
6572
#' destdir = destdir \\%||\\% conspicuous_place(),
6673
#' pedantic = is.null(destdir) && interactive()
@@ -241,6 +248,12 @@ tidy_unzip <- function(zipfile) {
241248
invisible(target)
242249
}
243250

251+
normalize_url <- function(url) {
252+
stopifnot(is.character(url))
253+
has_scheme <- grepl("^http[s]?://", url)
254+
ifelse(has_scheme, url, paste0("https://", url))
255+
}
256+
244257
conspicuous_place <- function() {
245258
Filter(dir.exists, c(
246259
file.path(Sys.getenv("HOME"), "Desktop"), # typical macOS = ~/Desktop

man/use_course.Rd

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/use_course_details.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/manual/manual-use-course.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ hadley <- use_course(
116116
)
117117

118118
list.files(hadley, all.files = TRUE, recursive = TRUE)
119+
120+
rematch2 <- use_course("github.com/r-lib/rematch2/archive/master.zip")
121+
use_course("rstd.io/usethis-src")
122+
use_course("bit.ly/uusseetthhiiss")

tests/testthat/test-use-course.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
context("use_course")
22

3+
test_that("normalize_url() prepends https:// (or not)", {
4+
expect_error(normalize_url(1), "is\\.character.*not TRUE")
5+
expect_identical(normalize_url("http://bit.ly/aaa"), "http://bit.ly/aaa")
6+
expect_identical(normalize_url("bit.ly/aaa"), "https://bit.ly/aaa")
7+
expect_identical(
8+
normalize_url("https://github.com/r-lib/rematch2/archive/master.zip"),
9+
"https://github.com/r-lib/rematch2/archive/master.zip"
10+
)
11+
expect_identical(
12+
normalize_url("https://rstd.io/usethis-src"),
13+
"https://rstd.io/usethis-src"
14+
)
15+
expect_identical(
16+
normalize_url("rstd.io/usethis-src"),
17+
"https://rstd.io/usethis-src"
18+
)
19+
})
20+
321
test_that("conspicuous_place() returns a writeable directory", {
422
expect_error_free(x <- conspicuous_place())
523
expect_true(is_dir(x))

0 commit comments

Comments
 (0)