-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from matthiasgomolka/sfplus
Clean support for SimFin+
- Loading branch information
Showing
29 changed files
with
1,145 additions
and
705 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,155 @@ | ||
#' Generic input checks | ||
#' @description This function covers all kinds of (recurring) input checks in | ||
#' {simfinapi}. This keeps the other functions cleaner. | ||
#' @inheritParams sfa_get_statement | ||
#' @inheritParams sfa_get_prices | ||
#' @inheritParams sfa_get_shares | ||
#' @inheritParams sfa_get_ref | ||
#' @importFrom checkmate assert_string assert_directory assert_character | ||
#' assert_integerish assert_choice assert_date | ||
#' @importFrom data.table year | ||
check_inputs <- function( | ||
api_key = NULL, cache_dir = NULL, ticker = NULL, simfin_id = NULL, | ||
statement = NULL, period = NULL, fyear = NULL, start = NULL, end = NULL, | ||
ttm = NULL, shares = NULL, ratios = NULL, type = NULL, ref_data = NULL | ||
) { | ||
if (!is.null(api_key)) { | ||
checkmate::assert_string(api_key, pattern = "^[[:alnum:]]{32}$") | ||
} | ||
if (!is.null(cache_dir)) { | ||
checkmate::assert_directory(cache_dir, access = "rw") | ||
} | ||
if (!is.null(ticker)) { | ||
checkmate::assert_character( | ||
ticker, | ||
pattern = "^[A-Za-z0-9_\\.\\-]+$", | ||
any.missing = FALSE, | ||
null.ok = TRUE | ||
) | ||
} | ||
if (!is.null(simfin_id)) { | ||
checkmate::assert_integerish( | ||
simfin_id, | ||
lower = 1L, | ||
any.missing = FALSE, | ||
null.ok = TRUE | ||
) | ||
} | ||
if (!is.null(statement)) { | ||
checkmate::assert_choice( | ||
statement, | ||
c("pl", "bs", "cf", "derived", "all"), | ||
fmatch = TRUE | ||
) | ||
} | ||
if (!is.null(period)) { | ||
checkmate::assert_choice( | ||
period, | ||
c("q1", "q2", "q3", "q4", "fy", "h1", "h2", "9m", "6m", "quarters"), | ||
fmatch = TRUE | ||
) | ||
} | ||
if (!is.null(fyear)) { | ||
checkmate::assert_integerish( | ||
fyear, | ||
lower = 1900L, | ||
upper = data.table::year(Sys.Date()) | ||
) | ||
msg_sfplus_required <- function(var, verb = "Omitting") { | ||
stop(verb, " '", var, "' is reserved for SimFin+ users.", call. = FALSE) | ||
} | ||
|
||
#' @importFrom checkmate assert_string | ||
check_api_key <- function(api_key) { | ||
checkmate::assert_string(api_key, pattern = "^[[:alnum:]]{32}$") | ||
} | ||
|
||
#' @importFrom checkmate assert_directory | ||
check_cache_dir <- function(cache_dir) { | ||
checkmate::assert_directory(cache_dir, access = "rw") | ||
} | ||
|
||
#' @importFrom checkmate assert_logical | ||
check_sfplus <- function(sfplus) { | ||
checkmate::assert_logical(sfplus, any.missing = FALSE, len = 1L) | ||
} | ||
|
||
#' @importFrom checkmate assert_character | ||
check_ticker <- function(ticker) { | ||
checkmate::assert_character( | ||
ticker, | ||
pattern = "^[A-Za-z0-9_\\.\\-]+$", | ||
any.missing = FALSE, | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_integerish | ||
check_simfin_id <- function(simfin_id) { | ||
checkmate::assert_integerish( | ||
simfin_id, | ||
lower = 1L, | ||
any.missing = FALSE, | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_choice | ||
check_statement <- function(statement, sfplus) { | ||
checkmate::assert_choice( | ||
statement, | ||
c("pl", "bs", "cf", "derived", "all"), | ||
fmatch = TRUE | ||
) | ||
if (statement == "all" & isFALSE(sfplus)) { | ||
stop('statement = "all" is reserved for SimFin+ users.', call. = FALSE) | ||
} | ||
if (!is.null(start)) { | ||
checkmate::assert_date( | ||
start, | ||
lower = as.Date("1900-01-01"), | ||
upper = Sys.Date() | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_choice | ||
check_period <- function(period, sfplus) { | ||
if (is.null(period) & isFALSE(sfplus)) { | ||
msg_sfplus_required("period") | ||
} | ||
if (!is.null(end)) { | ||
checkmate::assert_date( | ||
end, | ||
lower = as.Date("1900-01-01"), | ||
upper = Sys.Date() | ||
) | ||
checkmate::assert_choice( | ||
period, | ||
c("q1", "q2", "q3", "q4", "fy", "h1", "h2", "9m", "6m", "quarters"), | ||
null.ok = TRUE, | ||
fmatch = TRUE | ||
) | ||
if (period == "quarters" & isFALSE(sfplus)) { | ||
stop('period = "quarters" is reserved for SimFin+ users.', call. = FALSE) | ||
} | ||
if (!is.null(ttm)) { | ||
checkmate::assert_logical(ttm, any.missing = FALSE, len = 1L) | ||
} | ||
|
||
#' @importFrom checkmate assert_integerish | ||
check_fyear <- function(fyear, sfplus) { | ||
if (is.null(fyear) & isFALSE(sfplus)) { | ||
msg_sfplus_required("fyear") | ||
} | ||
if (!is.null(shares)) { | ||
checkmate::assert_logical(shares, any.missing = FALSE, len = 1L) | ||
checkmate::assert_integerish( | ||
fyear, | ||
lower = 1900L, | ||
upper = data.table::year(Sys.Date()), | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_date | ||
check_start <- function(start, sfplus) { | ||
if (!is.null(start) & isFALSE(sfplus)) { | ||
msg_sfplus_required("start", "Specifying") | ||
} | ||
if (!is.null(ratios)) { | ||
checkmate::assert_logical(ratios, any.missing = FALSE, len = 1L) | ||
checkmate::assert_date( | ||
start, | ||
lower = as.Date("1900-01-01"), | ||
upper = Sys.Date(), | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_date | ||
check_end <- function(end, sfplus) { | ||
if (!is.null(end) & isFALSE(sfplus)) { | ||
msg_sfplus_required("end", "Specifying") | ||
} | ||
if (!is.null(type)) { | ||
checkmate::assert_choice( | ||
type, | ||
choices = c("common", "wa-basic", "wa-diluted"), | ||
fmatch = TRUE | ||
checkmate::assert_date( | ||
end, | ||
lower = as.Date("1900-01-01"), | ||
upper = Sys.Date(), | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_logical | ||
check_ttm <- function(ttm) { | ||
checkmate::assert_logical(ttm, any.missing = FALSE, len = 1L) | ||
} | ||
|
||
#' @importFrom checkmate assert_logical | ||
check_shares <- function(shares, sfplus) { | ||
checkmate::assert_logical(shares, any.missing = FALSE, len = 1L) | ||
|
||
if (isTRUE(shares) & isFALSE(sfplus)) { | ||
stop( | ||
"'shares = TRUE' is reserved to SimFin+ users. As a normal user, please ", | ||
"use 'sfa_get_shares()' with 'type = \"wa-basic\"' or 'type = ", | ||
"\"wa-diluted\".", | ||
call. = FALSE | ||
) | ||
} | ||
if (!is.null(ref_data)) { | ||
checkmate::assert_choice( | ||
ref_data, | ||
choices = c("industries", "markets"), | ||
fmatch = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_logical | ||
check_ratios <- function(ratios, sfplus) { | ||
if (!is.null(ratios) & isFALSE(sfplus)) { | ||
msg_sfplus_required("ratios", "Specifying") | ||
} | ||
checkmate::assert_logical( | ||
ratios, | ||
any.missing = FALSE, | ||
len = 1L, | ||
null.ok = TRUE | ||
) | ||
} | ||
|
||
#' @importFrom checkmate assert_choice | ||
check_type <- function(type) { | ||
checkmate::assert_choice( | ||
type, | ||
choices = c("common", "wa-basic", "wa-diluted"), | ||
fmatch = TRUE | ||
) | ||
} | ||
|
||
#' @param api_key See function using this argument. | ||
#' @param cache_dir See function using this argument. | ||
#' @param ticker See function using this argument. | ||
#' @param simfin_id See function using this argument. | ||
#' @param statement See function using this argument. | ||
#' @param period See function using this argument. | ||
#' @param fyear See function using this argument. | ||
#' @param start See function using this argument. | ||
#' @param end See function using this argument. | ||
#' @param ttm See function using this argument. | ||
#' @param shares See function using this argument. | ||
#' @param ratios See function using this argument. | ||
#' @param type See function using this argument. | ||
#' @importFrom checkmate assert_choice | ||
check_ref_data <- function(ref_data) { | ||
checkmate::assert_choice( | ||
ref_data, | ||
choices = c("industries", "markets"), | ||
fmatch = TRUE | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.