Module: TimezoneHelper

Included in:
Kernel
Defined in:
lib/football-timezones/timezones.rb

Instance Method Summary collapse

Instance Method Details

#find_zone(league:, season:) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/football-timezones/timezones.rb', line 132

def find_zone( league:, season: )
   ## note: do NOT pass in league struct! pass in key (string)
   raise ArgumentError, "league key as string|symbol expected"  unless league.is_a?(String) || league.is_a?(Symbol)

   @zones ||= begin
                zones = {}
                ['timezones_africa',
                 'timezones_america',
                 'timezones_asia',
                 'timezones_europe',
                 'timezones_middle_east',
                 'timezones_pacific',
                 'timezones_world',].each do |name|
                     recs = read_csv( "#{SportDb::Module::Timezones.root}/config/#{name}.csv" )
                     recs.each do |rec|
                        zone = UTC.find_zone( rec['zone'] )
                        if zone.nil?
                          ## raise ArgumentError - invalid zone
                          puts "!! ERROR - cannot find timezone in timezone db:"
                          pp rec
                          exit 1
                        end
                        zones[ rec['key']] = zone
                     end
                 end
                 zones
              end

   ## lookup first try by league+season
   league_code = league.to_s.downcase
   season      = Season( season )

   ##  e.g. world+2022, etc.
   key = "#{league_code}+#{season}"
   zone = @zones[key]

   ## try league e.g. eng.1 etc.
   zone = @zones[league_code]  if zone.nil?

   ## try first code only (country code )
   if zone.nil?
     code, _  = league_code.split( '.', 2 )
     zone = @zones[code]
   end

   zone
end

#find_zone!(league:, season:) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/football-timezones/timezones.rb', line 123

def find_zone!( league:, season: )
  zone = find_zone( league: league, season: season )
  if zone.nil?  ## still not found; report error
    puts "!! ERROR: no timezone found for #{league} #{season}"
    exit 1
  end
  zone
end