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)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
# File 'lib/football-timezones/timezones.rb', line 115

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



106
107
108
109
110
111
112
113
# File 'lib/football-timezones/timezones.rb', line 106

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