From f86d658e95b1de85bf8c94c04c74544a6a94b64b Mon Sep 17 00:00:00 2001 From: FvD Date: Fri, 14 Aug 2020 08:58:30 -0600 Subject: [PATCH 01/14] Code review --- R/get_contact_information.R | 6 ------ R/utils.R | 1 + tests/testthat/test-cleaning.R | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/R/get_contact_information.R b/R/get_contact_information.R index 7d0e7b4..a273159 100644 --- a/R/get_contact_information.R +++ b/R/get_contact_information.R @@ -35,18 +35,12 @@ get_contact_information <- function(user_code, api_token, contact_id = "") { ... = contact_id) }) - contenido <- system.file("testdata/prueba_get_contact_information.json", - package = "lacrmr") - contenido <- httr::content(r, "text") contenido <- jsonlite::fromJSON(contenido, simplifyVector = TRUE) - jsonlite::toJSON(contenido, pretty = TRUE) - for (i in 1:length(contenido$Contact)) { - # print(contenido[["Contact"]][[i]]) contenido$Contact[i][(is.null(contenido$Contact[[i]]) == TRUE)] <- NA contenido$Contact[i][(sjmisc::is_empty(contenido$Contact[[i]]) == TRUE)] <- NA contenido$Contact[i][(contenido$Contact[[i]] == "")] <- NA diff --git a/R/utils.R b/R/utils.R index 983ef93..c002f66 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,4 +1,5 @@ #' GET call to lacrm API +#' @param x an integer #' @param user_code The User Code to identify your account #' @param api_function The API token to connect to your account #' @param api_function The `Less Annoying CRM` function that you want to use in the API call diff --git a/tests/testthat/test-cleaning.R b/tests/testthat/test-cleaning.R index 81c76fa..8ae8f65 100644 --- a/tests/testthat/test-cleaning.R +++ b/tests/testthat/test-cleaning.R @@ -1,7 +1,7 @@ -context("limpieza") +context("Cleaning") -testthat::test_that("List are remove from the pipeline report", { +testthat::test_that("List are removed from the pipeline report", { mockery::stub(where = get_pipeline_report, what = "jsonlite::fromJSON", how = pipeline_data) From fae48c34a82d2b72b543447426071f8ae041d0c9 Mon Sep 17 00:00:00 2001 From: FvD Date: Fri, 14 Aug 2020 09:28:18 -0600 Subject: [PATCH 02/14] remove non-existent param from docs --- R/utils.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index c002f66..983ef93 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,5 +1,4 @@ #' GET call to lacrm API -#' @param x an integer #' @param user_code The User Code to identify your account #' @param api_function The API token to connect to your account #' @param api_function The `Less Annoying CRM` function that you want to use in the API call From c72cac429c6df9dc57b598c9e16850a4933702dd Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 10:42:53 -0600 Subject: [PATCH 03/14] Restore previous version with logo --- README.Rmd | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.Rmd b/README.Rmd index c68a7f6..f4ce092 100644 --- a/README.Rmd +++ b/README.Rmd @@ -13,7 +13,11 @@ knitr::opts_chunk$set( ) ``` -# lacrmr +# lacrmr + + +[![CRAN status](https://www.r-pkg.org/badges/version/lacrmr)](https://cran.r-project.org/package=lacrmr) + Get the information from your Less Annoying Customer Relationship Management API in a tidy data way. Useful for getting metrics, visualize your goals, create reports and automate your workflow. @@ -40,7 +44,7 @@ are and will return: ## Installation -Right now we have the development version. You can install `lacrmr` from github: +Rigth now we have the development version. You can install lacrmr from github: ```{r, eval = FALSE} #install.packages("devtools") @@ -92,6 +96,6 @@ Have fun analyzing your customer relationship data! ## Getting help If you have problems using the functions or find a bug, please let us know -with a minimal reproducible example on [github](https://github.com/ixpantia/lacrmr/issues) -or send us an email to `hola@ixpantia.com`. +with a minimal reproducible example on [github](https://github.com/ixpantia/lacrmr/issues) or send ud an email to +hola@ixpantia.com From 9a68e3b82d2261c55e5cc8fdb8daea231c0a3bb5 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 10:43:19 -0600 Subject: [PATCH 04/14] Spellcheck and include png in ignore file --- .Rbuildignore | 4 +++- README.Rmd | 41 ++++++++++++++++++++++------------------- README.md | 16 +++++++++++----- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index cbf6444..70ff5b4 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -4,4 +4,6 @@ ^docs$ ^Meta$ ^README\.Rmd$ - +^README-.*\.png$ +^.Rprofile$ +^LICENSE\.md$ diff --git a/README.Rmd b/README.Rmd index f4ce092..7b2a818 100644 --- a/README.Rmd +++ b/README.Rmd @@ -19,24 +19,26 @@ knitr::opts_chunk$set( [![CRAN status](https://www.r-pkg.org/badges/version/lacrmr)](https://cran.r-project.org/package=lacrmr) -Get the information from your Less Annoying Customer Relationship Management API in a tidy data way. Useful for getting metrics, visualize your goals, create reports -and automate your workflow. +Get the information from your Less Annoying Customer Relationship Management +API in a tidy data way. Useful for getting metrics, visualize your goals, +create reports and automate your workflow. ## Overview -If you have a business or an organization that uses [Less Annoying CRM](https://www.lessannoyingcrm.com/) to manage contacts but also you -are an R user, probably you will want to get your customer relationship data into +If you have a business or an organization that uses [Less Annoying +CRM](https://www.lessannoyingcrm.com/) to manage contacts but also you are an R +user, probably you will want to get your customer relationship data into R and do your own analysis or even to automate your monthly reports. -For this you will need to connect to the Less Annoying CRM API and deal with the -json file to take it to a tidy format as a first step. +For this you will need to connect to the Less Annoying CRM API and deal with +the json file to take it to a tidy format as a first step. -This package provides you with 4 functions that makes this process easier. These -are and will return: +This package provides you with 4 functions that makes this process easier. +These are and will return: | Function | Return | | -------- | ------ | -| **get_pipeline_report()** | It will provide you with a dataframe containing the data correspondant to the pipeline that you need. | +| **get_pipeline_report()** | It will provide you with a dataframe containing the data corresponding to the pipeline that you need. | | **get_account_information** | This will give you the information of your account. | | **get_contact_information** | Information related to the contact you are searching for. | | **search_contacts** | If you want to make sure a contact exists on your CRM, this function will return an statement confirming the contact. | @@ -44,7 +46,7 @@ are and will return: ## Installation -Rigth now we have the development version. You can install lacrmr from github: +Right now we have the development version. You can install lacrmr from Github: ```{r, eval = FALSE} #install.packages("devtools") @@ -53,9 +55,10 @@ devtools::install_github("ixpantia/lacrmr") ## Credentials: -At first you will need to obtain your [user code and API token](https://www.lessannoyingcrm.com/app/Settings/Api) -from your Less Annoying CRM. Once you have your credentials, you will be able -to use the functions and take your lacrm data into R! +At first you will need to obtain your [user code and API +token](https://www.lessannoyingcrm.com/app/Settings/Api) from your Less +Annoying CRM. Once you have your credentials, you will be able to use the +functions and take your lacrm data into R! If you need advice on how to do this and also what are the best practices to not leave your credentials in your code, check the package vignette! @@ -87,15 +90,15 @@ sales <- get_pipeline_report(user_code = "12454", dplyr::glimpse(sales) ``` -Once you are done you will have a data frame with your customer relationship data. -This will give you the opportunity to create your own visualizations or even -your personalized dashboard. +Once you are done you will have a data frame with your customer relationship +data. This will give you the opportunity to create your own visualizations or +even your personalized dashboard. Have fun analyzing your customer relationship data! ## Getting help -If you have problems using the functions or find a bug, please let us know -with a minimal reproducible example on [github](https://github.com/ixpantia/lacrmr/issues) or send ud an email to -hola@ixpantia.com +If you have problems using the functions or find a bug, please let us know with +a minimal reproducible example on [github](https://github.com/ixpantia/lacrmr/issues) +or send us an email to hola@ixpantia.com diff --git a/README.md b/README.md index 60576df..7d64724 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ -# lacrmr +# lacrmr + + + +[![CRAN +status](https://www.r-pkg.org/badges/version/lacrmr)](https://cran.r-project.org/package=lacrmr) + Get the information from your Less Annoying Customer Relationship Management API in a tidy data way. Useful for getting metrics, visualize @@ -23,15 +29,15 @@ easier. These are and will return: | Function | Return | | ----------------------------- | --------------------------------------------------------------------------------------------------------------------- | -| **get\_pipeline\_report()** | It will provide you with a dataframe containing the data correspondant to the pipeline that you need. | +| **get\_pipeline\_report()** | It will provide you with a dataframe containing the data corresponding to the pipeline that you need. | | **get\_account\_information** | This will give you the information of your account. | | **get\_contact\_information** | Information related to the contact you are searching for. | | **search\_contacts** | If you want to make sure a contact exists on your CRM, this function will return an statement confirming the contact. | ## Installation -Rigth now we have the development version. You can install lacrmr from -github: +Right now we have the development version. You can install lacrmr from +Github: ``` r #install.packages("devtools") @@ -109,5 +115,5 @@ Have fun analyzing your customer relationship data\! If you have problems using the functions or find a bug, please let us know with a minimal reproducible example on -[github](https://github.com/ixpantia/lacrmr/issues) or send ud an email +[github](https://github.com/ixpantia/lacrmr/issues) or send us an email to From d294c52db15e1f7b2a0538692425b3a0f5d9dc61 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 11:03:41 -0600 Subject: [PATCH 05/14] Code review --- tests/testthat/test-credentials.R | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/testthat/test-credentials.R b/tests/testthat/test-credentials.R index f997fc5..66dd598 100644 --- a/tests/testthat/test-credentials.R +++ b/tests/testthat/test-credentials.R @@ -26,16 +26,3 @@ testthat::test_that("Search contacts error message with invalid credentials", { search_term = "brenesii")) }) - -# test_that("pipeline report is succesful with valid credentials", { -# # Acceso a claves --------------------------------------------------------- -# codigo_usuario <- "88518" -# token_api <- "44HK8TM1ZTFMTDTNYZ1M215DPH15D0M7310SX31F97G3P2SC3F" -# pipelineid <- "3641868001717448079753414412494" -# -# # Hacer conexion -# pipeline_general <- get_pipeline_report(user_code = codigo_usuario, -# api_token = token_api, -# pipelineid = pipelineid) -# }) - From 82fda13a03f2320bbd9d2701b8079ccb9aba1c04 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 12:16:21 -0600 Subject: [PATCH 06/14] Ref #46 - fix error messages --- R/get_pipeline_report.R | 58 +++++++++++-------------------- tests/testthat/test-credentials.R | 8 +++-- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/R/get_pipeline_report.R b/R/get_pipeline_report.R index 6e57e07..458430f 100644 --- a/R/get_pipeline_report.R +++ b/R/get_pipeline_report.R @@ -23,7 +23,7 @@ NULL #' @export get_pipeline_report <- function(user_code, api_token, pipelineid) { if (missing(user_code)) { - warning("Please add a valid user code") + warning("Please add a valid user code") } else if (missing(api_token)) { warning("Please add a valid API token") } else if (missing(pipelineid)) { @@ -31,49 +31,33 @@ get_pipeline_report <- function(user_code, api_token, pipelineid) { } else { tryCatch({ - r <- get_request(user_code = user_code, - api_token = api_token, - api_function = "GetPipelineReport", - ... = pipelineid) - }) + r <- get_request(user_code = user_code, + api_token = api_token, + api_function = "GetPipelineReport", + ... = pipelineid) + }) - contenido <- jsonlite::fromJSON(httr::content(r, "text"), - simplifyVector = TRUE) + validate_json <- jsonlite::validate(httr::content(r, "text"))[1] - if (length(contenido$Error) == 1) { - if (stringr::str_detect(contenido$Error, "Invalid user credentials") == TRUE) { - stop("Invalid user credentials. Please check your user code or your api token", - call. = FALSE) - } - } + if (validate_json == FALSE) { + stop("Invalid user credentials or pipeline ID.\n Please check your user code or your api token") + } - if (length(contenido$Error) == 1) { - if (stringr::str_detect(contenido$Error, "Your account is not active") == TRUE) { - stop("Your account is not active. Please contact lacrm", - call. = FALSE) - } - } + pipeline <- jsonlite::fromJSON(httr::content(r, "text"), + simplifyVector = TRUE) - contenido <- as.data.frame(contenido) - contenido <- jsonlite::flatten(contenido) + pipeline <- as.data.frame(pipeline) + pipeline <- jsonlite::flatten(pipeline) - if (length(contenido$Error) == 1) { - if (stringr::str_detect(contenido$Error, "You don't have permission to view PipelineId") == - TRUE) { - stop("Invalid pipelineid. Please check your pipelineid", - call. = FALSE) - } - } + # Clean data frame variable names. + pipeline <- janitor::clean_names(pipeline) - # Clean data frame variable names. - contenido <- janitor::clean_names(contenido) + # Select variables of interest + pipeline <- pipeline %>% + dplyr::select(-result_email, -result_phone, -result_address, + -result_website, -result_contact_custom_fields) - # Select variables of interest - contenido <- contenido %>% - dplyr::select(-result_email, -result_phone, -result_address, - -result_website, -result_contact_custom_fields) - - return(contenido) + return(pipeline) } } diff --git a/tests/testthat/test-credentials.R b/tests/testthat/test-credentials.R index 66dd598..7ec7fa3 100644 --- a/tests/testthat/test-credentials.R +++ b/tests/testthat/test-credentials.R @@ -1,26 +1,30 @@ context("credentials") testthat::test_that("Pipeline report error with invalid credentials", { + skip_on_cran() expect_error(get_pipeline_report(user_code = "6A6E88", api_token = "96066", pipelineid = "57102821"), - "Invalid user credentials. Please check your user code or your api token") + "Invalid user credentials.") }) testthat::test_that("Account information error message with invalid credentials", { + skip_on_cran() expect_error(get_account_information(user_code = "6A6E88", api_token = "FBHV7C"), - "Invalid user credentials. Please check your user code or your api token") + "Invalid user credentials") }) testthat::test_that("Contact information error message with invalid credentials", { + skip_on_cran() expect_error(get_contact_information(user_code = "6A6E88", api_token = "TQ9XM", contact_id = "Fulano")) }) testthat::test_that("Search contacts error message with invalid credentials", { + skip_on_cran() expect_error(search_contacts(user_code = "6A6E88", api_token = "TQ9XM", search_term = "brenesii")) From de5bf3dee012e038fb378078a547b8c322e4d944 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 13:34:29 -0600 Subject: [PATCH 07/14] Fix credential validation --- R/get_contact_information.R | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/R/get_contact_information.R b/R/get_contact_information.R index a273159..2db4da8 100644 --- a/R/get_contact_information.R +++ b/R/get_contact_information.R @@ -35,22 +35,32 @@ get_contact_information <- function(user_code, api_token, contact_id = "") { ... = contact_id) }) - contenido <- httr::content(r, "text") + validate_json <- jsonlite::validate(httr::content(r, "text"))[1] - contenido <- jsonlite::fromJSON(contenido, + if (validate_json == FALSE) { + stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") + } + + contact_info <- httr::content(r, "text") + + if (contact_info$Success[1] == FALSE) { + stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") + } + + contact_info <- jsonlite::fromJSON(contact_info, simplifyVector = TRUE) - for (i in 1:length(contenido$Contact)) { - contenido$Contact[i][(is.null(contenido$Contact[[i]]) == TRUE)] <- NA - contenido$Contact[i][(sjmisc::is_empty(contenido$Contact[[i]]) == TRUE)] <- NA - contenido$Contact[i][(contenido$Contact[[i]] == "")] <- NA + for (i in 1:length(contact_info$Contact)) { + contact_info$Contact[i][(is.null(contact_info$Contact[[i]]) == TRUE)] <- NA + contact_info$Contact[i][(sjmisc::is_empty(contact_info$Contact[[i]]) == TRUE)] <- NA + contact_info$Contact[i][(contact_info$Contact[[i]] == "")] <- NA } - contenido <- as.data.frame(contenido) %>% + contact_info <- as.data.frame(contact_info) %>% dplyr::select(-Success) %>% janitor::clean_names() - return(contenido) + return(contact_info) } } From faebd0466a7fc804e589d00cbf54ebf826f4ce5f Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 13:34:49 -0600 Subject: [PATCH 08/14] Include edge case (proper json error) --- R/get_pipeline_report.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/get_pipeline_report.R b/R/get_pipeline_report.R index 458430f..d813bd7 100644 --- a/R/get_pipeline_report.R +++ b/R/get_pipeline_report.R @@ -46,6 +46,10 @@ get_pipeline_report <- function(user_code, api_token, pipelineid) { pipeline <- jsonlite::fromJSON(httr::content(r, "text"), simplifyVector = TRUE) + if (pipeline$Success[1] == FALSE) { + stop("Invalid user credentials or pipeline ID.\n Please check your user code or your api token") + } + pipeline <- as.data.frame(pipeline) pipeline <- jsonlite::flatten(pipeline) From e9aa9bbb77e807c2da8c8e67de33740f7b5ef4c6 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 13:44:34 -0600 Subject: [PATCH 09/14] REf #46 fix warning --- R/get_account_information.R | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/R/get_account_information.R b/R/get_account_information.R index 1e69d2d..17f2909 100644 --- a/R/get_account_information.R +++ b/R/get_account_information.R @@ -32,21 +32,24 @@ get_account_information <- function(user_code, api_token) { r <- get_request(user_code = user_code, api_token = api_token, api_function = 'GetUserInfo') - }) - contenido <- httr::content(r, "text") - contenido <- jsonlite::fromJSON(contenido) - if (length(contenido$Error) == 1) { - if (stringr::str_detect(contenido$Error, "Invalid user credentials") == TRUE) { - stop("Invalid user credentials. Please check your user code or your api token", - call. = FALSE) - } - } + validate_json <- jsonlite::validate(httr::content(r, "text"))[1] + + if (validate_json == FALSE) { + stop("Invalid user credentials.\n Please check your user code or your api token") + } + + account_info <- httr::content(r, "text") + account_info <- jsonlite::fromJSON(account_info) + + if (pipeline$Success[1] == FALSE) { + stop("Invalid user credentials or pipeline ID.\n Please check your user code or your api token") + } - contenido <- as.data.frame(contenido) + account_info <- as.data.frame(account_info) - return(contenido) + return(account_info) } From 81cee427ed2346dbc314208070e978a9a707eb53 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 13:47:38 -0600 Subject: [PATCH 10/14] Fix test to reflect changes --- tests/testthat/test-error-warning-msg.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-error-warning-msg.R b/tests/testthat/test-error-warning-msg.R index 2f86980..e05f518 100644 --- a/tests/testthat/test-error-warning-msg.R +++ b/tests/testthat/test-error-warning-msg.R @@ -10,7 +10,7 @@ testthat::test_that("Invalid credentials", { expect_error( get_account_information(user_code = "6A6E88", api_token = "57102821"), - "Invalid user credentials. Please check your user code or your api token" + "Invalid user credentials" ) expect_warning( From 0d2e79481e022ece28a5ddadb423603822fe8d0e Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 17:15:18 -0600 Subject: [PATCH 11/14] Ref #47 reorganize test --- R/get_pipeline_report.R | 5 +++-- tests/testthat/test-cleaning.R | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/R/get_pipeline_report.R b/R/get_pipeline_report.R index d813bd7..885c8f7 100644 --- a/R/get_pipeline_report.R +++ b/R/get_pipeline_report.R @@ -37,9 +37,10 @@ get_pipeline_report <- function(user_code, api_token, pipelineid) { ... = pipelineid) }) - validate_json <- jsonlite::validate(httr::content(r, "text"))[1] + content <- httr::content(r, "text") + validate_json <- jsonlite::validate(content) - if (validate_json == FALSE) { + if (validate_json[1] == FALSE) { stop("Invalid user credentials or pipeline ID.\n Please check your user code or your api token") } diff --git a/tests/testthat/test-cleaning.R b/tests/testthat/test-cleaning.R index 8ae8f65..2df092e 100644 --- a/tests/testthat/test-cleaning.R +++ b/tests/testthat/test-cleaning.R @@ -1,17 +1,21 @@ - context("Cleaning") testthat::test_that("List are removed from the pipeline report", { + + mockery::stub(where = get_pipeline_report, + what = "jsonlite::validate", + how = TRUE) + mockery::stub(where = get_pipeline_report, what = "jsonlite::fromJSON", how = pipeline_data) pipeline_test <- get_pipeline_report(user_code = "user_code_test", - api_token = "token_api_test", - pipelineid = "pipeline_test") + api_token = "token_api_test", + pipelineid = "pipeline_test") - expect_equal(ncol(pipeline_test), 33) - expect_equal(nrow(pipeline_test), 7) + expect_equal(ncol(pipeline_test), 33) + expect_equal(nrow(pipeline_test), 7) }) From a3289d18e68e2acfbef41b3aab11399bfc537840 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 17:53:57 -0600 Subject: [PATCH 12/14] Ref #47 Fixes issues in get_contact_information --- R/get_contact_information.R | 10 ++++++---- R/utils.R | 2 +- man/get_contact_information.Rd | 2 +- tests/testthat/test-cleaning.R | 18 +++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/R/get_contact_information.R b/R/get_contact_information.R index 2db4da8..09795f7 100644 --- a/R/get_contact_information.R +++ b/R/get_contact_information.R @@ -35,20 +35,22 @@ get_contact_information <- function(user_code, api_token, contact_id = "") { ... = contact_id) }) - validate_json <- jsonlite::validate(httr::content(r, "text"))[1] + content <- httr::content(r, "text") + validate_json <- jsonlite::validate(content) + + print(validate_json) if (validate_json == FALSE) { stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") } - contact_info <- httr::content(r, "text") + contact_info <- jsonlite::fromJSON(content, simplifyVector = TRUE) if (contact_info$Success[1] == FALSE) { stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") } - contact_info <- jsonlite::fromJSON(contact_info, - simplifyVector = TRUE) + print(contact_info) for (i in 1:length(contact_info$Contact)) { contact_info$Contact[i][(is.null(contact_info$Contact[[i]]) == TRUE)] <- NA diff --git a/R/utils.R b/R/utils.R index 983ef93..34bb756 100644 --- a/R/utils.R +++ b/R/utils.R @@ -11,7 +11,7 @@ get_request <- function(user_code, api_token, api_function, ...) { pipelineid <- item } else if (api_function == "SearchContacts") { search_term <- item - } else if (api_function == "ContactId") { + } else if (api_function == "GetContact") { contact_id <- item } diff --git a/man/get_contact_information.Rd b/man/get_contact_information.Rd index e805ca8..b19a53d 100644 --- a/man/get_contact_information.Rd +++ b/man/get_contact_information.Rd @@ -4,7 +4,7 @@ \alias{get_contact_information} \title{get_contact_information} \usage{ -get_contact_information(user_code, api_token, contact_id = "") +get_contact_information(user_code, api_token, contactid = "") } \arguments{ \item{user_code}{The user code to identify your account} diff --git a/tests/testthat/test-cleaning.R b/tests/testthat/test-cleaning.R index 2df092e..caaf5ee 100644 --- a/tests/testthat/test-cleaning.R +++ b/tests/testthat/test-cleaning.R @@ -20,20 +20,24 @@ testthat::test_that("List are removed from the pipeline report", { }) -testthat::test_that("Lists are remove from the get contacts information", { +testthat::test_that("Lists are removed from the contacts information", { mockery::stub(where = get_contact_information, - what = "jsonlite::fromJSON", - how = contact_information) + what = "jsonlite::validate", + how = TRUE) mockery::stub(where = get_contact_information, - what = "get_request", + what = "jsonlite::fromJSON", how = contact_information) + #mockery::stub(where = get_contact_information, + # what = "get_request", + # how = ) + get_contact_information_test <- get_contact_information(user_code = "user_code_test", api_token = "token_api_test", - contact_id = 123) + contact_id = "123") - expect_equal(ncol(pipeline_test), 33) - expect_equal(nrow(pipeline_test), 7) + expect_equal(ncol(get_contact_information_test), 22) + expect_equal(nrow(get_contact_information_test), 1) }) From 5fc569a0777806e62d5fd3820b990f54d1a98a05 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 18:16:24 -0600 Subject: [PATCH 13/14] Introduced function to flatten list before processing The stepst to process the list was generating warnings. with this change the processing is a little bit cleaner. --- R/get_contact_information.R | 24 +++++++++++------------- R/utils.R | 17 +++++++++++++++++ man/get_contact_information.Rd | 2 +- tests/testthat/test-cleaning.R | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/R/get_contact_information.R b/R/get_contact_information.R index 09795f7..2944043 100644 --- a/R/get_contact_information.R +++ b/R/get_contact_information.R @@ -38,8 +38,6 @@ get_contact_information <- function(user_code, api_token, contact_id = "") { content <- httr::content(r, "text") validate_json <- jsonlite::validate(content) - print(validate_json) - if (validate_json == FALSE) { stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") } @@ -50,19 +48,19 @@ get_contact_information <- function(user_code, api_token, contact_id = "") { stop("Invalid user credentials or contact ID.\n Please check your user code or your api token") } - print(contact_info) - - for (i in 1:length(contact_info$Contact)) { - contact_info$Contact[i][(is.null(contact_info$Contact[[i]]) == TRUE)] <- NA - contact_info$Contact[i][(sjmisc::is_empty(contact_info$Contact[[i]]) == TRUE)] <- NA - contact_info$Contact[i][(contact_info$Contact[[i]] == "")] <- NA - } + contact_info <- flattenlist(contact_info) + contact_info <- sapply(contact_info, + function(x) ifelse(x == "NULL", NA, x)) + contact_info <- lapply(contact_info, + function(x) if (length(x) == 0) {0} else {x}) + contact_info <- lapply(contact_info, + function(x) if (sjmisc::is_empty(x) == TRUE) {NA} else {x}) - contact_info <- as.data.frame(contact_info) %>% - dplyr::select(-Success) %>% - janitor::clean_names() + contact_info <- as.data.frame(contact_info) %>% + dplyr::select(-Success) %>% + janitor::clean_names() - return(contact_info) + return(contact_info) } } diff --git a/R/utils.R b/R/utils.R index 34bb756..300a5d9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -32,3 +32,20 @@ get_request <- function(user_code, api_token, api_function, ...) { } +#' Flatten nested lists +#' +#' Code based on answer on Stack Overflow: +#' https://stackoverflow.com/a/41882883/1329484 +#' +#' @param nested_list A nested list to be flattened +#' @noRd +flattenlist <- function(nested_list){ + morelists <- sapply(nested_list, function(xprime) class(xprime)[1] == "list") + out <- c(nested_list[!morelists], unlist(nested_list[morelists], recursive = FALSE)) + if (sum(morelists)) { + Recall(out) + }else{ + return(out) + } +} + diff --git a/man/get_contact_information.Rd b/man/get_contact_information.Rd index b19a53d..e805ca8 100644 --- a/man/get_contact_information.Rd +++ b/man/get_contact_information.Rd @@ -4,7 +4,7 @@ \alias{get_contact_information} \title{get_contact_information} \usage{ -get_contact_information(user_code, api_token, contactid = "") +get_contact_information(user_code, api_token, contact_id = "") } \arguments{ \item{user_code}{The user code to identify your account} diff --git a/tests/testthat/test-cleaning.R b/tests/testthat/test-cleaning.R index caaf5ee..67a46d9 100644 --- a/tests/testthat/test-cleaning.R +++ b/tests/testthat/test-cleaning.R @@ -37,7 +37,7 @@ testthat::test_that("Lists are removed from the contacts information", { api_token = "token_api_test", contact_id = "123") - expect_equal(ncol(get_contact_information_test), 22) + expect_equal(ncol(get_contact_information_test), 20) expect_equal(nrow(get_contact_information_test), 1) }) From 1e1ef77aa2bca5c4b88ecd024f49f2b9426e4cc5 Mon Sep 17 00:00:00 2001 From: FvD Date: Sat, 15 Aug 2020 18:18:21 -0600 Subject: [PATCH 14/14] removed trailing stub --- tests/testthat/test-cleaning.R | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-cleaning.R b/tests/testthat/test-cleaning.R index 67a46d9..f222641 100644 --- a/tests/testthat/test-cleaning.R +++ b/tests/testthat/test-cleaning.R @@ -1,7 +1,6 @@ context("Cleaning") testthat::test_that("List are removed from the pipeline report", { - mockery::stub(where = get_pipeline_report, what = "jsonlite::validate", how = TRUE) @@ -16,7 +15,6 @@ testthat::test_that("List are removed from the pipeline report", { expect_equal(ncol(pipeline_test), 33) expect_equal(nrow(pipeline_test), 7) - }) @@ -29,15 +27,11 @@ testthat::test_that("Lists are removed from the contacts information", { what = "jsonlite::fromJSON", how = contact_information) - #mockery::stub(where = get_contact_information, - # what = "get_request", - # how = ) - - get_contact_information_test <- get_contact_information(user_code = "user_code_test", - api_token = "token_api_test", - contact_id = "123") + get_contact_information_test <- get_contact_information( + user_code = "user_code_test", + api_token = "token_api_test", + contact_id = "123") expect_equal(ncol(get_contact_information_test), 20) expect_equal(nrow(get_contact_information_test), 1) - })