Class: CongregaPlenum::CongressmenService
- Inherits:
-
Object
- Object
- CongregaPlenum::CongressmenService
- Defined in:
- lib/modules/congressmen_service.rb,
sig/congrega_plenum.rbs
Overview
Service responsible for interacting with congressmen endpoints, wrapping both list and detail requests into high level helpers.
Constant Summary collapse
- ITEMS_PER_PAGE =
Default page size for list endpoints.
100- PROGRESS_INTERVAL =
Interval used to emit progress logs while iterating.
50- SERVICE_TAG =
Tag prefix for structured logging.
'CongregaPlenum::CongressmenService'
Class Method Summary collapse
-
.api_get(endpoint, params = {}) ⇒ payload_hash
Wraps CongregaPlenum::Client#get to add service context without hiding request failures.
-
.api_get_paginated(endpoint, params = {}) ⇒ payload_list
Same as CongressmenService.api_get, but for bulk pagination calls.
-
.client ⇒ CongregaPlenum::Client
Shared client instance configured via configure.
-
.fetch_all ⇒ Array<Hash>
Fetches the entire list of congressmen regardless of legislature.
-
.fetch_all_by_legislature(legislature_id) ⇒ Array<Hash>
Fetches all congressmen for a given legislature, enriching each entry with its detailed payload.
-
.fetch_by_id(deputy_id) ⇒ Hash?
Retrieves the detailed payload of a single congressman.
-
.fetch_list(page: 1, items_per_page: ITEMS_PER_PAGE, legislature_id: nil) ⇒ Array<Hash>
Returns a paginated list of congressmen straight from the API, without making detail calls.
Class Method Details
.api_get(endpoint, params = {}) ⇒ payload_hash
Wraps CongregaPlenum::Client#get to add service context without hiding request failures.
23 24 25 26 27 28 |
# File 'lib/modules/congressmen_service.rb', line 23 def api_get(endpoint, params = {}) client.get(endpoint, params) rescue StandardError => e log_error("Erro ao acessar API em #{endpoint}: #{e.}") raise end |
.api_get_paginated(endpoint, params = {}) ⇒ payload_list
Same as api_get, but for bulk pagination calls. Failures are propagated so consumers can abort an incomplete synchronization.
32 33 34 35 36 37 |
# File 'lib/modules/congressmen_service.rb', line 32 def api_get_paginated(endpoint, params = {}) client.get_paginated(endpoint, params) rescue StandardError => e log_error("Erro paginando API em #{endpoint}: #{e.}") raise end |
.client ⇒ CongregaPlenum::Client
Shared client instance configured via CongregaPlenum.configure.
18 19 20 |
# File 'lib/modules/congressmen_service.rb', line 18 def client @client ||= CongregaPlenum::Client.instance end |
.fetch_all ⇒ Array<Hash>
Fetches the entire list of congressmen regardless of legislature.
42 43 44 |
# File 'lib/modules/congressmen_service.rb', line 42 def fetch_all fetch_deputies_collection(context: 'todos os deputados', params: { itens: ITEMS_PER_PAGE }) end |
.fetch_all_by_legislature(legislature_id) ⇒ Array<Hash>
Fetches all congressmen for a given legislature, enriching each entry with its detailed payload.
51 52 53 54 55 56 |
# File 'lib/modules/congressmen_service.rb', line 51 def fetch_all_by_legislature(legislature_id) fetch_deputies_collection( context: "legislatura #{legislature_id}", params: { itens: ITEMS_PER_PAGE, idLegislatura: legislature_id } ) end |
.fetch_by_id(deputy_id) ⇒ Hash?
Retrieves the detailed payload of a single congressman.
63 64 65 66 67 68 |
# File 'lib/modules/congressmen_service.rb', line 63 def fetch_by_id(deputy_id) log_debug("Buscando deputado #{deputy_id}") response = api_get("deputados/#{deputy_id}") extract_detail(response, "deputado #{deputy_id}") end |
.fetch_list(page: 1, items_per_page: ITEMS_PER_PAGE, legislature_id: nil) ⇒ Array<Hash>
Returns a paginated list of congressmen straight from the API, without making detail calls.
77 78 79 80 81 82 83 84 85 |
# File 'lib/modules/congressmen_service.rb', line 77 def fetch_list(page: 1, items_per_page: ITEMS_PER_PAGE, legislature_id: nil) log_debug("Buscando lista de deputados página #{page}") params = { pagina: page, itens: items_per_page } params[:idLegislatura] = legislature_id if legislature_id response = api_get('deputados', params) response['dados'] || [] end |