Class: CongregaPlenum::CongressmenService

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Integer)
100
PROGRESS_INTERVAL =

Interval used to emit progress logs while iterating.

Returns:

  • (Integer)
50
SERVICE_TAG =

Tag prefix for structured logging.

Returns:

  • (String)
'CongregaPlenum::CongressmenService'

Class Method Summary collapse

Class Method Details

.api_get(endpoint, params = {}) ⇒ payload_hash

Wraps CongregaPlenum::Client#get to add service context without hiding request failures.

Parameters:

  • (String)
  • (params_hash)

Returns:

  • (payload_hash)


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.message}")
  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.

Parameters:

  • (String)
  • (params_hash)

Returns:

  • (payload_list)


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.message}")
  raise
end

.clientCongregaPlenum::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_allArray<Hash>

Fetches the entire list of congressmen regardless of legislature.

Returns:

  • (Array<Hash>)


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.

Parameters:

  • legislature_id (Integer)
  • (Integer)

Returns:

  • (Array<Hash>)


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.

Parameters:

  • deputy_id (Integer)
  • (Integer)

Returns:

  • (Hash, nil)

Raises:



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.

Parameters:

  • page (Integer) (defaults to: 1)
  • items_per_page (Integer) (defaults to: ITEMS_PER_PAGE)
  • legislature_id (Integer, nil) (defaults to: nil)
  • page: (Integer) (defaults to: 1)
  • items_per_page: (Integer) (defaults to: ITEMS_PER_PAGE)
  • legislature_id: (Integer, nil) (defaults to: nil)

Returns:

  • (Array<Hash>)


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