Skip to content

Commit

Permalink
Adiciona o fetch_votacoes_senado
Browse files Browse the repository at this point in the history
  • Loading branch information
jairNeto committed Feb 21, 2019
1 parent b7c0112 commit e454b79
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 4 deletions.
58 changes: 58 additions & 0 deletions R/colunas_constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,64 @@
"ementa"="character"
## "siglaTipo"="character","codTipo"="integer","numero"="numeric","ano"="numeric",
)
.COLNAMES_VOT_SEN <-
c(
"descricao_identificacao_materia"="character",
"indicador_votacao_secreta"="character",
"descricao_votacao"="character",
"descricao_resultado"="character",
"codigo_sessao"="character",
"sigla_casa_sessao"="character",
"nome_casa_sessao"="character",
"codigo_sessao_legislativa"="character",
"sigla_tipo_sessao"="character",
"numero_sessao"="character",
"data_sessao"="character",
"hora_inicio_sessao"="character",
"codigo_tramitacao"="character",
"numero_autuacao"="character",
"data_tramitacao"="character",
"numero_ordem_tramitacao"="character",
"texto_tramitacao"="character",
"indicador_recebimento"="character",
"data_recebimento"="character",
"origem_tramitacao_local_codigo_local"="character",
"origem_tramitacao_local_tipo_local"="character",
"origem_tramitacao_local_sigla_casa_local"="character",
"origem_tramitacao_local_nome_casa_local"="character",
"origem_tramitacao_local_sigla_local"="character",
"origem_tramitacao_local_nome_local"="character",
"destino_tramitacao_local_codigo_local"="character",
"destino_tramitacao_local_tipo_local"="character",
"destino_tramitacao_local_sigla_casa_local"="character",
"destino_tramitacao_local_nome_casa_local"="character",
"destino_tramitacao_local_sigla_local"="character",
"destino_tramitacao_local_nome_local"="character",
"situacao_codigo_situacao"="character",
"situacao_sigla_situacao"="character",
"situacao_descricao_situacao"="character",
"descricao_voto"="character",
"codigo_parlamentar"="character",
"nome_parlamentar"="character",
"nome_completo_parlamentar"="character",
"sexo_parlamentar"="character",
"forma_tratamento"="character",
"url_foto_parlamentar"="character",
"url_pagina_parlamentar"="character",
"email_parlamentar"="character",
"sigla_partido_parlamentar"="character",
"uf_parlamentar"="character",
"codigo_materia"="character",
"sigla_casa_identificacao_materia"="character",
"nome_casa_identificacao_materia"="character",
"sigla_subtipo_materia"="character",
"descricao_subtipo_materia"="character",
"descricao_identificacao_materia"="character",
"numero_materia"="character",
"ano_materia"="character",
"indicador_tramitando"="character",
"descricao_objetivo_processo"="character"
)

# Partidos
.COLNAMES_PARTIDOS <- c("id"="integer","sigla"="character","nome"="character","uri"="character")
Expand Down
1 change: 1 addition & 0 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
.DEFERIMENTO_SENADO_PATH <- "/dadosabertos/materia/movimentacoes/"
.AGENDA_SENADO_PATH <- "/dadosabertos/plenario/agenda/mes/"
.AGENDA_SENADO_COMISSOES <- "/dadosabertos/agenda/"
.SENADO_VOTACOES_PATH <- "/dadosabertos/materia/votacoes/"

# Link do repositório do rcongresso
.RCONGRESSO_LINK <- "https://github.com/analytics-ufcg/rcongresso"
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ rename_table_to_underscore <- function(df) {
tramitacao_df
}

#' @title Renames the cols of the bill's voting on Senate
#' @description Renames each item from vector with the pattern: split by underscore and lowercase
#' @param df Dataframe
#' @return Dataframe containing the renamed strings.
#' @export
#' @rdname fetch_votacoes_senado
#' @export
.rename_votacoes_df <- function(df) {
new_names = names(df) %>%
.to_underscore() %>%
stringr::str_replace(
"sessao_plenaria_|tramitacao_identificacao_tramitacao_|identificacao_parlamentar_",
""
)

names(df) <- new_names

df
}

#' @title Get the author on Chamber
#' @description Return a dataframe with the link, name, code, type and house
#' @param prop_id Proposition ID
Expand Down
41 changes: 39 additions & 2 deletions R/votacoes.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @title Fetches details about a voting
#' @description Fetches details about a voting.
#' @title Fetches details about a voting on Chamber
#' @description Fetches details about a voting on Chamber.
#' @param id_votacao Voting's ID
#' @return Dataframe containing details about a voting, including tittle,
#' start voting time, finish voting time, result and approval
Expand All @@ -21,6 +21,43 @@ fetch_votacao <- function(id_votacao = NULL){
.coerce_types(.COLNAMES_VOTACAO)
}

#' @title Fetches details about a voting on Senate
#' @description Fetches details about a voting on Senate.
#' Ao fim, a função retira todos as colunas que tenham tipo lista para uniformizar o dataframe.
#' @param proposicao_id Proposition Id
#' @return Dataframe containing details about a voting on Senate
#' @examples
#' fetch_votacoes_senado(91341)
#' @rdname fetch_votacoes_senado
#' @export
fetch_votacoes_senado <- function(proposicao_id) {

json_votacoes <- .senado_api(paste0(.SENADO_VOTACOES_PATH, proposicao_id), asList = TRUE)
votacoes_data <-
json_votacoes %>%
magrittr::extract2("VotacaoMateria") %>%
magrittr::extract2("Materia")
votacoes_ids <-
votacoes_data %>%
magrittr::extract2("IdentificacaoMateria") %>%
tibble::as_tibble() %>%
unique()
votacoes_df <-
votacoes_data %>%
magrittr::extract2("Votacoes") %>%
purrr::map_df( ~ .) %>%
tidyr::unnest()

votacoes_df <-
votacoes_df %>%
tibble::add_column(!!!votacoes_ids)

votacoes_df <- votacoes_df[,!sapply(votacoes_df, is.list)]

.rename_votacoes_df(votacoes_df)
votacoes_df
}

#' @title Fetches the positions of a group on a voting
#' @description Fetch how the groups in the chamber of deputies
#' instructed their members to vote on a given voting.
Expand Down
4 changes: 2 additions & 2 deletions man/fetch_votacao.Rd

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

30 changes: 30 additions & 0 deletions man/fetch_votacoes_senado.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test_votacoes.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ orientacoes_pec241 <<- fetch_orientacoes(7252)
ultima_votacao_pec241 <<- fetch_votacoes(2088351) %>% ultima_votacao()
votos_partidos_pec241 <<- get_votos_partidos(7252)
proposicao_votacao7252 <<- fetch_proposicao_from_votacao(7252)
proposicao_votacao_senado <<- fetch_votacoes_senado(91341)


# Testa erros
Expand All @@ -22,6 +23,7 @@ test_that("Is dataframe", {
expect_true(is.data.frame(ultima_votacao_pec241))
expect_true(is.data.frame(votos_partidos_pec241))
expect_true(is.data.frame(proposicao_votacao7252))
expect_true(is.data.frame(proposicao_votacao_senado))
})

test_that("Not Empty", {
Expand All @@ -31,6 +33,11 @@ test_that("Not Empty", {
expect_true(nrow(ultima_votacao_pec241) != 0)
expect_true(nrow(votos_partidos_pec241) != 0)
expect_true(nrow(proposicao_votacao7252) != 0)
expect_true(nrow(proposicao_votacao_senado) != 0)
})

test_that("fetch_votos()", {
expect_true(all(sapply(proposicao_votacao_senado, class) %in% .COLNAMES_VOT_SEN))
})

test_that("fetch_votos()", {
Expand Down

0 comments on commit e454b79

Please sign in to comment.