Skip to content

Commit

Permalink
add test suite (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeper committed May 28, 2016
1 parent 1938d74 commit 4a5598f
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 37 deletions.
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Package: imguR
Type: Package
Title: An Imgur.com API Client Package
Version: 1.0.3
Date: 2016-03-29
Version: 1.0.4
Date: 2016-05-28
Authors@R: c(person("Thomas J.", "Leeper", role = c("aut", "cre"),
email = "[email protected]"),
person("Aaron", "Statham", role = c("ctb"),
email = "[email protected]"),
person("Yihui", "Xie", role = c("ctb"))
)
Maintainer: Thomas J. Leeper <[email protected]>
Imports: httr, png, jpeg, tools
Imports: tools, graphics, grDevices, httr, png, jpeg
Suggests: testthat
Description: A complete API client for the image hosting service Imgur.com, including the an imgur graphics device, enabling the easy upload and sharing of plots.
License: GPL-3
URL: https://github.com/leeper/imguR
BugReports: https://github.com/leeper/imguR/issues
URL: https://github.com/cloudyr/imguR
BugReports: https://github.com/cloudyr/imguR/issues
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ export(set_album_images)
export(update_album)
export(update_image)
export(upload_image)
export(vote_comment)
export(vote_comment)
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# CHANGES TO imguR 1.0.3 #

* Added a test suite and travis-CI testing. (#7)
* Migrated imguR to the cloudyr project.

# CHANGES TO imguR 1.0.2 #

* Fixed an error related to passing headers through httr functions. (#6)
Expand Down
4 changes: 2 additions & 2 deletions R/default_memes.r
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_memes <-
function(...){
out <- imgurGET('memegen/defaults', ...)
lapply(out, `class<-`, 'imgur_image')
}
structure(lapply(out, `class<-`, 'imgur_image'), class = 'imgur_gallery_album')
}
2 changes: 1 addition & 1 deletion R/delete_image.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ function(id, ...){
id <- id$id
out <- imgurDELETE(paste0('image/', id), ...)
structure(out, class = 'imgur_basic')
}
}
2 changes: 1 addition & 1 deletion R/get_album.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ function(album, ...){
out <- imgurGET(paste0('album/', album), ...)
out[['images']] <- lapply(out[['images']], `class<-`, 'imgur_image')
structure(out, class = 'imgur_album')
}
}
2 changes: 1 addition & 1 deletion R/get_album_images.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ function(album, ...){
if(inherits(album, 'imgur_album') || inherits(album, 'imgur_gallery_album'))
album <- album$id
out <- imgurGET(paste0('album/', album, '/images'), ...)
structure(out, class = 'imgur_image')
lapply(out, structure, class = 'imgur_image')
}
2 changes: 1 addition & 1 deletion R/get_memes_gallery.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function(sort = 'viral',
if(!is.null(window))
stopifnot(window %in% c('day', 'week', 'month', 'year', 'all'))
stopifnot(sort %in% c('viral', 'time', 'top'))
stopifnot(!is.numeric(as.numeric(page)))
stopifnot(is.numeric(as.numeric(page)))
out <- imgurGET(paste0('gallery/g/memes/', sort,
ifelse(!is.null(window), paste0('/', window, '/'),
'/'),
Expand Down
2 changes: 1 addition & 1 deletion R/onLoad.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.onLoad <- function(libname, pkgname){
options(imgur_user_rate_warning = 20)
options(imgur_client_rate_warning = 100)
}
}
4 changes: 2 additions & 2 deletions R/plot.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ plot.imgur_image <-
plot.imgur_gallery_image <-
function(x, ...) {
ext <- tolower(file_ext(x$link))
if(ext == 'gif') {
if (ext == 'gif') {
stop("plot method for GIF images not currently suported")
} else {
readfun <- switch(ext, 'jpg' = readJPEG,
'jpeg' = readJPEG,
'png' = readPNG,
stop('Unrecognized file extension.'))
contents <- GET(x$link)
out <- do.call(readfun, list(contents))
out <- do.call(readfun, list(content(contents, "raw")))
plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxt='n', yaxt='n',
xlab = "", ylab = "", main = "",
xaxs='i', yaxs='i', mar=rep(0,4), mgp=rep(0,3), ...)
Expand Down
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# imguR Package #
# imguR #

**imguR** enables intuitive uploading of plots created in R to the free image hosting service [Imgur](http://www.imgur.com) simply, quickly, and intuitively via the [Imgur v3 API](http://api.imgur.com). The package is a complete client library for the Imgur API, meaning that one can also delete, favorite, and comment on images, as well as organize them in albums and galleries, among other operations.

Expand All @@ -9,7 +9,7 @@ By default, all images are loaded to Imgur anonymously. Optionally, using an OAu

The two workhorse functions for anonymously uploading images to Imgur are `imgur` and `imgur_off`, the latter of which wraps `dev.off` and completes the image upload.

```
```R
library('imguR')
i <- imgur('pdf') # first argument to `imgur` is any graphics device function
plot(x=1:10, y=-1:-10, col=1:10, pch=19, main="oh hai dere")
Expand All @@ -19,7 +19,7 @@ imgur_off(i)

To upload images to a user account, first complete an OAuth login (see next section) and then pass the OAuth2.0 token object to `imgur`, e.g.:

```
```R
tkn <- imgur_login()
i <- imgur(token = tkn)
hist(rnorm(100))
Expand All @@ -33,7 +33,7 @@ By default, all operations in **imguR** are performed anonymously using an API k

Generating an OAuth2.0 token is easy in an interactive session using `imgur_login()` (and completing a user account login in your web browser on a screen [that looks like this](http://i.imgur.com/DgqMUeq.png)), the response of which is a Reference Class object that can be passed to all package functions. For example:

```
```R
# login
tkn <- imgur_login()

Expand All @@ -52,14 +52,14 @@ The OAuth2.0 token is moderately long lived. You can see when it expires by look

The ability to refresh the OAuth2.0 token also means that it is possible to use the token non-interactively. First, generate a token and save it locally (with the default `cache = TRUE` argument to `imgur_login`):

```
```R
# generate a token interactively
tkn <- imgur_login()
```

Then, reload (using httr's built-in OAuth caching mechanism) and optionally refresh the token in the non-interactive session before performing any operations:

```
```R
tkn <- imgur_login()
# tkn$refresh() # refresh token, if expired
i <- imgur(token = tkn)
Expand All @@ -74,7 +74,7 @@ Note: `imgur_login` uses OAuth credentials registered to the package maintainer.

If operations (e.g., `upload_image`) are performed with a `token` argument, they can subsequently be modified (e.g., `update_image`) by passing the relevant identifiers:

```
```R
i <- imgur(token = tkn)
hist(rnorm(100))
u <- imgur_off(i)
Expand All @@ -88,7 +88,7 @@ update_image(u$id, title = 'My graph', description = 'A simple graph', token = t

If you tried to perform the `update_image` operation without passing a `token` argument, the operation would fail. Similarly, if an image is uploaded anonymously, it is not possibly to anonymously modify it. Instead, the `imgur_image` response object (a simple list) will include a `deletehash` element, which is essentially a private key for the image. This can be used to modify the image:

```
```R
i <- imgur()
hist(rnorm(100))
u <- imgur_off(i)
Expand All @@ -101,10 +101,10 @@ This procedure can also be used for other API operations, such as modifying albu

## Requirements and Installation ##

[![CRAN Version](http://www.r-pkg.org/badges/version/imguR)](http://cran.r-project.org/package=imguR)
[![CRAN Version](http://www.r-pkg.org/badges/version/imguR)](https://cran.r-project.org/package=imguR)
![Downloads](http://cranlogs.r-pkg.org/badges/imguR)
[![Travis-CI Build Status](https://travis-ci.org/leeper/imguR.png?branch=master)](https://travis-ci.org/leeper/imguR)
[![codecov.io](http://codecov.io/github/leeper/imguR/coverage.svg?branch=master)](http://codecov.io/github/leeper/imguR?branch=master)
[![Travis-CI Build Status](https://travis-ci.org/cloudyr/imguR.png?branch=master)](https://travis-ci.org/cloudyr/imguR)
[![codecov.io](http://codecov.io/github/cloudyr/imguR/coverage.svg?branch=master)](http://codecov.io/github/cloudyr/imguR?branch=master)

The current stable version **imguR** can be installed from [CRAN](http://cran.r-project.org/package=imguR) using:

Expand All @@ -118,5 +118,7 @@ The development version can be installed directly from GitHub using `ghit`:
if(!require('ghit')) {
install.packages('ghit')
}
ghit::install_github('leeper/imguR')
ghit::install_github('cloudyr/imguR')
```

[![cloudyr project logo](http://i.imgur.com/JHS98Y7.png)](https://github.com/cloudyr)
18 changes: 9 additions & 9 deletions man/imguR.Rd
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
\name{imgur}
\alias{imgur}
\alias{imguR}
\title{Initiate an Imgur-enabled Graphics Device}
\description{Open a specified graphics device function an configure it to upload to Imgur}
\alias{imgur_off}
\title{Initiate/close an Imgur-enabled Graphics Device}
\description{Open a specified graphics device function an configure it to upload to Imgur and then close it using \code{imgur_off}.}
\usage{
imgur(device = png, file = NULL, title = NULL, description = NULL,
album = NULL, name = NULL, key = NULL, token = NULL, ...)

imgur_off(obj, ...)
}
\arguments{
\item{device}{A graphics device function. Default is \code{png}.}
Expand All @@ -16,22 +19,19 @@ imgur(device = png, file = NULL, title = NULL, description = NULL,
\item{name}{Optionally, a file name, otherwise the \code{file} filename is used.}
\item{key}{An API key. If \code{NULL}, the default imguR package API key is used.}
\item{token}{Optionally, an OAuth2.0 token (returned by \code{\link{imgur_login}}. If \code{NULL}, the API key used instead.}
\item{...}{Other arguments passed to the specified device function.}
\item{obj}{An object of class \dQuote{imgur_device}.}
\item{...}{For \code{imgur}, other arguments passed to the specified device function. For \code{imgur_off}, additional arguments passed to \code{\link{upload_image}}.}
}
\details{
This package is a wrapper for the \code{pdf} function. A temporary file is created by \code{pdf}, acting as normal
graphical device. After plotting, when \code{dev.off} is called, the file is closed, uploaded to imguR and a URL of
the files location is returned. In the case of multipaged pdfs being uploaded, imguR automatically concatenates them
into a single image.
\code{imgur} is a wrapper for a local graphics device (by default, the \code{\link[grDevices]{png}} function. A temporary file is created by the graphics device. After plotting, a call to \code{imgur_off} uploads the file to imgur (using \code{\link{upload_image}}) and a URL of the files location is returned.
}
\value{An object of class \dQuote{imgur_device} to be passed to \code{\link{imgur_off}}.}
\value{For \code{imgur}, an object of class \dQuote{imgur_device} to be passed to \code{imgur_off}. For \code{imgur_off}, the response from \code{\link{upload_image}}.}
%\references{}
\author{
Thomas J. Leeper, based on code by Aaron Statham.
}

\seealso{
\code{\link{imgur_off}}
\code{\link{upload_image}}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/update_image.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ update_image(id, title = NULL, description = NULL, ...)
\item{...}{Other arguments passed to HTTP request functions, for example: \code{token} (an OAuth2.0 token) or \code{key} (an API key, used by default).}
}
\details{Update the title or description of an already-uploaded image.}
\value{An object of class \dQuote{imgur_basic}.}
\value{A logical, with the class \dQuote{imgur_basic}.}
%\references{}
\author{Thomas J. Leeper}

Expand Down
3 changes: 3 additions & 0 deletions tests/test-all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library("testthat")
library("imguR")
test_check("imguR")
40 changes: 40 additions & 0 deletions tests/testthat/test-get-operations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
context("Basic Get Requests")

test_that("Get Album", {
expect_true(inherits(get_album("cVsXX"), "imgur_album"))
})

test_that("Get Galleries", {
expect_true(inherits(get_gallery(), "imgur_gallery_album"))
})

test_that("Search Galleries", {
expect_true(inherits(search_gallery(query = "meme"), "imgur_gallery_album"))
})

test_that("Get Album Images", {
expect_true(inherits(get_album_images("cVsXX")[[1]], "imgur_image"))
})

test_that("Get Image", {
expect_true(inherits(get_image("VI8MBMu"), "imgur_image"))
})

test_that("Plot Image", {
expect_true(inherits(plot(get_image("VI8MBMu")), "imgur_image"))
})

test_that("Get Random Images", {
expect_true(inherits(get_random_images(), "imgur_gallery_album"))
})

test_that("Get Reddit Images", {
expect_true(inherits(g <- get_reddit_gallery('dataisbeautiful'), "imgur_gallery_album"))
expect_true(inherits(get_reddit_image('dataisbeautiful', g[[1]]), "imgur_gallery_image"))
})

test_that("Get Memes", {
expect_true(inherits(default_memes(), "imgur_gallery_album"))
expect_true(inherits(get_memes_gallery('viral', page = 1), "imgur_gallery_album"))
})

31 changes: 31 additions & 0 deletions tests/testthat/test-image-upload.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
context("image upload")

test_that("image upload", {
pdf(tmpfile <- tempfile())
hist(rnorm(20))
dev.off()
expect_true(inherits(upload_image(tmpfile), "imgur_image"))
})

test_that("update image (anonymously)", {
pdf(tmpfile <- tempfile())
hist(rnorm(20))
dev.off()
expect_true(inherits(u <- upload_image(tmpfile, title = "Old Title"), "imgur_image"))
expect_true(update_image(u$deletehash, title = 'New Title'))
expect_true(get_image(u)[["title"]] == "New Title")
})

test_that("delete image (anonymously)", {
pdf(tmpfile <- tempfile())
hist(rnorm(20))
dev.off()
expect_true(inherits(u <- upload_image(tmpfile, title = "Old Title"), "imgur_image"))
expect_true(delete_image(u$deletehash))
})

test_that("imgur device", {
i <- imgur('png')
hist(rnorm(20))
expect_true(inherits(imgur_off(i), "imgur_image"))
})
7 changes: 7 additions & 0 deletions tests/testthat/test-ratelimits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context("Rate limits")

test_that("Rate Limits", {
expect_true(inherits(rl <- rate_limit(), "imgur_basic"))
expect_true(all(c("UserLimit", "UserRemaining", "ClientLimit", "ClientRemaining") %in% names(rl)))
})

0 comments on commit 4a5598f

Please sign in to comment.