Skip to content

Commit

Permalink
better cross-refs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Mar 27, 2021
1 parent 37e8cab commit 88d88ce
Show file tree
Hide file tree
Showing 29 changed files with 57 additions and 270 deletions.
14 changes: 8 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ export(ref_statuses)
export(ref_tidy)
export(ref_titles)
export(tidy_bom)
import(dplyr)
import(httr)
import(jsonlite)
import(stringi)
import(tibble)
import(utils)
importFrom(Rcpp,sourceCpp)
importFrom(dplyr,inner_join)
importFrom(httr,GET)
importFrom(httr,accept_json)
importFrom(httr,http_type)
importFrom(janitor,make_clean_names)
importFrom(jsonlite,fromJSON)
importFrom(purrr,discard)
importFrom(stringi,stri_trans_general)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
useDynLib(mnis, .registration = TRUE)
41 changes: 5 additions & 36 deletions R/mnis-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,13 @@
#' which allow a maximum of three parameters.
#'
#'
#' @section mnis functions:
#'
#' [mnis_additional()]
#'
#' [mnis_all_members()]
#'
#' [mnis_base()]
#'
#' [mnis_constituency_results()]
#'
#' [mnis_department()]
#'
#' [mnis_eligible()]
#'
#' [mnis_extra()]
#'
#' [mnis_full_biog()]
#'
#' [mnis_general_election_results()]
#'
#' [mnis_joined_between()]
#'
#' [mnis_lords_type()]
#'
#' [mnis_member_date()]
#'
#' [mnis_party_state()]
#'
#' [mnis_reference()]
#'
#' @docType package
#' @name mnis
#' @import utils
#' @import httr
#' @import jsonlite
#' @import dplyr
#' @import tibble
#' @import stringi
#' @importFrom httr GET http_type accept_json
#' @importFrom jsonlite fromJSON
#' @importFrom dplyr inner_join
#' @importFrom tibble as_tibble tibble
#' @importFrom stringi stri_trans_general
#' @importFrom janitor make_clean_names
#' @importFrom purrr discard
#' @useDynLib mnis, .registration = TRUE
Expand Down
1 change: 0 additions & 1 deletion R/mnis_additional.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#' Members' name (e.g. surname, forename, Honorary prefixes, full
#' details of House of Lords title and rank if applicable, etc...)
#' @seealso [mnis_full_biog()]
#' @seealso [mnis_extra()]
#' @examples
#' \dontrun{
#' x <- mnis_basic_details(172)
Expand Down
10 changes: 1 addition & 9 deletions R/mnis_all_members.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@ mnis_all_members <- function(house = "all", party = NULL,

query <- paste0(baseurl, house, party, "/HouseMemberships/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- got$Members$Member

Expand Down
1 change: 0 additions & 1 deletion R/mnis_all_reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#' Defaults to "snake_case".
#' @export
#'
#' @seealso [mnis_reference()]
#' @rdname mnis_all_reference
#' @examples
#' \dontrun{
Expand Down
10 changes: 1 addition & 9 deletions R/mnis_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ mnis_base <- function(request) {

query <- paste0(baseurl, request)

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- do.call(rbind, got$Members$Member)
}
10 changes: 1 addition & 9 deletions R/mnis_constituency_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ mnis_constituency_results <- function(constituency_id = NULL, election_id = 0,
as.character(constituency_id), "/",
as.character(election_id), "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

details <- got$Constituency$Details

Expand Down
17 changes: 2 additions & 15 deletions R/mnis_department.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@

#' mnis_department
#'
#' Request the holders of posts in specific departments by department ID
#' (see [mnis_reference()]), or all current and former
#' minister/shadow minister positions.
#' Request the holders of posts in specific departments by department ID.
#'
#' @param department_id The department to look up. `0` returns the
#' cabinet/shadow cabinet, `-1` returns a list of all ministers.
#' For departmental IDs, see the `ref_departments` function in
#' [mnis_reference()]. Defaults to `0`.
#' @param bench Flag to return either Government or Opposition information.
#' This parameter is case insensitive, so both `'Opposition'` and
#' `'opposition'` will return the same data.
Expand All @@ -20,7 +16,6 @@
#' @return A tibble with information on departments and
#' ministers/shadow ministers.
#' @export
#' @seealso [mnis_reference()]
#' @examples
#' \dontrun{
#' x <- mnis_department(department_id = 0, bench = "Government", former = TRUE)
Expand All @@ -38,15 +33,7 @@ mnis_department <- function(department_id = 0, bench = "Government",
query <- paste0(base_url, "Department/", as.character(department_id),
"/", utils::URLencode(bench), "/", query_former, "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- tibble::as_tibble(as.data.frame(got$Department$Posts))

Expand Down
10 changes: 1 addition & 9 deletions R/mnis_eligible.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@ mnis_eligible <- function(eligible = TRUE, house = "all", party = NULL,
query <- paste0(base_url, "members/query/iseligible=", eligible,
house_query, party)

got <- httr::GET(query, httr::accept_json(), encoding = "UTF-8")

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- tibble::as_tibble(got$Members$Member)

Expand Down
11 changes: 1 addition & 10 deletions R/mnis_general_election_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#' @return Returns a list with details of the search parameter and
#' a tibble with election results.
#' @export
#' @seealso [mnis_reference()]
#' @examples
#' \dontrun{
#' x <- mnis_general_election_results(
Expand All @@ -42,15 +41,7 @@ mnis_general_election_results <- function(location_type = "Country",
utils::URLencode(location_name), "/", as.Date(start_date),
"/", as.Date(end_date), "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- got$ElectionResults

Expand Down
17 changes: 5 additions & 12 deletions R/mnis_joined_between.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#' coerced to a date with `as.Date()`. Defaults to the current system date.
#' @param house The house to which the member belongs. Accepts one of
#' `'all'`, `'lords'` and `'commons'`. This parameter is
#' not case sensititive. Defaults to `'all'`.
#' not case sensitive. Defaults to `'all'`.
#' @param party All members from a given party who joined between the two
#' dates. The party name must be fully spelled out (e.g. `'green party'`),
#' the API does not accept searches on this parameter. For a tibble of
#' parties, see `ref_parties` in [mnis_reference()].
#' This parameter is not case sensititive. Defaults to `NULL`.
#' parties, see [ref_parties()].
#' This parameter is not case sensitive. Defaults to `NULL`.
#' @param eligible If the member is currently eligible to sit. Accepts
#' one of `'all'`, `'current'`, `'former'`. This parameter
#' is not case sensititive. Defaults to `'all'`.
#' is not case sensitive. Defaults to `'all'`.
#' @inheritParams mnis_basic_details
#' @return A tibble with data on all members who joined
#' between the two given dates.
Expand Down Expand Up @@ -77,14 +77,7 @@ mnis_joined_between <- function(start_date = "1900-01-01",
as.Date(start_date), "and", as.Date(end_date), house_query,
party_q, eligible_query)

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}
got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- tibble::as_tibble(got$Members$Member)

Expand Down
11 changes: 1 addition & 10 deletions R/mnis_lords_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#' @return A tibble with information on the numbers of different types
#' of Lords on a given date.
#' @export
#' @seealso [mnis_reference()]
#' @examples
#' \dontrun{
#' x <- mnis_lords_type()
Expand All @@ -24,15 +23,7 @@ mnis_lords_type <- function(date = Sys.Date(), tidy = TRUE,

query <- paste0(base_url, "LordsByType/", as.Date(date), "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- tibble::as_tibble(as.data.frame(got$LordsByType))

Expand Down
10 changes: 1 addition & 9 deletions R/mnis_member_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ mnis_member_date <- function(ID = NULL, date = Sys.Date(),
query <- paste0(base_url, "member/historical/", as.character(ID),
"/", as.Date(date), "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- as.list(got$Member)

Expand Down
10 changes: 1 addition & 9 deletions R/mnis_mps_on_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,7 @@ mnis_mps_on_date <- function(date1 = Sys.Date(), date2 = NULL,
"commonsmemberbetween=", min(date_vec), "and",
max(date_vec), "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

mps <- got$Members$Member

Expand Down
10 changes: 1 addition & 9 deletions R/mnis_party_state.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ mnis_party_state <- function(house = "Commons", date = Sys.Date(),
tidy = TRUE, tidy_style = "snake_case") {
query <- paste0(base_url, "houseOverview/", house, "/", date, "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

x <- tibble::as_tibble(got$HouseOverview$Party)

Expand Down
10 changes: 1 addition & 9 deletions R/mnis_peers_on_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@ mnis_peers_on_date <- function(date1 = Sys.Date(), date2 = NULL,
query <- paste0(base_url, "members/query/House=Lords|Membership=all|",
"lordsmemberbetween=", date1, "and", date2, "/")

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

lords <- got$Members$Member

Expand Down
14 changes: 3 additions & 11 deletions R/mnis_political_interests.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,13 @@ mnis_political_interests <- function(interest, house = NULL, current = TRUE,

query <- paste0(base_url, "members/query/", interest_query, house_query)

got <- httr::GET(query, httr::accept_json())

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query)

if (length(got$Members$Member$`@Member_Id`) <= 1) { ## fails

df <- tibble::as.tibble(t(unlist(got$Members$Member)))
df <- tibble::as_tibble(t(unlist(got$Members$Member)))
} else {
df <- tibble::as.tibble(got$Members$Member)
df <- tibble::as_tibble(got$Members$Member)
}

df <- mnis_tidy(df, tidy_style)
Expand Down
12 changes: 1 addition & 11 deletions R/mnis_reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,8 @@ ref_parties <- function(tidy = TRUE, tidy_style = "snake_case") {
#' @export
#' @rdname mnis_reference
ref_party_sub_types <- function(tidy = TRUE, tidy_style = "snake_case") {
got <- httr::GET(
paste0(base_url, "ReferenceData/PartySubTypes/"),
httr::accept_json()
)

if (httr::http_type(got) != "application/json") {
stop("API did not return json", call. = FALSE)
}

got <- mnis::tidy_bom(got)

got <- jsonlite::fromJSON(got, flatten = TRUE)
got <- mnis_query(query = paste0(base_url, "ReferenceData/PartySubTypes/"))

x <- as.list(got$PartySubTypes$PartySubType)

Expand Down
Loading

0 comments on commit 88d88ce

Please sign in to comment.