Browse Source

initial commit

master
boB Rudis 8 years ago
commit
a5f3b2c269
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 7
      .Rbuildignore
  2. 7
      .gitignore
  3. 17
      .travis.yml
  4. 25
      DESCRIPTION
  5. 7
      NAMESPACE
  6. 2
      NEWS.md
  7. 23
      R/uaparser.R
  8. 9
      R/uaparserjs-package.R
  9. 12
      R/zzz.r
  10. 49
      README.Rmd
  11. 83
      README.md
  12. 2904
      inst/js/bundle.js
  13. 2169
      inst/js/index.js
  14. 32
      man/ua_parse.Rd
  15. 14
      man/uaparserjs.Rd
  16. 1091
      tests/agents.txt
  17. 2
      tests/test-all.R
  18. 12
      tests/testthat/test-uaparserjs.R
  19. 21
      uaparserjs.Rproj

7
.Rbuildignore

@ -0,0 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^README\.Rmd$
^README\.*html$
^NOTES\.*Rmd$
^NOTES\.*html$

7
.gitignore

@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Rproj
src/*.o
src/*.so
src/*.dll

17
.travis.yml

@ -0,0 +1,17 @@
language: r
warnings_are_errors: true
sudo: required
apt_packages:
- binutils
- libproj-dev
- gdal-bin
env:
global:
- CRAN: http://cran.rstudio.com
notifications:
email:
on_success: change
on_failure: change

25
DESCRIPTION

@ -0,0 +1,25 @@
Package: uaparserjs
Type: Package
Title: Parse Browser 'User-Agent' Strings into Data Frames
Version: 0.1.0
Date: 2016-08-04
Author: Bob Rudis (@hrbrmstr)
Maintainer: Bob Rudis <bob@rudis.net>
Description: Despite there being a section in RFC 7231
<https://tools.ietf.org/html/rfc7231#section-5.5.3> defining a suggested
structure for 'User-Agent' headers this data is notoriously difficult
to parse consistently. A function is provided that will take in user agent
strings and return structured R objects. This is a 'V8'-backed package
based on the 'ua-parser' project <https://github.com/ua-parser>.
URL: http://github.com/hrbrmstr/uaparserjs
BugReports: https://github.com/hrbrmstr/uaparserjs/issues
License: AGPL
Suggests:
testthat
Depends:
R (>= 3.0.0)
Imports:
purrr,
dplyr,
V8
RoxygenNote: 5.0.1

7
NAMESPACE

@ -0,0 +1,7 @@
# Generated by roxygen2: do not edit by hand
export(ua_parse)
import(V8)
importFrom(dplyr,as_data_frame)
importFrom(purrr,map)
importFrom(purrr,map_df)

2
NEWS.md

@ -0,0 +1,2 @@
0.1.0
* Initial release

23
R/uaparser.R

@ -0,0 +1,23 @@
#' Parse a vector of user agents into a data frame
#'
#' Takes in a character vector of user agent strings and returns a \code{tibble}
#' of parsed user agents.
#'
#' @param user_agents a character vector of user agents
#' @export
#' @return a \code{tibble} with columns for user agent family, major & minor versions
#' plus patch level along with os family and major & minor versions plus
#' device brand and model.
#' @references \url{http://www.uaparser.org/}
#' @note The regex YAML import date: 2016-08-04
#' @examples
#' ua_parse(paste0("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, ",
#' "like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 ",
#' "Chrome/15.0.874.106 Safari/535.2", collapse=""))
ua_parse <- function(user_agents) {
purrr::map_df(user_agents, function(x) {
dplyr::as_data_frame(as.list(unlist(.pkgenv$ctx$call("parser.parse", x))))
})
}

9
R/uaparserjs-package.R

@ -0,0 +1,9 @@
#' Parse Browser User Agent Strings into Data Frames
#'
#' @name uaparserjs
#' @docType package
#' @author Bob Rudis (@@hrbrmstr)
#' @importFrom purrr map map_df
#' @import V8
#' @importFrom dplyr as_data_frame
NULL

12
R/zzz.r

@ -0,0 +1,12 @@
.pkgenv <- new.env(parent=emptyenv())
.onAttach <- function(...) {
ctx <- v8()
ctx$source(system.file("js/bundle.js", package="uaparserjs"))
ctx$eval(JS('var parser = uap(regexes)'))
assign("ctx", ctx, envir=.pkgenv)
}

49
README.Rmd

@ -0,0 +1,49 @@
---
output: rmarkdown::github_document
---
`uaparserjs` : Parse Browser 'User-Agent' Strings into Data Frames
Despite there being a section in RFC 7231 <https://tools.ietf.org/html/rfc7231#section-5.5.3> defining a suggested structure for `User-Agent` headers this data is notoriously difficult to parse consistently. A function is provided that will take in user agent strings and return structured R objects. This is a V8-backed package based on the `ua-parser` project <https://github.com/ua-parser>.
NOTE: there is a [C++-backed R package](https://github.com/ua-parser/uap-r) but it has [issues](https://github.com/ua-parser/uap-r/issues) compiling on a few platforms and is dependent upon Boost regex. This version is definitely slower but it works on all platforms.
The following functions are implemented:
- `ua_parse` : Parse a vector of user agents into a data frame
### Installation
```{r eval=FALSE}
devtools::install_github("hrbrmstr/uaparserjs")
```
```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE}
options(width=120)
```
### Usage
```{r}
library(uaparserjs)
# current verison
packageVersion("uaparserjs")
dplyr::glimpse(ua_parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2"))
dplyr::glimpse(ua_parse(readLines("tests/agents.txt")))
```
### Test Results
```{r}
library(uaparserjs)
library(testthat)
date()
test_dir("tests/")
```

83
README.md

@ -0,0 +1,83 @@
`uaparserjs` : Parse Browser 'User-Agent' Strings into Data Frames
Despite there being a section in RFC 7231 <https://tools.ietf.org/html/rfc7231#section-5.5.3> defining a suggested structure for `User-Agent` headers this data is notoriously difficult to parse consistently. A function is provided that will take in user agent strings and return structured R objects. This is a V8-backed package based on the `ua-parser` project <https://github.com/ua-parser>.
NOTE: there is a [C++-backed R package](https://github.com/ua-parser/uap-r) but it has [issues](https://github.com/ua-parser/uap-r/issues) compiling on a few platforms and is dependent upon Boost regex. This version is definitely slower but it works on all platforms.
The following functions are implemented:
- `ua_parse` : Parse a vector of user agents into a data frame
### Installation
``` r
devtools::install_github("hrbrmstr/uaparserjs")
```
### Usage
``` r
library(uaparserjs)
# current verison
packageVersion("uaparserjs")
```
## [1] '0.1.0'
``` r
dplyr::glimpse(ua_parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2"))
```
## Observations: 1
## Variables: 9
## $ userAgent <chr> "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/...
## $ ua.family <chr> "Chromium"
## $ ua.major <chr> "15"
## $ ua.minor <chr> "0"
## $ ua.patch <chr> "874"
## $ os.family <chr> "Ubuntu"
## $ os.major <chr> "11"
## $ os.minor <chr> "10"
## $ device.family <chr> "Other"
``` r
dplyr::glimpse(ua_parse(readLines("tests/agents.txt")))
```
## Observations: 1,091
## Variables: 13
## $ userAgent <chr> "Mozilla/5.0 (Windows; U; en-US) AppleWebKit/531.9 (KHTML, like Gecko) AdobeAIR/2.5.1", "Mozi...
## $ ua.family <chr> "AdobeAIR", "Amazon Silk", "Amazon Silk", "Amazon Silk", "Amazon Silk", "Amazon Silk", "Andro...
## $ ua.major <chr> "2", "1", "2", "2", "2", "3", "2", "2", "2", "2", "3", "4", "4", "4", "4", "1", "1", "6", "7"...
## $ ua.minor <chr> "5", "1", "0", "1", "2", "25", "2", "3", "3", "3", "0", "0", "0", "0", "0", NA, NA, "13", "6"...
## $ ua.patch <chr> "1", "0-80", NA, NA, NA, NA, "2", "3", "4", "5", "1", "3", "3", "3", "4", NA, NA, "13719", "2...
## $ os.family <chr> "Windows", "Android", "Android", "Android", "Android", "Android", "Android", "Android", "Andr...
## $ device.family <chr> "Other", "Kindle", "Kindle Fire HD", "Kindle Fire", "Kindle Fire HD", "Kindle Fire HD", "HTC ...
## $ device.brand <chr> NA, "Amazon", "Amazon", "Amazon", "Amazon", "Amazon", "HTC", "HTC", "Amazon", "Tecno", "Samsu...
## $ device.model <chr> NA, "Kindle", "Kindle Fire HD 7\"", "Kindle Fire", "Kindle Fire HD 7\"", "Kindle Fire HD 7\""...
## $ os.major <chr> NA, NA, NA, NA, NA, "4", "2", "2", "2", "2", "3", "4", "4", "4", "4", NA, NA, NA, NA, "4", "7...
## $ os.minor <chr> NA, NA, NA, NA, NA, "0", "2", "3", "3", "3", "0", "0", "0", "0", "0", NA, NA, NA, NA, "1", "0...
## $ os.patch <chr> NA, NA, NA, NA, NA, "3", "2", "3", "4", "5", "1", "3", "3", "3", "4", NA, NA, NA, NA, "1", "6...
## $ os.patchMinor <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
### Test Results
``` r
library(uaparserjs)
library(testthat)
date()
```
## [1] "Thu Aug 4 17:21:51 2016"
``` r
test_dir("tests/")
```
## testthat results ========================================================================================================
## OK: 3 SKIPPED: 0 FAILED: 0
##
## DONE ===================================================================================================================

2904
inst/js/bundle.js

File diff suppressed because it is too large

2169
inst/js/index.js

File diff suppressed because it is too large

32
man/ua_parse.Rd

@ -0,0 +1,32 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/uaparser.R
\name{ua_parse}
\alias{ua_parse}
\title{Parse a vector of user agents into a data frame}
\usage{
ua_parse(user_agents)
}
\arguments{
\item{user_agents}{a character vector of user agents}
}
\value{
a \code{tibble} with columns for user agent family, major & minor versions
plus patch level along with os family and major & minor versions plus
device brand and model.
}
\description{
Takes in a character vector of user agent strings and returns a \code{tibble}
of parsed user agents.
}
\note{
The regex YAML import date: 2016-08-04
}
\examples{
ua_parse(paste0("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, ",
"like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 ",
"Chrome/15.0.874.106 Safari/535.2", collapse=""))
}
\references{
\url{http://www.uaparser.org/}
}

14
man/uaparserjs.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/uaparserjs-package.R
\docType{package}
\name{uaparserjs}
\alias{uaparserjs}
\alias{uaparserjs-package}
\title{Parse Browser User Agent Strings into Data Frames}
\description{
Parse Browser User Agent Strings into Data Frames
}
\author{
Bob Rudis (@hrbrmstr)
}

1091
tests/agents.txt

File diff suppressed because it is too large

2
tests/test-all.R

@ -0,0 +1,2 @@
library(testthat)
test_check("uaparserjs")

12
tests/testthat/test-uaparserjs.R

@ -0,0 +1,12 @@
context("basic functionality")
test_that("we can do something", {
res <- ua_parse(paste0("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, ",
"like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 ",
"Chrome/15.0.874.106 Safari/535.2", collapse=""))
expect_that(res, is_a("data.frame"))
expect_that(res$ua.patch, equals("874"))
expect_that(res$os.family, equals("Ubuntu"))
})

21
uaparserjs.Rproj

@ -0,0 +1,21 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageRoxygenize: rd,collate,namespace
Loading…
Cancel
Save