Btibert3
2/11/2013 - 10:20 PM

Upload Directory of Files using Gist API

Upload Directory of Files using Gist API

#" 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)
}

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.