Skip to content

Commit

Permalink
extend function to check if a file is a suitable .Rmd and to handle t…
Browse files Browse the repository at this point in the history
…he rendering, as well as derive title from the YAML frontmatter
  • Loading branch information
DaveParr committed May 23, 2020
1 parent aa0b286 commit ddccf8c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions R/is_postable_Rmd.R
Original file line number Diff line number Diff line change
@@ -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")
)
}
42 changes: 29 additions & 13 deletions R/post_new_article.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"))
}
}
31 changes: 31 additions & 0 deletions man/is_postable_Rmd.Rd

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

10 changes: 5 additions & 5 deletions man/post_new_article.Rd

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

0 comments on commit ddccf8c

Please sign in to comment.