Class: CongregaPlenum::LegislaturesService

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/legislatures_service.rb,
sig/congrega_plenum.rbs

Overview

Service responsible for interacting with legislature endpoints including mesa composition and deputies per legislature.

Constant Summary collapse

PAGE_SIZE =

Default pagination size for legislature listings.

Returns:

  • (Integer)
50
DEPUTIES_PAGE_SIZE =

Pagination size for deputies per legislature.

Returns:

  • (Integer)
100
SERVICE_TAG =

Tag prefix for structured logging.

Returns:

  • (String)
'CongregaPlenum::LegislaturesService'

Class Method Summary collapse

Class Method Details

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

Wraps 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/legislatures_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

Bulk variant of api_get. 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/legislatures_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/legislatures_service.rb', line 18

def client
  @client ||= CongregaPlenum::Client.instance
end

.fetch_allArray<Hash>

Returns every legislature exposed by a API.

Returns:

  • (Array<Hash>)


42
43
44
45
46
47
48
49
# File 'lib/modules/legislatures_service.rb', line 42

def fetch_all
  log_info('Iniciando busca de todas as legislaturas')

  legislatures = api_get_paginated('legislaturas', itens: PAGE_SIZE)

  log_info("Total de #{legislatures.size} legislaturas encontradas")
  legislatures
end

.fetch_by_id(legislature_id) ⇒ Hash?

Fetches a single legislature payload.

Parameters:

  • legislature_id (Integer)
  • (Integer)

Returns:

  • (Hash, nil)

Raises:



56
57
58
59
60
61
# File 'lib/modules/legislatures_service.rb', line 56

def fetch_by_id(legislature_id)
  log_debug("Buscando legislatura #{legislature_id}")

  response = api_get("legislaturas/#{legislature_id}")
  extract_detail(response, "legislatura #{legislature_id}")
end

.fetch_deputies(legislature_id) ⇒ Array<Hash>

Lists every deputy that served/serves in the given legislature using the idLegislatura filter supported by the deputies endpoint.

Parameters:

  • legislature_id (Integer)
  • (Integer)

Returns:

  • (Array<Hash>)


80
81
82
83
84
85
86
87
# File 'lib/modules/legislatures_service.rb', line 80

def fetch_deputies(legislature_id)
  log_info("Iniciando coleta de deputados da legislatura #{legislature_id}")

  deputies = api_get_paginated('deputados', itens: DEPUTIES_PAGE_SIZE, idLegislatura: legislature_id)

  log_info("Coletamos #{deputies.size} deputados para a legislatura #{legislature_id}")
  deputies
end

.fetch_mesa(legislature_id) ⇒ Array<Hash>

Retrieves the mesa composition for the provided legislature ID.

Parameters:

  • legislature_id (Integer)
  • (Integer)

Returns:

  • (Array<Hash>)


67
68
69
70
71
72
73
# File 'lib/modules/legislatures_service.rb', line 67

def fetch_mesa(legislature_id)
  response = api_get("legislaturas/#{legislature_id}/mesa")
  mesa_data = response['dados'] || []

  log_info("Mesa da legislatura #{legislature_id} coletada com #{mesa_data.size} integrantes")
  mesa_data
end