R/configurations-from-file.R

Defines functions tables_from_yaml_data database_tables_from_file cryptowatch_config_from_file gdax_config_from_file

Documented in cryptowatch_config_from_file database_tables_from_file gdax_config_from_file tables_from_yaml_data

# Read in configurations and add them into the relevant configuration object

#' @title Read yaml API configurations into a \code{gdax_api_configurations} object
#' 
#' @description Reads in API configurations from a .yaml file.
#' 
#' @param yaml_file The location of the .yaml configuration file
#' 
#' @return A valid \code{gdax_api_configuration} object with slots filled from
#' the input \code{yaml_file}
#' 
gdax_config_from_file <- function (yaml_file) {
  
  # Read in the yml file
  yaml_data <- yaml::yaml.load_file(yaml_file)$gdax_config
  
  # Initialize a gdax_api_configurations object
  gdax_config_obj   <- gdax_api_configurations()
  
  # Fill in applicable slots with the yaml data
  slots_to_fill <- yaml_data[names(yaml_data) %in% slotNames(gdax_config_obj)]
  gdax_config_obj <- do.call(gdax_api_configurations, slots_to_fill)
  
  # Validate the object and return it
  validObject(gdax_config_obj)
  return (gdax_config_obj)
}

#' @title Read yaml API configurations into a \code{gdax_api_configurations} object
#' 
#' @description Reads in API configurations from a .yaml file.
#' 
#' @param yaml_file The location of the .yaml configuration file
#' 
#' @return A valid \code{gdax_api_configuration} object with slots filled from
#' the input \code{yaml_file}
#' 
cryptowatch_config_from_file <- function (yaml_file) {
  
  # Read in the yml file
  yaml_data <- yaml::yaml.load_file(yaml_file)$cryptowatch_config
  
  # Initialize a gdax_api_configurations object
  cryptowatch_config_obj <- cryptowatch_configurations()
  
  # Fill in applicable slots with the yaml data
  slots_to_fill <- yaml_data[names(yaml_data) %in% slotNames(cryptowatch_config_obj)]
  cryptowatch_config_obj <- do.call(cryptowatch_configurations, slots_to_fill)
  
  # Validate the object and return it
  validObject(cryptowatch_config_obj)
  return (cryptowatch_config_obj)
}

#' @title Read yaml database configurations into a \code{database_configurations} object
#' 
#' @description Reads in database configurations from a .yaml file.
#' 
#' @param yaml_file The location of the .yaml configuration file
#' 
#' @return A list of \code{btclearn_table} objects with slots filled from
#' the input \code{yaml_file}
#' 
database_tables_from_file <- function (yaml_file) {
  
  # Read in the yml file
  database_yaml_data <- yaml::yaml.load_file(yaml_file)
  
  # Build a list of each table in the database from those configurations
  database_tables <- tables_from_yaml_data(database_yaml_data)
  
  return (database_tables)
}

#' @title Convert database yaml data into a list of \code{btclearn_table} objects
#' 
#' @description Creates a list of valid \code{btclearn_table} objects
#' 
#' @param database_yaml_data Data from the database yaml file
#' 
#' @return A list of valid \code{btclearn_table} objects 
#' 
tables_from_yaml_data <- function (database_yaml_data) {
  
  # Generate a table for each table in the input
  databases <- names(database_yaml_data)
  all_table_obj <- lapply(databases, function (database) {
    
    # Extract out the tables and lapply over them
    tables <- names(database_yaml_data[[database]])
    database_tables <- lapply(tables, function (tab) {
      
      # Extract out table information for this table
      table_info <- database_yaml_data[[database]][[tab]]
      
      # Fill in missing information
      table_info$database_name <- database
      table_info$table_name    <- tab
      table_info$foreign_keys  <- 
        if (is.null(table_info$foreign_keys)) list() else table_info$foreign_keys 
      table_info$columns       <- 
        if (is.null(table_info$columns)) list() else table_info$columns
        
      # Build and return the table object
      table_obj <- do.call(btclearn_table, table_info)
      return (table_obj)
    })
    
    return (database_tables)
  })
  
  # Calling unlist removes emptiness but still returns a list ... emptiness
  # is a result of using an incomplete yaml file
  return (unlist(all_table_obj))
}
kyleengel/btclearn documentation built on June 7, 2018, 12:26 a.m.