R/sample.R

Defines functions scrape_text get_request_sample get_response_sample

Documented in get_request_sample get_response_sample scrape_text

#' Get sample request/response
#'
#' For endpoints that use request/response, use this function to get
#' json-formatted text.
#'
#' @inheritParams validate_endpoint
#'
#' @return character, json format
#' @seealso json_viewer
#' @export
#'
get_response_sample <- function(endpoint){

  endpoint %>%
    validate_endpoint() %>%
    `[[`("HelpLocation") %>%
    paste("score", sep = "/") %>%
    scrape_text("#responseSummary pre")
}

#' @rdname get_response_sample
#' @export
#'
get_request_sample <- function(endpoint){

  endpoint %>%
    validate_endpoint() %>%
    `[[`("HelpLocation") %>%
    paste("score", sep = "/") %>%
    scrape_text("#requestSummary pre")
}

#' Scrapes text from node
#'
#' @keywords internal
#'
#' @param url    character, URL to scrape
#' @param css    character, css selector to use to identify node
#'
#' @return character
#'
#' @export
#'
scrape_text <- function(url, css){

  result <-
    xml2::read_html(url) %>%
    rvest::html_node(css) %>%
    rvest::html_text()

  result
}
ijlyttle/AzureMLPlus documentation built on May 17, 2017, 3:13 a.m.