Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Last active September 9, 2022 02:41
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/4758157 to your computer and use it in GitHub Desktop.
Save ramnathv/4758157 to your computer and use it in GitHub Desktop.
Upload Directory of Files using Gist API

This is a short set of functions that use the httr package to upload a directory of files as a gist. The post_gist function uploads an anonymous gist, which can only be deleted within a short time of being uploaded. So be cautious in what you upload using this function.

#" Function that takes a list of files and creates payload for API
#'
#' @param filenames names of files to post
#' @param description brief description of gist (optional)
#' @param public whether gist is public (defaults to TRUE)
create_gist <- function(filenames, description = "", public = TRUE){
files = lapply(filenames, function(file){
x = list(content = paste(readLines(file, warn = F), collapse = '\n'))
})
names(files) = filenames
body = list(description = description, public = public, files = files)
RJSONIO::toJSON(body)
}
#' Function that posts a directory of files as a gist
#'
#' @param gdir directory of files to post
#' @param description brief description of gist
#' @param public whether gist is public (defaults to TRUE)
post_gist <- function(gdir, description = "", public = TRUE){
g_url = "https://api.github.com/gists"
gist = create_gist(dir(gdir), description, public)
posted = httr::POST(g_url, body = gist)
browseURL(content(posted)$html_url)
}
@skishchampi
Copy link

helpful little script. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment