Class: CbfCalendario::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cbf_calendario/client.rb

Constant Summary collapse

DEFAULT_BASE =
'https://www.cbf.com.br'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DEFAULT_BASE, read_timeout: 30, open_timeout: 15) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
# File 'lib/cbf_calendario/client.rb', line 24

def initialize(base_url: DEFAULT_BASE, read_timeout: 30, open_timeout: 15)
  @base_url = base_url.to_s.chomp('/')
  @read_timeout = read_timeout
  @open_timeout = open_timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



22
23
24
# File 'lib/cbf_calendario/client.rb', line 22

def base_url
  @base_url
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



22
23
24
# File 'lib/cbf_calendario/client.rb', line 22

def open_timeout
  @open_timeout
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



22
23
24
# File 'lib/cbf_calendario/client.rb', line 22

def read_timeout
  @read_timeout
end

Class Method Details

.coerce_date!(data) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/cbf_calendario/client.rb', line 122

def self.coerce_date!(data)
  case data
  when Date then data
  when Time then data.to_date
  else
    parse_data_br!(data)
  end
end

.normalize_id_atleta!(id_atleta) ⇒ Object



98
99
100
101
102
103
# File 'lib/cbf_calendario/client.rb', line 98

def self.normalize_id_atleta!(id_atleta)
  s = id_atleta.to_s.strip
  raise InvalidAthleteIdError, 'id_atleta deve conter só dígitos (ex.: 12345)' unless s.match?(/\A\d+\z/)

  s
end

.normalize_id_clube!(id_clube) ⇒ Object

Raises:



91
92
93
94
95
96
# File 'lib/cbf_calendario/client.rb', line 91

def self.normalize_id_clube!(id_clube)
  s = id_clube.to_s.strip
  raise InvalidClubIdError, 'id_clube deve conter só dígitos (ex.: 20001)' unless s.match?(/\A\d+\z/)

  s
end

.normalize_id_jogo!(id_jogo) ⇒ Object

Raises:



84
85
86
87
88
89
# File 'lib/cbf_calendario/client.rb', line 84

def self.normalize_id_jogo!(id_jogo)
  s = id_jogo.to_s.strip
  raise InvalidGameIdError, 'id_jogo deve conter só dígitos (ex.: 832024)' unless s.match?(/\A\d+\z/)

  s
end

.parse_data_br!(str) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cbf_calendario/client.rb', line 131

def self.parse_data_br!(str)
  m = str.to_s.strip.match(/\A(\d{2})\/(\d{2})\/(\d{4})\z/)
  raise InvalidDateError, 'Use dd/mm/aaaa (ex.: 15/05/2026)' unless m

  day = m[1].to_i
  month = m[2].to_i
  year = m[3].to_i
  Date.new(year, month, day)
rescue ArgumentError
  raise InvalidDateError, 'Data inválida'
end

Instance Method Details

#atleta_por_id(id_atleta) ⇒ Object

Busca dados completos de um atleta por ID. Saída: { atleta_id: “123”, atleta: { … } }

Raises:



75
76
77
78
79
80
81
82
# File 'lib/cbf_calendario/client.rb', line 75

def atleta_por_id(id_atleta)
  aid = Client.normalize_id_atleta!(id_atleta)
  payload = buscar_atleta_payload(aid)
  atleta = extrair_atleta_do_payload(payload, aid)
  raise HttpError, 'Resposta sem dados do atleta' unless atleta.is_a?(Hash) && !atleta.empty?

  { atleta_id: aid, atleta: atleta }
end

#atletas_do_clube(id_clube) ⇒ Object

GET /api/cbf/atletas/:id_clube — retorna hash com dados dos atletas. Saída: { clube_id: “123”, atletas: [ … ] }



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cbf_calendario/client.rb', line 47

def atletas_do_clube(id_clube)
  cid = Client.normalize_id_clube!(id_clube)
  payload = get_json(api_path_atletas_clube(cid))
  atletas =
    if payload.is_a?(Array)
      payload
    elsif payload.is_a?(Hash) && payload['atletas'].is_a?(Array)
      payload['atletas']
    else
      raise HttpError, 'Resposta sem lista de atletas'
    end

  { clube_id: cid, atletas: atletas }
end

#calendario_json(data) ⇒ Object

Payload bruto da API para o dia (Hash).



117
118
119
120
# File 'lib/cbf_calendario/client.rb', line 117

def calendario_json(data)
  date = Client.coerce_date!(data)
  get_json(api_path_calendario(date))
end

#clube_por_id(id_clube) ⇒ Object

Busca dados completos de um clube por ID. Saída: { clube_id: “123”, clube: { … } }

Raises:



64
65
66
67
68
69
70
71
# File 'lib/cbf_calendario/client.rb', line 64

def clube_por_id(id_clube)
  cid = Client.normalize_id_clube!(id_clube)
  payload = buscar_clube_payload(cid)
  clube = extrair_clube_do_payload(payload, cid)
  raise HttpError, 'Resposta sem dados do clube' unless clube.is_a?(Hash) && !clube.empty?

  { clube_id: cid, clube: clube }
end

#jogo_partida(id_jogo) ⇒ Object

Apenas payload (Hash com string keys).



41
42
43
# File 'lib/cbf_calendario/client.rb', line 41

def jogo_partida(id_jogo)
  partida_completa(id_jogo)['jogo']
end

#jogos_pendentes_no_dia(data) ⇒ Object

Aceita Date, Time ou String “dd/mm/aaaa”. Retorna Array<Hash> com símbolos como chaves, ordenado e sem IDs duplicados. Cada hash: campeonato, serie, mandante, visitante, placar_ou_horario, data, data_iso, local, rodada, id_jogo.



109
110
111
112
113
114
# File 'lib/cbf_calendario/client.rb', line 109

def jogos_pendentes_no_dia(data)
  date = Client.coerce_date!(data)
  payload = get_json(api_path_calendario(date))
  jogos = extrair_jogos_pendentes(payload, date)
  dedup_and_sort(jogos)
end

#partida_completa(id_jogo) ⇒ Object

GET /api/cbf/jogos/:id — resposta completa da API (mesmo conteúdo que show_game.rb grava em JSON). Chaves são String como no JSON original.

Raises:



32
33
34
35
36
37
38
# File 'lib/cbf_calendario/client.rb', line 32

def partida_completa(id_jogo)
  jid = Client.normalize_id_jogo!(id_jogo)
  payload = get_json(api_path_jogo(jid))
  raise HttpError, 'Resposta sem objeto "jogo"' unless payload['jogo'].is_a?(Hash)

  payload
end