Class: Fifa
- Inherits:
-
Object
- Object
- Fifa
- Defined in:
- lib/fifadat/api.rb,
lib/fifadat/config.rb
Defined Under Namespace
Classes: Metal
Constant Summary collapse
- BASE_URL =
'https://api.fifa.com/api/v3'- COMPETITIONS =
read_seasons( *Dir.glob( "#{Fifadat.root}/config/seasons/**/*.csv"))
- CODES =
code (slugs) mappings for competitions
read_codes( "#{Fifadat.root}/config/codes_nati.csv", "#{Fifadat.root}/config/codes_club.csv", "#{Fifadat.root}/config/codes_club-europe.csv", )
Class Method Summary collapse
- ._idComp_by!(name:) ⇒ Object
- ._idSeason_by!(name:, season:) ⇒ Object
-
.competition_url(name:) ⇒ Object
note - singular !! (not plural).
-
.competitions_url ⇒ Object
list first 50 comps.
- .read_codes(*paths) ⇒ Object
- .read_seasons(*paths) ⇒ Object
- .search_matches_url(from:, to:, count: 500) ⇒ Object
- .search_seasons_url(name:) ⇒ Object
Class Method Details
._idComp_by!(name:) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/fifadat/config.rb', line 89 def self._idComp_by!( name: ) ## note - downcase and remove all spaces from name ## e.g. WORLD CUP => worldcup q = name.downcase.gsub( ' ', '' ) rec = CODES[ q ] if rec.nil? raise ArgumentError, "no competition (idCompetition) found for #{name}; sorry" end rec[:idCompetition] end |
._idSeason_by!(name:, season:) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fifadat/config.rb', line 102 def self._idSeason_by!( name:, season: ) ## note - lookup season key is a string season = Season( season ).to_s idComp = _idComp_by!( name: name ) rec = COMPETITIONS[ idComp ][ season ] if rec.nil? raise ArgumentError, "no idSeason found for #{name} #{season}; sorry" end rec[:idSeason] end |
.competition_url(name:) ⇒ Object
note - singular !! (not plural)
43 44 45 46 |
# File 'lib/fifadat/api.rb', line 43 def self.competition_url( name: ) ## note - singular !! (not plural) idCompetition = _idCompetition_by!( name: name.downcase ) Metal.competition_url( idCompetition: idCompetition ) end |
.competitions_url ⇒ Object
list first 50 comps
38 39 40 |
# File 'lib/fifadat/api.rb', line 38 def self.competitions_url ## list first 50 comps "#{BASE_URL}/competitions/?language=en" end |
.read_codes(*paths) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fifadat/config.rb', line 45 def self.read_codes( *paths ) comps = {} paths.each do |path| rows = read_csv( path ) ## hack - use club in base as automagic flag basename = File.basename(path, File.extname(path)) club = basename.include?('club') ## otherwise assume nati(onal) teams rows.each do |row| ## note - do NOT convert to integer!!! ## e.g. a5rfzm8qpy3ca8d8wxlkkdslw ## keep as string ## todo/fix ## allow alternatives e.g. ## world|worldcup ## eng|eng.1 etc. code = row['code'] idComp = row['id_comp'] || row['IdCompetition'] rec = { code: code, idCompetition: idComp, club: club, # club | nati(ional team) flag } comps[code] = rec end end comps end |
.read_seasons(*paths) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fifadat/config.rb', line 8 def self.read_seasons( *paths ) comps = {} paths.each do |path| rows = read_csv( path ) rows.each do |row| ## note - do NOT convert to integer!!! ## e.g. a5rfzm8qpy3ca8d8wxlkkdslw ## keep as string idComp = row['id_comp'] || row['IdCompetition'] idSeason = row['id_season'] || row['IdSeason'] ## e.g. '2026' or '2026/27' etc. ## normalize season season = Season.parse( row['season'] ).to_s seasons = comps[idComp] ||={} seasons[ season ] = { season: season, idSeason: idSeason, idCompetition: idComp, name: row['name'], start_date: row['start_date'], end_date: row['end_date'] } end end comps end |
.search_matches_url(from:, to:, count: 500) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fifadat/api.rb', line 8 def self.search_matches_url( from:, to:, count: 500 ) ## ## https://api.fifa.com/api/v3/calendar/matches?from=2026-03-24T00%3A00%3A00Z ## &to=2026-03-31T23%3A59%3A59Z ## &language=en ## &count=100 from_encoded = URI.encode_www_form_component( from ) to_encoded = URI.encode_www_form_component( to ) "#{BASE_URL}/calendar/matches?"+ "from=#{from_encoded}"+ "&to=#{to_encoded}"+ "&language=en"+ "&count=#{count}" end |
.search_seasons_url(name:) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/fifadat/api.rb', line 27 def self.search_seasons_url( name: ) ## API_ROOT/seasons/search?name=FIFA%20U-20%20Women%20World%20Cup ## todo/fix - url encode name!!! ## note - encodes spaces as plus (+) NOT %20 name_encoded = URI.encode_www_form_component( name ) "#{BASE_URL}/seasons/search?name=#{name_encoded}&count=500" end |