├── .Rbuildignore ├── LICENSE ├── .gitignore ├── NAMESPACE ├── GoTr.Rproj ├── man ├── Gotr-package.Rd └── got_api.Rd ├── DESCRIPTION ├── R ├── GoTr-package.R └── got_api.R └── README.md /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2017 2 | COPYRIGHT HOLDER: Mango Solutions 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(got_api) 4 | importFrom(httr,GET) 5 | importFrom(httr,content) 6 | importFrom(httr,http_error) 7 | importFrom(httr,http_type) 8 | importFrom(httr,modify_url) 9 | importFrom(httr,status_code) 10 | importFrom(httr,user_agent) 11 | importFrom(jsonlite,fromJSON) 12 | -------------------------------------------------------------------------------- /GoTr.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | -------------------------------------------------------------------------------- /man/Gotr-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/GoTr-package.R 3 | \docType{package} 4 | \name{Gotr-package} 5 | \alias{Gotr-package} 6 | \alias{Gotr} 7 | \title{R wrapper for An API of Ice And Fire} 8 | \description{ 9 | This package provides access to the \href{https://anapioficeandfire.com/}{anapioficeandfire} 10 | API from R. It pulls data of the universe of Ice and Fire including HBO series Game of Thrones. 11 | } 12 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: GoTr 2 | Title: R wrapper for An API of Ice And Fire 3 | Version: 0.0.0.9000 4 | Authors@R: person("Ava", "Yang", email = "ayang@mango-solutions.com", role = c("aut", "cre")) 5 | Description: This package provides access to the anapioficeandfire API from R. It pulls data 6 | of the universe of Ice and Fire including HBO series Game of Thrones. 7 | imports: httr, jsonlite 8 | License: MIT + file LICENSE 9 | Encoding: UTF-8 10 | LazyData: true 11 | RoxygenNote: 6.0.1 12 | -------------------------------------------------------------------------------- /R/GoTr-package.R: -------------------------------------------------------------------------------- 1 | #' R wrapper for An API of Ice And Fire 2 | #' 3 | #' This package provides access to the \href{https://anapioficeandfire.com/}{anapioficeandfire} 4 | #' API from R. It pulls data of the universe of Ice and Fire including HBO series Game of Thrones. 5 | #' 6 | #' @docType package 7 | #' @name Gotr-package 8 | #' @aliases Gotr Gotr-package 9 | #' @importFrom httr modify_url GET content http_type http_error status_code user_agent 10 | #' @importFrom jsonlite fromJSON 11 | NULL 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GoTr 2 | 3 | > R wrapper for An API of Ice And Fire 4 | 5 | [An API of Ice And Fire](https://anapioficeandfire.com/) is the world's greatest source for quantified and structured data from the universe of Ice and Fire (and the HBO series Game of Thrones). Current available resources cover Books, Characters and Houses. This package pulls data in JSON format and parse them to R list objects. 6 | 7 | ## Installation 8 | Installation from github requires the devtools package to be installed. 9 | 10 | ```R 11 | # Install GoTr from github 12 | devtools::install_github("MangoTheCat/GoTr") 13 | ``` 14 | ## Usage 15 | 16 | ```R 17 | library(GoTr) 18 | 19 | # Retrieve books on page 1 20 | books_page1 <- got_api(type = "books") 21 | 22 | # Retrieve books on page 1, change page size to 20. Default is 10. 23 | booksAll <- got_api(type = "books", query = list(pageSize="20")) 24 | 25 | # Retrieve characters on page 3, change page size to 20. 26 | characters_page_3 <- got_api(type = "characters", query = list(page = "3", pageSize="20")) 27 | 28 | # Retrieve character Jon Snow by id 29 | jon_snow <- got_api(type = "characters", id = "583") 30 | 31 | # Retrieve character Jon Snow by name 32 | jon_snow_by_name <- got_api(type = "characters", query = list(name = "Jon Snow")) 33 | 34 | # Retrieve house id 378 35 | house378 <- got_api(type = "houses", id = 378) 36 | ``` 37 | 38 | 39 | -------------------------------------------------------------------------------- /man/got_api.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/got_api.R 3 | \name{got_api} 4 | \alias{got_api} 5 | \title{Retrieves books, characters or houses information from the anapioficeandfire API} 6 | \usage{ 7 | got_api(type = c("books", "characters", "houses"), id = NULL, url = NULL, 8 | ...) 9 | } 10 | \arguments{ 11 | \item{type}{The endpoint type, one of books, characters or houses. 12 | Default to books if nothing is provided.} 13 | 14 | \item{id}{The object index. Default is all unless an id is specified.} 15 | 16 | \item{...}{Additional parameters to pass on to \code{GET} such as query, path, etc. 17 | User can also filter by the parameters of interest like name of a character.} 18 | } 19 | \value{ 20 | List with all parameters of that endpoint 21 | } 22 | \description{ 23 | A book is a book that are already published or planned for future. 24 | A Character is an individual within the Ice And Fire universe. 25 | A House is a house branch within the Ice And Fire universe. 26 | } 27 | \examples{ 28 | \dontrun{ 29 | 30 | # Retrieve books on page 1 31 | books_page1 <- got_api(type = "books") 32 | 33 | # Retrieve books on page 1, change page size to 20. Default is 10. 34 | booksAll <- got_api(type = "books", query = list(pageSize="20")) 35 | 36 | # Retrieve characters on page 3, change page size to 20. 37 | characters_page_3 <- got_api(type = "characters", query = list(page = "3", pageSize="20")) 38 | 39 | # Retrieve character Jon Snow by id 40 | jon_snow <- got_api(type = "characters", id = "583") 41 | 42 | # Retrieve character Jon Snow by name 43 | jon_snow_by_name <- got_api(type = "characters", query = list(name = "Jon Snow")) 44 | 45 | # Retrieve house id 378 46 | house378 <- got_api(type = "houses", id = 378) 47 | } 48 | } 49 | \author{ 50 | Mango Solutions 51 | } 52 | -------------------------------------------------------------------------------- /R/got_api.R: -------------------------------------------------------------------------------- 1 | #' Retrieves books, characters or houses information from the anapioficeandfire API 2 | #' 3 | #' A book is a book that are already published or planned for future. 4 | #' A Character is an individual within the Ice And Fire universe. 5 | #' A House is a house branch within the Ice And Fire universe. 6 | #' 7 | #' @param type The endpoint type, one of books, characters or houses. 8 | #' Default to books if nothing is provided. 9 | #' @param id The object index. Default is all unless an id is specified. 10 | #' @param ... Additional parameters to pass on to \code{GET} such as query, path, etc. 11 | #' User can also filter by the parameters of interest like name of a character. 12 | #' @author Mango Solutions 13 | #' @return List with all parameters of that endpoint 14 | #' @examples 15 | #' \dontrun{ 16 | #' 17 | #' # Retrieve books on page 1 18 | #' books_page1 <- got_api(type = "books") 19 | #' 20 | #' # Retrieve books on page 1, change page size to 20. Default is 10. 21 | #' booksAll <- got_api(type = "books", query = list(pageSize="20")) 22 | #' 23 | #' # Retrieve characters on page 3, change page size to 20. 24 | #' characters_page_3 <- got_api(type = "characters", query = list(page = "3", pageSize="20")) 25 | #' 26 | #' # Retrieve character Jon Snow by id 27 | #' jon_snow <- got_api(type = "characters", id = "583") 28 | #' 29 | #' # Retrieve character Jon Snow by name 30 | #' jon_snow_by_name <- got_api(type = "characters", query = list(name = "Jon Snow")) 31 | #' 32 | #' # Retrieve house id 378 33 | #' house378 <- got_api(type = "houses", id = 378) 34 | #' } 35 | #' @export 36 | got_api <- function(type = c("books", "characters", "houses"), 37 | id = NULL, 38 | url = NULL, 39 | ...) { 40 | 41 | # Check if url, modify from input type and id from if not 42 | if (is.null(url)) { 43 | type <- match.arg(type) 44 | path <- paste("/api", type, id, sep = "/") 45 | url <- modify_url("https://anapioficeandfire.com", path = path) 46 | } 47 | 48 | # Set up user agent 49 | ua <- user_agent("Gotr - https://github.com/mangothecat") 50 | 51 | # ------------------- 52 | # Send a GET request 53 | resp <- GET(url, ua, ...) 54 | if (http_type(resp) != "application/json") { 55 | stop("API did not return json", call. = FALSE) 56 | } 57 | 58 | # ------------------- 59 | # Turn API errors into R errors 60 | if (http_error(resp)) { 61 | stop( 62 | sprintf( 63 | "anapioficeandfire API request failed [%s]\n%s\n<%s>", 64 | status_code(resp), 65 | parsed$message, 66 | parsed$documentation_url 67 | ), 68 | call. = FALSE 69 | ) 70 | } 71 | 72 | # ------------------- 73 | # Parse JSON object 74 | parsed <- jsonlite::fromJSON(content(resp, "text"), simplifyVector = FALSE) 75 | 76 | return(parsed) 77 | 78 | } 79 | --------------------------------------------------------------------------------