Skip to content

Commit

Permalink
Merge pull request ixpantia#45 from FvD/T44
Browse files Browse the repository at this point in the history
Resolve problems with credentials check
  • Loading branch information
ronnyhdez authored Aug 22, 2020
2 parents db0304f + 1e1ef77 commit 086e423
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 123 deletions.
4 changes: 3 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
^docs$
^Meta$
^README\.Rmd$

^README-.*\.png$
^.Rprofile$
^LICENSE\.md$
25 changes: 14 additions & 11 deletions R/get_account_information.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

36 changes: 20 additions & 16 deletions R/get_contact_information.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,32 @@ get_contact_information <- function(user_code, api_token, contact_id = "") {
... = contact_id)
})

contenido <- system.file("testdata/prueba_get_contact_information.json",
package = "lacrmr")
content <- httr::content(r, "text")
validate_json <- jsonlite::validate(content)

contenido <- httr::content(r, "text")
if (validate_json == FALSE) {
stop("Invalid user credentials or contact ID.\n Please check your user code or your api token")
}

contenido <- jsonlite::fromJSON(contenido,
simplifyVector = TRUE)
contact_info <- jsonlite::fromJSON(content, simplifyVector = TRUE)

jsonlite::toJSON(contenido, pretty = TRUE)
if (contact_info$Success[1] == FALSE) {
stop("Invalid user credentials or contact ID.\n Please check your user code or your api token")
}

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
}
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})

contenido <- as.data.frame(contenido) %>%
dplyr::select(-Success) %>%
janitor::clean_names()
contact_info <- as.data.frame(contact_info) %>%
dplyr::select(-Success) %>%
janitor::clean_names()

return(contenido)
return(contact_info)
}

}
Expand Down
61 changes: 25 additions & 36 deletions R/get_pipeline_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,46 @@ 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)) {
warning("Please add a valid pipeline ID")
} 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)
content <- httr::content(r, "text")
validate_json <- jsonlite::validate(content)

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[1] == 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)
if (pipeline$Success[1] == 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, "You don't have permission to view PipelineId") ==
TRUE) {
stop("Invalid pipelineid. Please check your pipelineid",
call. = FALSE)
}
}
pipeline <- as.data.frame(pipeline)
pipeline <- jsonlite::flatten(pipeline)

# Clean data frame variable names.
contenido <- janitor::clean_names(contenido)
# Clean data frame variable names.
pipeline <- janitor::clean_names(pipeline)

# Select variables of interest
contenido <- contenido %>%
dplyr::select(-result_email, -result_phone, -result_address,
-result_website, -result_contact_custom_fields)
# Select variables of interest
pipeline <- pipeline %>%
dplyr::select(-result_email, -result_phone, -result_address,
-result_website, -result_contact_custom_fields)

return(contenido)
return(pipeline)
}
}

19 changes: 18 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}

47 changes: 27 additions & 20 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,40 @@ knitr::opts_chunk$set(
)
```

# lacrmr
# lacrmr <a href="url"><img src="man/figures/lacrmR.png" align="right" width="30%"></a>

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.
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/lacrmr)](https://cran.r-project.org/package=lacrmr)
<!-- badges: end -->

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. |


## Installation

Right 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")
Expand All @@ -49,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!
Expand Down Expand Up @@ -83,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 us 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

16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# lacrmr
# lacrmr <a href="url"><img src="man/figures/lacrmR.png" align="right" width="30%"></a>

<!-- badges: start -->

[![CRAN
status](https://www.r-pkg.org/badges/version/lacrmr)](https://cran.r-project.org/package=lacrmr)
<!-- badges: end -->

Get the information from your Less Annoying Customer Relationship
Management API in a tidy data way. Useful for getting metrics, visualize
Expand All @@ -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")
Expand Down Expand Up @@ -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 <hola@ixpantia.com>
36 changes: 19 additions & 17 deletions tests/testthat/test-cleaning.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
context("Cleaning")

context("limpieza")
testthat::test_that("List are removed from the pipeline report", {
mockery::stub(where = get_pipeline_report,
what = "jsonlite::validate",
how = TRUE)

testthat::test_that("List are remove from the pipeline report", {
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")

expect_equal(ncol(pipeline_test), 33)
expect_equal(nrow(pipeline_test), 7)
api_token = "token_api_test",
pipelineid = "pipeline_test")

expect_equal(ncol(pipeline_test), 33)
expect_equal(nrow(pipeline_test), 7)
})


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)

get_contact_information_test <- get_contact_information(user_code = "user_code_test",
api_token = "token_api_test",
contact_id = 123)

expect_equal(ncol(pipeline_test), 33)
expect_equal(nrow(pipeline_test), 7)
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)
})
Loading

0 comments on commit 086e423

Please sign in to comment.