R/assets.R

Defines functions get_name get_id update_list ems

ems <- function(conn) {
  obj <- list()
  class(obj) <- "ems"
  obj$connection <- conn
  obj <- update_list(obj)
  return(obj)
}

update_list <- function(obj) {
  conn <- obj$connection
  r <- request(conn, uri_keys = c('ems_sys', 'list'))
  d <- httr::content(r)
  df <- data.frame(id = character(0), name = character(0), stringsAsFactors = FALSE)
  for ( i in 1:length(d) ) {
    df[i, ] <- unlist(d[[i]][c('id','name')])
  }
  df$id <- as.integer(df$id)
  obj$list <- df
  return(obj)
}

get_id <- function(obj, name) {
  name <- toupper(name)
  x <- obj$list$id[toupper(obj$list$name)==name]
  if ( length(x)==0 ) {
    stop(sprintf('%s is not available at EMS API.', name))
  } else if ( length(x)>1 ) {
    return(x[1])
  }
  return(x)
}

get_name <- function(obj, id) {
  return(obj$name[obj$id==id])
}
ge-flight-analytics/Rems documentation built on May 17, 2023, 8:02 a.m.