R/get_indexes_helper.R

#' Get Indexes Values and Associated HTML Names
#'
#' This simple helper function extracts the names and HTML values (options values) of all
#' available indexes in Casablanca Stock Exchange.
#'
#' @param request The object returned by an HTTP GET request. \code{NULL} by default.
#' @param with_description Boolean vector of length one indicating if the description
#' (HTML names) should be returned or not. \code{TRUE} by default.
#'
#' @return A character vector containing all available indexes. If \code{with_description}
#' is true, a data frame with INDEX and DESCRIPTION columns is returned.
#'
#' @importFrom magrittr %>%
#' @importFrom rvest html_nodes
#' @importFrom rvest html_attr
#' @importFrom rvest html_text
#' @importFrom xml2 read_html
#' @importFrom httr GET
#' @importFrom httr add_headers
#' @importFrom stringr str_trim
#'
#' @examples
#' \dontrun{get_indexes_helper()}
#' \dontrun{get_indexes_helper(with_description = FALSE)}
#' \dontrun{get_indexes_helper(get_request, FALSE)}
#'
#'
get_indexes_helper <- function(request = NULL, with_description = TRUE) {

    if (is.null(request)) {
        request <- GET(URL_INDEXES, add_headers('User-Agent' = 'Mozilla'))
    }

    options <- request %>%
        read_html() %>%
        html_nodes(xpath = INDEXES_XPATH)

    indexes_values <- options %>%
        html_attr('value')

    if (!with_description) return(indexes_values)

    indexes_description <- options %>%
        html_text() %>%
        str_trim()

    return(data.frame(INDEX_CODE = indexes_values,
                      DESCRIPTION = indexes_description))

}
blnash508/cfm documentation built on May 30, 2019, 4:31 p.m.