-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#' Translate Text to Morse Code using the FunTranslations API | ||
#' | ||
#' This function takes a string of text as input and translates it to Morse code using the FunTranslations API. | ||
#' | ||
#' @param text A character string containing the text to be translated to Morse code. | ||
#' @param api_key (optional) Your FunTranslations API key, if you have a paid subscription. | ||
#' @return A list containing the translated Morse code text and other metadata. | ||
#' @export | ||
translate_to_morse <- function(text, api_key = NULL) { | ||
url <- "https://api.funtranslations.com/translate/morse.json" | ||
payload <- list(text = text) | ||
|
||
# Add API key if provided | ||
if (!is.null(api_key)) { | ||
headers <- c("X-FunTranslations-Api-Secret" = api_key) | ||
} else { | ||
headers <- NULL | ||
} | ||
|
||
# Make the API request | ||
response <- httr::POST(url, body = payload, httr::add_headers(headers)) | ||
|
||
# Check the response status | ||
if (httr::status_code(response) == 200) { | ||
# Parse the JSON response | ||
result <- jsonlite::fromJSON(httr::content(response, "text")) | ||
|
||
# Extract the relevant information | ||
morse_code <- result$contents$translated | ||
text_translated <- result$contents$text | ||
translation_type <- result$contents$translation | ||
|
||
# Return the results as a list | ||
return(list( | ||
morse_code = morse_code, | ||
text_translated = text_translated, | ||
translation_type = translation_type | ||
)) | ||
} else { | ||
# Handle error cases | ||
stop(paste("Error:", httr::status_code(response), httr::content(response, "text"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.