R/utils.R

#' Visualizar resposta
#' 
#' Visualizar resposta de request no RStudio
#' 
#' @param r response object returned by \code{\link[httr]{GET}} or 
#' \code{\link[httr]{POST}}
#' 
#' @examples
#' \dontrun{
#' r <- httr::GET('https://google.com')
#' visualize(r)
#' }
#' @export
visualize <- function(r) {
  ct <- r[["headers"]][["content-type"]]
  if (!grepl("html", ct)) {
    stop("Not a HTML file.")
  }
  if (interactive()) {
    dir <- tempfile()
    dir.create(dir)
    html_file <- file.path(dir, "index.html")
    h <- httr::content(r, "text")
    # enc <- stringi::stri_enc_detect(h)[[1]]$Encoding[1]
    h <- iconv(h, from = "UTF-8", to = "ISO-8859-1")
    cat(h, file = html_file)
    rstudioapi::viewer(html_file)
  }
  invisible()
}

#' Pipe operator
#'
#' See \code{\link[magrittr]{\%>\%}} for more details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
jtrecenti/sabesp documentation built on May 20, 2019, 3:17 a.m.