Skip to content

Commit

Permalink
added message_tbl
Browse files Browse the repository at this point in the history
  • Loading branch information
seankross committed Nov 21, 2016
1 parent 882286e commit ab4467a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 10 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Package: twilio
Type: Package
Title: An interface to the Twilio API for R
Version: 0.1.0.9008
Version: 0.1.0.9012
Authors@R: person("Sean", "Kross", , "[email protected]", c("aut", "cre"))
Description: Send and retrieve text and picture messages using the Twilio API.
Depends:
R (>= 3.1.0)
Imports:
purrr,
httr,
jsonlite
jsonlite,
magrittr,
lubridate
License: MIT + file LICENSE
URL: http://github.com/seankross/twilio
Encoding: UTF-8
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(print,twilio_media)
S3method(print,twilio_message)
export(get_message_media)
export(get_messages)
export(message_tbl)
export(send_message)
importFrom(httr,GET)
importFrom(httr,POST)
Expand All @@ -12,5 +13,10 @@ importFrom(httr,content)
importFrom(httr,http_type)
importFrom(httr,modify_url)
importFrom(httr,status_code)
importFrom(httr,user_agent)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,parse_date_time)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(purrr,map)
importFrom(purrr,map_if)
2 changes: 1 addition & 1 deletion R/get_message_media.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param message_sid An SID for a message that contains media.
#' @return A list containing media information.
#' @importFrom jsonlite fromJSON
#' @importFrom httr modify_url GET authenticate http_type content
#' @importFrom httr modify_url GET authenticate http_type content user_agent
#' @importFrom purrr map
#' @export
#' @examples
Expand Down
2 changes: 1 addition & 1 deletion R/get_messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param page_size The number of messages per page. The maximum number allowed is 1000.
#' @return A \code{twilio_message_log} object.
#' @importFrom jsonlite fromJSON
#' @importFrom httr modify_url GET authenticate http_type content
#' @importFrom httr modify_url GET authenticate http_type content user_agent
#' @importFrom purrr map
#' @export
#' @examples
Expand Down
39 changes: 39 additions & 0 deletions R/message_tbl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Make a Data Frame from Message Logs
#'
#' @param message_log An S3 object with the class \code{twilio_message_log}. Likely
#' the result of a call to \code{\link{get_messages}}.
#' @return A data frame.
#'
#' @importFrom magrittr %>% %<>%
#' @importFrom purrr map map_if
#' @importFrom lubridate parse_date_time
#' @export
#' @examples
#' \dontrun{
#'
#' # Set API credentials
#' # You only need to do this once per R session
#' Sys.setenv(TWILIO_SID = "M9W4Ozq8BFX94w5St5hikg7UV0lPpH8e56")
#' Sys.setenv(TWILIO_TOKEN = "483H9lE05V0Jr362eq1814Li2N1I424t")
#'
#' # Get messages sent to your account
#' messages <- get_messages()
#'
#' # Create data frame from log
#' sms_data <- message_tbl(messages)
#'
#' }
message_tbl <- function(message_log){
stopifnot(identical(class(message_log), "twilio_message_log"))

raw_log <- as.data.frame(do.call(rbind, message_log), stringsAsFactors = FALSE)
raw_log$date_created %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z")
raw_log$date_created <- do.call(c, raw_log$date_created)
raw_log$date_updated %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z")
raw_log$date_updated <- do.call(c, raw_log$date_updated)
raw_log$date_sent %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z")
raw_log$date_sent <- do.call(c, raw_log$date_sent)
raw_log$error_code %<>% map(function(x){ifelse(is.null(x), NA, x)}) %>% unlist()
raw_log$error_message %<>% map(function(x){ifelse(is.null(x), NA, x)}) %>% unlist()
raw_log %>% map_if(is.list, unlist) %>% as.data.frame(stringsAsFactors = FALSE)
}
4 changes: 0 additions & 4 deletions R/messages_tbl.R

This file was deleted.

13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ You should only need to do this once per session.
```r
Sys.setenv(TWILIO_SID = "M9W4Ozq8BFX94w5St5hikg7UV0lPpH8e56")
Sys.setenv(TWILIO_TOKEN = "483H9lE05V0Jr362eq1814Li2N1I424t")

library(twilio)
```

### Send a Text Message
Expand All @@ -39,7 +41,8 @@ send_message("2125557634", "9178675903", media_url = "https://www.r-project.org/
### Send a Picture Message with Text

```r
send_message("2125557634", "9178675903", "Do you like the new logo?", "https://www.r-project.org/logo/Rlogo.png")
send_message("2125557634", "9178675903", "Do you like the new logo?",
"https://www.r-project.org/logo/Rlogo.png")
```

### Get Message Logs
Expand All @@ -48,9 +51,15 @@ send_message("2125557634", "9178675903", "Do you like the new logo?", "https://w
messages <- get_messages()
```

### Makes Logs into a Data Frame

```r
sms_data <- message_tbl(messages)
```

### Get Media from a Message

```r
media <- get_message_media(messages[[1]]$sid)
media <- get_message_media(sms_data$sid[1])
browseURL(media[[1]]$url)
```
35 changes: 35 additions & 0 deletions man/message_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab4467a

Please sign in to comment.