From 566a9f40ba56bb494e3c59e62ff5edc7ccc38631 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Sun, 7 Apr 2024 11:14:27 +0200 Subject: [PATCH] refactor: reuse url and only change endpoint --- R/gleif.R | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/R/gleif.R b/R/gleif.R index d22374d..ed0bbc7 100644 --- a/R/gleif.R +++ b/R/gleif.R @@ -1,13 +1,28 @@ +#' Download the latest LEI mapping data +#' +#' @param type `character(1)` the type of mapping data to download. +#' @references +#' @export +#' @examples +#' \donttest{ +#' lei_mapping("isin") +#' } +lei_mapping <- function(type = c("isin", "bic", "mic", "oc")) { + url <- latest_url(type) + mapping <- gleif_download(url) + as_tibble(mapping) +} + latest_url <- function(type = c("isin", "bic", "mic", "oc")) { type <- match.arg(type) - # nolint start - url <- switch(type, - isin = "https://www.gleif.org/en/lei-data/lei-mapping/download-isin-to-lei-relationship-files", - bic = "https://www.gleif.org/en/lei-data/lei-mapping/download-bic-to-lei-relationship-files", - mic = "https://www.gleif.org/en/lei-data/lei-mapping/download-mic-to-lei-relationship-files", - oc = "https://www.gleif.org/en/lei-data/lei-mapping/download-oc-to-lei-relationship-files" + url <- "https://www.gleif.org/en/lei-data/lei-mapping" + endpoint <- switch(type, + isin = "download-isin-to-lei-relationship-files", + bic = "download-bic-to-lei-relationship-files", + mic = "download-mic-to-lei-relationship-files", + oc = "download-oc-to-lei-relationship-files" ) - # nolint end + url <- paste(url, endpoint, sep = "/") files <- rvest::read_html(url) |> rvest::html_element("table") |> rvest::html_elements("a") |> @@ -25,18 +40,3 @@ gleif_download <- function(url) { mapping <- utils::read.csv(file) setNames(mapping, tolower(names(mapping))) } - -#' Download the latest LEI mapping data -#' -#' @param type `character(1)` the type of mapping data to download. -#' @references -#' @export -#' @examples -#' \donttest{ -#' lei_mapping("isin") -#' } -lei_mapping <- function(type = c("isin", "bic", "mic", "oc")) { - url <- latest_url(type) - mapping <- gleif_download(url) - as_tibble(mapping) -}