R/util.R

Defines functions drop_cruft is.url http_check_status bin_name_check bin_name_random

Documented in bin_name_random

#' Create random bin name for Filebin
#'
#' @param length Number of characters in bin name. Must be 8 or more.
#' @param pattern Character set.
#'
#' @return A character vector.
#' @export
#'
#' @examples
#' bin_name_random(8)
bin_name_random <- function(length = 16, pattern = "[a-z0-9]") {
  stri_rand_strings(1, length, pattern)
}

bin_name_check <- function(bin) {
  if (nchar(bin) < 8) stop("Bin name too short.")
}

http_check_status <- function(response) {                       # nocov start
  if (http_error(response)) {
    stop(http_status(response)$reason, call. = FALSE)
  }
}                                                               # nocov end

is.url <- function(x) {
  grepl("https?://", x, ignore.case = TRUE)
}

drop_cruft <- function(.data) {
  .data %>%
    select(
      -starts_with("bin_"),
      -ends_with("_at_relative"),
      -bytes_readable
    ) %>%
    clean_names() %>%
    rename_at(vars(ends_with("_at")), ~ sub("_at$", "", .))
}

drop_null <- function(x) {
  if (length(x)) {
    x[!sapply(x, is.null)]
  } else {
    x
  }
}

stop_response <- function(response) {
  stop(paste0(sub("\n$", "", content(response)), "."), call. = FALSE)
}

Try the filebin package in your browser

Any scripts or data that you put into this service are public.

filebin documentation built on Sept. 2, 2023, 9:07 a.m.