diff --git a/DESCRIPTION b/DESCRIPTION index ca1db22..1a591f7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,3 +9,6 @@ URL: https://github.com/DaveParr/dev.to.ol BugReports: http://that Encoding: UTF-8 RoxygenNote: 6.1.1 +Imports: + rmarkdown, + assertthat diff --git a/NAMESPACE b/NAMESPACE index b3e10d6..8e063f1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,11 @@ export(api_key) export(get_my_user) export(get_users_articles) +export(is_postable_Rmd) export(post_new_article) +importFrom(assertthat,has_extension) +importFrom(assertthat,is.readable) +importFrom(assertthat,see_if) importFrom(attempt,stop_if_not) importFrom(curl,has_internet) importFrom(httr,GET) diff --git a/R/is_postable_Rmd.R b/R/is_postable_Rmd.R new file mode 100644 index 0000000..b0514ba --- /dev/null +++ b/R/is_postable_Rmd.R @@ -0,0 +1,23 @@ +#' @title Check a file is postable article +#' @description Will check file is readable and extention is .Rmd +#' @param file file path +#' @return TRUE, or FALSE with message +#' @details DETAILS +#' @examples +#' \dontrun{ +#' if(interactive()){ +#' is_postable_Rmd("./my/file.Rmd) +#' attr(is_postable_Rmd("./my/file.Rmd), "msg") +#' } +#' } +#' @seealso +#' \code{\link[assertthat]{assert_that}},\code{\link[assertthat]{assertions-file}} +#' @rdname is_postable_Rmd +#' @export +#' @importFrom assertthat see_if is.readable has_extension +is_postable_Rmd <- function(file) { + assertthat::see_if( + assertthat::is.readable(file), + assertthat::has_extension(file, "Rmd") + ) +} diff --git a/R/post_new_article.R b/R/post_new_article.R index 6744013..e1d6bd8 100644 --- a/R/post_new_article.R +++ b/R/post_new_article.R @@ -1,5 +1,5 @@ #' @title Post a markdown file to dev.to -#' @description Create a new post well rendered markdown file generated from an .Rmd with a `github_document` output. +#' @description Create a new post from an .Rmd #' @param key Your API key, Default: NA #' @param file The path to the file, Default: file #' @return The response @@ -12,21 +12,37 @@ #' } #' @seealso #' \code{\link[httr]{POST}},\code{\link[httr]{add_headers}},\code{\link[httr]{verbose}},\code{\link[httr]{content}} -#' \code{\link[readr]{read_file}}, \code{\link[dev.to.ol]{api_key}} +#' \code{\link[readr]{read_file}}, \code{\link[dev.to.ol]{api_key}}, \code{\link[dev.to.ol]{is_postable_article}} #' @rdname post_new_article #' @export #' @importFrom httr POST add_headers verbose content #' @importFrom readr read_file -post_new_article <- function(key = NA, title, file) { - response <- httr::POST( - url = "https://dev.to/api/articles", - httr::add_headers("api-key" = api_key(key = key)), - body = list(article = list( - title = title, - body_markdown = readr::read_file(file = file) - )), - encode = 'json' - ) - httr::content(response) +post_new_article <- function(file, key = NA) { + + check_file <- is_postable_Rmd(file) + + if (check_file) { + file_frontmatter <- rmarkdown::yaml_front_matter(file) + + output_path <- rmarkdown::render('./data/test.Rmd', + output_format = 'github_document', + output_dir = getwd()) + + + file_string <- readr::read_file(output_path) + + response <- httr::POST( + url = "https://dev.to/api/articles", + httr::add_headers("api-key" = api_key(key = key)), + body = list(article = list( + title = file_frontmatter$title, + body_markdown = file_string + )), + encode = 'json' + ) + httr::content(response) + } else { + message(attr(check_file, "msg")) + } } diff --git a/man/is_postable_Rmd.Rd b/man/is_postable_Rmd.Rd new file mode 100644 index 0000000..f113be2 --- /dev/null +++ b/man/is_postable_Rmd.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_postable_Rmd.R +\name{is_postable_Rmd} +\alias{is_postable_Rmd} +\title{Check a file is postable article} +\usage{ +is_postable_Rmd(file) +} +\arguments{ +\item{file}{file path} +} +\value{ +TRUE, or FALSE with message +} +\description{ +Will check file is readable and extention is .Rmd +} +\details{ +DETAILS +} +\examples{ +\dontrun{ +if(interactive()){ + is_postable_Rmd("./my/file.Rmd) + attr(is_postable_Rmd("./my/file.Rmd), "msg") + } +} +} +\seealso{ +\code{\link[assertthat]{assert_that}},\code{\link[assertthat]{assertions-file}} +} diff --git a/man/post_new_article.Rd b/man/post_new_article.Rd index 37f0e46..9b76840 100644 --- a/man/post_new_article.Rd +++ b/man/post_new_article.Rd @@ -4,18 +4,18 @@ \alias{post_new_article} \title{Post a markdown file to dev.to} \usage{ -post_new_article(key = NA, title, file) +post_new_article(file, key = NA) } \arguments{ -\item{key}{Your API key, Default: NA} - \item{file}{The path to the file, Default: file} + +\item{key}{Your API key, Default: NA} } \value{ The response } \description{ -Create a new post well rendered markdown file generated from an .Rmd with a `github_document` output. +Create a new post from an .Rmd } \details{ Will look for an api key in the `.REnviron` file. Seems to check if the body is identical to a previous article and error if so with `"Body markdown has already been taken"`. @@ -29,5 +29,5 @@ if(interactive()){ } \seealso{ \code{\link[httr]{POST}},\code{\link[httr]{add_headers}},\code{\link[httr]{verbose}},\code{\link[httr]{content}} - \code{\link[readr]{read_file}}, \code{\link[dev.to.ol]{api_key}} + \code{\link[readr]{read_file}}, \code{\link[dev.to.ol]{api_key}}, \code{\link[dev.to.ol]{is_postable_article}} }