Skip to content

Commit

Permalink
refactor: reuse url and only change endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Apr 7, 2024
1 parent c3800e0 commit 566a9f4
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions R/gleif.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
#' Download the latest LEI mapping data
#'
#' @param type `character(1)` the type of mapping data to download.
#' @references <https://www.gleif.org/en/lei-data/lei-mapping>
#' @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") |>
Expand All @@ -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 <https://www.gleif.org/en/lei-data/lei-mapping>
#' @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)
}

0 comments on commit 566a9f4

Please sign in to comment.