Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rOpenGov/pxweb
Browse files Browse the repository at this point in the history
  • Loading branch information
antagomir committed Oct 3, 2018
2 parents 6760b9a + 7b5d15b commit 54966f2
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ sandbox/*.R
sandbox

README.md
README.Rmd


# Git related
./.git*

# Travis scripts
# Travis yml
.travis.yml

# Appveyor yml
appveyor.yml

# Extra Vignette materials
vignettes/pxweb.md

Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(get_pxweb_levels)
export(get_pxweb_metadata)
export(interactive_pxweb)
export(pxweb_api)
export(update_pxweb_apis)
import(RJSONIO)
import(data.table)
import(httr)
Expand Down
97 changes: 95 additions & 2 deletions R/api_catalogue.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ api_catalogue <- function(){
return(api_catalogue)
}

#' Download and update the PXWEB API catalogue
#'
#' @details
#' Downloads the latest api catalogue from github.
#'
#' @export
update_pxweb_apis <- function(){
api_list <- get_api_list(raw = TRUE)
api_remote_list <- get_api_list_remote(raw = TRUE)
api_list$apis <- api_remote_list$apis
write_api_list(api_list = api_list)
message("pxweb api catalogue updated (temporary).")
}

#' Check and compare differences between remote and local api catalogue
#' @keywords internal
check_new_pxweb_apis <- function(){
api_local_name <- names(get_api_list(raw = TRUE)$apis)
api_remote_name <- try(names(get_api_list_remote(raw = TRUE)$apis), silent = TRUE)
if(class(api_remote_name) == "try-error") return(NULL)

remote_in_local <- api_remote_name %in% api_local_name
if(!all(remote_in_local)){
return(paste0("New PXWEB API(s):\n - ",
paste(api_remote_name[!remote_in_local], collapse = "\n - "),
"\n\nUse update_pxweb_apis() to update the api catalogue."))
}
}


#' Get (hard coded) api catalogue github urls
#'
#' @param type which type of github url is needed ('github_api' or 'github_raw').
#'
#' @keywords internal
#'
get_github_api_urls <- function(type){
if(type == "github_api") {
return("https://api.github.com/repos/rOpenGov/pxweb/contents/inst/extdata/api.json?ref=master")
# warning("api_fix")
# return("https://api.github.com/repos/rOpenGov/pxweb/contents/inst/extdata/api.json?ref=api_fix")
}
if(type == "github_raw"){
return("https://raw.githubusercontent.com/rOpenGov/pxweb/master/inst/extdata/api.json")
# warning("api_fix")
# return("https://raw.githubusercontent.com/rOpenGov/pxweb/api_fix/inst/extdata/api.json")
}
stop("No correct type")
}


#' Get the api catalogue from json file
Expand All @@ -27,13 +76,32 @@ api_catalogue <- function(){
#' @return api_list object
#'
get_api_list <- function(raw = FALSE){
api_file <- system.file("extdata/api.json", package = "pxweb")
api_file <- get_api_file_path()
api_raw <- RJSONIO::fromJSON(api_file)
if(raw) return(api_raw)
api_list <- c(api_raw$apis, api_raw$local_apis)
api_list
}

#' Get the api catalogue from github
#'
#' @inheritParams get_api_list
#'
#' @keywords internal
#'
#' @return api_list object
#'
get_api_list_remote <- function(raw = FALSE){
url_raw <- get_github_api_urls(type = "github_raw")
request <- httr::GET(url_raw)
httr::stop_for_status(request)
api_raw <-
httr::content(request, "parsed", "application/json", simplifyVector = TRUE)
if(raw) return(api_raw)
api_list <- c(api_raw$apis, api_raw$local_apis)
api_list
}



#' Write api catalogue to json file
Expand All @@ -42,7 +110,7 @@ get_api_list <- function(raw = FALSE){
#'
#' @param api_list api list in raw format to write to json file.
write_api_list <- function(api_list){
dest <- system.file("extdata/api.json", package = "pxweb")
dest <- get_api_file_path()
writeLines(RJSONIO::toJSON(api_list), con = dest)
}

Expand All @@ -63,3 +131,28 @@ get_api_index<- function(api_name, api_list){
if(sum(in_list) == 0) stop("API do not exist in api catalogue.")
which(in_list)[1]
}



#' The path to store paths to api.json
#' @keywords internal
temp_api_file_path <- function(){
file.path(temp_api_folder_path(), "api.json")
}

#' The path to the folder to store paths to api.json
#' @keywords internal
temp_api_folder_path <- function(){
file.path(tempdir(), "pxweb")
}

#' Refactorized function to get api_file path and copy if needed
#' @keywords internal
get_api_file_path <- function(){
api_file <- temp_api_file_path()
if(!file.exists(api_file)){
dir.create(temp_api_folder_path(), recursive = TRUE)
file.copy(system.file("extdata/api.json", package = "pxweb"), api_file)
}
api_file
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pxweb
======

[![Build Status](https://travis-ci.org/rOpenGov/pxweb.svg?branch=master)](https://travis-ci.org/rOpenGov/pxweb)
[![Build Status](https://travis-ci.org/rOpenGov/pxweb.svg?branch=master)](https://travis-ci.org/rOpenGov/pxweb) [![Build status](https://ci.appveyor.com/api/projects/status/40abe0fpxw2jftf3/branch/master?svg=true)](https://ci.appveyor.com/project/MansMeg/pxweb/branch/master)
[![Stories in Ready](https://badge.waffle.io/ropengov/pxweb.png?label=TODO)](http://waffle.io/ropengov/pxweb)
[![Coverage Status](https://coveralls.io/repos/rOpenGov/pxweb/badge.svg)](https://coveralls.io/r/rOpenGov/pxweb) [![rstudio mirror downloads](http://cranlogs.r-pkg.org/badges/grand-total/pxweb)](https://github.com/metacran/cranlogs.app)
[![cran version](http://www.r-pkg.org/badges/version/pxweb)](http://cran.rstudio.com/web/packages/pxweb)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pxweb
======

[![Build Status](https://travis-ci.org/rOpenGov/pxweb.svg?branch=master)](https://travis-ci.org/rOpenGov/pxweb)
[![Build Status](https://travis-ci.org/rOpenGov/pxweb.svg?branch=master)](https://travis-ci.org/rOpenGov/pxweb) [![Build status](https://ci.appveyor.com/api/projects/status/40abe0fpxw2jftf3/branch/master?svg=true)](https://ci.appveyor.com/project/MansMeg/pxweb/branch/master)
[![Stories in Ready](https://badge.waffle.io/ropengov/pxweb.png?label=TODO)](http://waffle.io/ropengov/pxweb)
[![Coverage Status](https://coveralls.io/repos/rOpenGov/pxweb/badge.svg)](https://coveralls.io/r/rOpenGov/pxweb) [![rstudio mirror downloads](http://cranlogs.r-pkg.org/badges/grand-total/pxweb)](https://github.com/metacran/cranlogs.app)
[![cran version](http://www.r-pkg.org/badges/version/pxweb)](http://cran.rstudio.com/web/packages/pxweb)
Expand Down
45 changes: 45 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# DO NOT CHANGE the "init" and "install" sections below

# Download script file from GitHub
init:
ps: |
$ErrorActionPreference = "Stop"
Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1"
Import-Module '..\appveyor-tool.ps1'
install:
ps: Bootstrap

cache:
- C:\RLibrary

# Adapt as necessary starting from here

build_script:
- travis-tool.sh install_deps

test_script:
- travis-tool.sh run_tests

on_failure:
- 7z a failure.zip *.Rcheck\*
- appveyor PushArtifact failure.zip

artifacts:
- path: '*.Rcheck\**\*.log'
name: Logs

- path: '*.Rcheck\**\*.out'
name: Logs

- path: '*.Rcheck\**\*.fail'
name: Logs

- path: '*.Rcheck\**\*.Rout'
name: Logs

- path: '\*_*.tar.gz'
name: Bits

- path: '\*_*.zip'
name: Bits
12 changes: 12 additions & 0 deletions man/check_new_pxweb_apis.Rd

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

12 changes: 12 additions & 0 deletions man/get_api_file_path.Rd

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

18 changes: 18 additions & 0 deletions man/get_api_list_remote.Rd

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

15 changes: 15 additions & 0 deletions man/get_github_api_urls.Rd

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

12 changes: 12 additions & 0 deletions man/temp_api_file_path.Rd

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

12 changes: 12 additions & 0 deletions man/temp_api_folder_path.Rd

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

14 changes: 14 additions & 0 deletions man/update_pxweb_apis.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-get_pxweb_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test_that(desc="baseURL 1",{

api_tests_get_pxweb_metadata_baseURL <- list(
list(baseURL = paste0(pxweb_api$new("api.scb.se")$base_url(language = "sv"), "/ssd"),
test_dims = c(22, 4)),
test_dims = c(21, 4)),
list(baseURL = paste0(pxweb_api$new("api.scb.se")$base_url(), "/ssd"),
test_dims = c(17, 4))
)
Expand Down

0 comments on commit 54966f2

Please sign in to comment.